2011-10-23 45 views
2

我是ios编程的新手。我有关于GCD计划的问题。最大公约数Objective-C

01 // This program finds the greatest common divisor of two nonnegative integer values 
02 
03 #import <Foundation/Foundation.h> 
04 
05 int main (int argc, const char * argv[]) { 
06  NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; 
07  unsigned int u, v, temp; 
08  
09  NSLog(@"Please type in two nonnegative integers."); 
10  scanf("%u%u", &u, &v); 
11  
12  while (v != 0) { 
13   temp = u % v; 
14   u = v; 
15   v = temp; 
16  } 
17  
18  NSLog(@"Their greatest common divisor is %u", u); 
19  
20  [pool drain]; 
21  return 0; 
22 } 

我不明白这个部分:

while(v!=0) 
    temp = u%v 
    u =v; 
    v = temp; 

这是什么意思,在英语?

回答

1

%是mod operator。这三条线将u除以v并将余数存储在温度中。然后u得到v的值,v得到余数。重复这个过程,而v不为0

+0

谢谢。非常有帮助 – Ben

0

我看你使用euclideans算法

,你可以看到,temp = u%v%是模运算符的地方划分u和v和它的剩余存储在温度然后将值存储在变量v中,最后将temp值存储在变量v中,并且整个过程将重复,直到v的值不等于0或不等于0为止。