# 目标C中的Euclid算法

``````#import <Foundation/Foundation.h>

int main(int argc, const char * argv[]) {
@autoreleasepool {

unsigned int u;
unsigned int v;
unsigned int temp;

NSLog(@"Enter two numbers: ");
scanf("%u%u", &u , &v);

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

NSLog(@" Greatest Common Factor: %u" , u);
}
return 0;
}
``````

• 情况1：如果用户输入`10 2`两个值，则答案为2
• 情况2：如果用户将整数反转为，该`2 10`怎么办？（答案仍然是2）

`````` 1  #import <Foundation/Foundation.h>
2
3  int main(int argc, const char * argv[]) {
4      @autoreleasepool {
5          unsigned int u, v, temp;
6          printf("Enter two numbers:\n");
7          scanf("%u%u", &u , &v);
8          while (v != 0)
9          {
10              temp = u % v;
11              printf("(11) u=%u v=%u temp=%u\n", u, v, temp);
12              u = v;
13              printf("(13) u=%u v=%u temp=%u\n", u, v, temp);
14              v = temp;
15              printf("(15) u=%u v=%u temp=%u\n", u, v, temp);
16          }
17          printf("Greatest Common Factor: %u\n" , u);
18      }
19      return 0;
20  }
``````

``````Enter two numbers:
2 10
(11) u=2 v=10 temp=2
(13) u=10 v=10 temp=2
(15) u=10 v=2 temp=2
(11) u=10 v=2 temp=0
(13) u=2 v=2 temp=0
(15) u=2 v=0 temp=0
Greatest Common Factor: 2
``````

0 条评论