2

我是 ios 编程新手。我对 GCD 计划有疑问。

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;

这是什么意思,用英语?

4

3 回答 3

2

该部分实际上使用欧几里得算法计算最大公约数。

于 2011-10-23T02:39:32.220 回答
1

% 是mod 运算符。这三行除以u并将v余数存储在 temp 中。然后u取 的值vv取余数。v不为 0时重复该过程。

于 2011-10-23T02:40:39.233 回答
0

我看到你正在使用欧几里得算法

如您所见,temp = u%v% 是一个模运算符,它将 u 和 v 相除,其余部分存储在 temp 中。然后将 的值存储在变量 v 中,最后将 temp 的值存储在变量 v 中。整个过程将重复,直到 v 的值不等于 0 或不为 0。

于 2011-12-21T13:02:41.707 回答