(a,b)=(b,amodb)
//欧几里得算法的非递归实现
#include <stdio.h> #include <stdlib.h> int gcd(int a,int b) { while(b!=0)//b不为0一直持续 { int temp=a%b; a=b;//使a变成b b=temp;//使b变成a%b } return a;//b为0时,返回a } int main() { int a,b; while(scanf("%d%d",&a,&b)!=EOF) { printf("%d\n",gcd(a,b)); } return 0; }
欧几里得算法的递归实现 #include <stdio.h> #include <stdlib.h> int gcd(int a,int b) { if(b==0)return a; else return gcd(b,a%b); } int main() { int a,b; while(scanf("%d%d",&a,&b)!=EOF) { printf("%d\n",gcd(a,b)); } return 0; }