有以下程序,若运行时从键盘输入:18,11<回车>,则程序的输出结果是______.
Main()
{int a,b;
printf("Enter a,b;");scanf("%d,%d",&a,&b);
while(a!=b)
{while(a>b)a-=b;
while(b>a)b-=a;
}
printf("%3d%3d\n",a,b);
}
为什么输出结果是1 1?
參考答案:Main()
{int a,b;
printf("Enter a,b;");scanf("%d,%d",&a,&b); //a=18,b=11
while(a!=b)//a!=b, 18!=11
{
while(a>b)a-=b; //18>11,a=7----b=11
while(b>a)b-=a; //11>7, b=4----a=7
}
//第二次,a=7,b=4
//while(a!=b) 7不等于4
//{
//while(a>b)a-=b; a=3-----b=4
//while(b>a)b-=a; a=3-----b=1
//}
//第三次,a=3,b=1
//while(a!=b) 3不等于1
//{
//while(a>b)a-=b; a=2-----b=1,a>b???, a=1,b=1
//while(b>a)b-=a; b>a?不同退出
//}
//第四次,a=1,b=1
//while(a!=b) a等于b循环结束
printf("%3d%3d\n",a,b); //1 1
}