#include "stdio.h"
int gcd(int a,int b){
int r;
while(r=a%b)
a=b,b=r;
return b;
}
int lcm(int a,int b){
return a/gcd(a,b)*b;
}
int main(int argc,char *argv[]){
int x,y;
printf("Please enter 2 positive integers...\n");
if(scanf("%d%d",&x,&y)!=2 || x<1 || y<1){
printf("Input error, exit...\n");
return 0;
}
printf("The GCD of %d & %d is %d\n",x,y,gcd(x,y));
printf("The LCM of %d & %d is %d\n",x,y,lcm(x,y));
return 0;
}
运行样例:
![](https://video.ask-data.xyz/img.php?b=https://iknow-pic.cdn.bcebos.com/023b5bb5c9ea15ce63872293ba003af33a87b23b?x-bce-process=image%2Fresize%2Cm_lfit%2Cw_600%2Ch_800%2Climit_1%2Fquality%2Cq_85%2Fformat%2Cf_auto)
追问if(scanf("%d%d",&x,&y)!=2 || x<1 || y<1){ printf("Input error, exit...\n");这一步是判断什么的
追答那不写着吗?判断输入是不是错误了……