2011-10-23 8 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です。これらの3行は、uvで割り、余りをtempに格納します。次にuvの値を取得し、vは剰余を取得します。 v私はあなたが見ることができるようにあなたがeuclideansアルゴリズム

を使用している参照0

+0

ありがとうございます。本当に役に立つ – Ben

0

ないが、プロセスが繰り返され、temp = u%v%は、それが格納されているUとVとの残りの部分を分割するモジュロ演算子であります一時的にの値が変数vに格納され、最後にtempの値が変数vに格納されます。この全プロセスは、vの値が0に等しくないか0になるまで繰り返されます。