请回c语言的大事帮帮忙

来源:百度知道 编辑:UC知道 时间:2024/07/08 18:04:33
我按照参考书编写了一个程序
#include<stdio.h>
int main(void)
{ int a,b,c;
printf("Input a and b:"); scanf("%d%d",&a,&b);
if(a<1||b<1)return 1;
c=a%b;
while(c!=0){ a=b; b=c; c=a%b; }
printf("Result is %d\n",b);
return 0;
}
听参考书说,这是一个“计算两个正整数的最大公因子”的程序,但是不知道在打开该exe文件后输入什么东西才能够计算,请帮帮忙!!!

根据你的程序应该输入两个数,中间没有空格,会得出这两个正整数的最大公因子。但会闪一下就退出了。可以加
3个getchar();
来解决
如下
#include<stdio.h>
int main(void)
{ int a,b,c;
printf("Input a and b:");
getchar();
scanf("%d%d",&a,&b);
getchar();
if(a<1||b<1)return 1;
c=a%b;
while(c!=0){ a=b; b=c; c=a%b; }
printf("Result is %d\n",b);
getchar();
return 0;
}

一般在编译器里面run的,比如常用的VC6.0,run后,就会提示你
Input a and b,这时,你只要敲键盘输入即可。

因为C程序exe文件运行完了之后就退出了,所以输出了你也看不到,所以想办法不让程序退出,你就能看到结果了。
我修改了下你的程序,当然没有你最先那样逻辑清晰。但这样能看到结果。你输入1就推出程序。
#include <stdio.h>

void main(void)
{
int a,b,c;
printf("Input a and b:"); scanf("%d%d",&a,&b);
//if(a<1||b<1)return 1;
c=a%b;
while(c!=0){ a=b; b=c; c=a%b; }
printf("Result is %d\n",b);
//return 0;
scanf("%d",&a);
if (a == 1)
{