c语言 8-8 求最大公约数
1、
#include <stdio.h> int gcd(int x, int y) { int max, min, tmp; if(x != y) { max = x > y ? x : y; min = x > y ? y : x; tmp = max % min; 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\n", 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(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\n", gcd(a, b)); return 0; }
3、
#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(min, tmp); else return min; } int main(void) { int a, b; puts("please input two integers."); do { printf("a = "); scanf("%d", &a); if(a <= 0) puts("the range of a is: > 0."); } while(a <= 0); do { printf("b = "); scanf("%d", &b); if(b <= 0) puts("the rage of b is: > 0"); } while(b <= 0); printf("the result: %d\n", gcd(a, b)); return 0 ; }