本关任务:编写两个函数,分别计算两个正整数的最小公倍数和最大公约数的函数。
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 int MinCommonMultiple(int a, int b); 5 int MaxCommonFactor(int a, int b); 6 7 int main(void) 8 { 9 int a, b, x,y; 10 printf("Input two positive integers,a,b:\n"); 11 scanf("%d,%d", &a, &b); 12 /************Begin************/ 13 if(a<=0 || b<=0){ 14 15 printf("Input error!"); 16 }else{ 17 x=MinCommonMultiple(a,b); 18 y=MaxCommonFactor(a,b); 19 20 printf("MinCommonMultiple = %d\n",x); 21 printf("MaxCommonFactor = %d",y); 22 } 23 /************End************/ 24 return 0; 25 } 26 27 int MinCommonMultiple(int a, int b) 28 { 29 int t,i; 30 if(a>b){ 31 t=a; 32 a=b; 33 b=t; 34 } 35 for(i=2*a;i<=a*b;i=i+a){ 36 if(i%b==0){ 37 return i; 38 } 39 40 } 41 42 } 43 44 int MaxCommonFactor(int a, int b) 45 { 46 int t,i; 47 if(a>b){ 48 t=a; 49 a=b; 50 b=t; 51 } 52 for(i=a;i>=1;i--){ 53 if(a%i==0 && b%i==0){ 54 return i; 55 } 56 } 57 58 }