在Object-C中求两个数得最大公约数,代码如下:
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[])
{
@autoreleasepool {
unsigned int u = 0;
unsigned int v = 0;
unsigned int temp = 0;
NSLog(@"Please type in two nonnegative integers.");
scanf("%u%u", &u, &v);
while (v != 0) {
temp = u % v;
u = v;
v = temp;
}
NSLog(@"Their greatest common divisor is %u.", u);
}
return 0;
}
程序运行结果如下:
2014-04-07 10:52:28.380 Object-Test[602:303] Please type in two nonnegative integers.
40 80 //此处输入40 80
2014-04-07 10:52:36.206 Object-Test[602:303] Their greatest common divisor is 40.
Program ended with exit code: 0