1、
#include <stdio.h> int gcd(int x, int y) { int max, min, tmp; max = x > y ? x : y; min = x > y ? y : x; tmp = x % y; if(tmp != 0) { gcd(min, tmp); } else { return min; } } int main(void) { int a, b; puts("please input two integers."); printf("a = "); scanf("%d", &a); printf("b = "); scanf("%d", &b); printf("result: %d ", gcd(a, b)); return 0; }
2、
#include <stdio.h> int gcd(int x, int y) { int max, min, tmp; max = x > y ? x : y; min = x > y ? y : x; tmp = max - min; if(tmp != 0) gcd(tmp, min); else return min; } int main(void) { int a, b; printf("a = "); scanf("%d", &a); printf("b = "); scanf("%d", &b); printf("result: %d ", gcd(a, b)); return 0; }
3、
#include <stdio.h> int gcd(int x, int y) { int tmp; if(x != y) { if(x > y) x -= y; else y -= x; tmp = gcd(x, y); } else tmp = x; return tmp; } int main(void) { int a, b; puts("please input two integers."); printf("a = "); scanf("%d", &a); printf("b = "); scanf("%d", &b); printf("result: %d ", gcd(a, b)); return 0; }
4、
#include <stdio.h> int gcd(int x, int y) { int max, min, tmp; max = x > y ? x : y; min = x > y ? y : x; if(max - min != 0) { max -= min; tmp = gcd(max,min); } else tmp = min; return tmp; } int main(void) { int a, b; puts("please input two integers."); printf("a = "); scanf("%d", &a); printf("b = "); scanf("%d", &b); printf("result: %d ", gcd(a, b)); return 0; }