补充C语言程序:求两个整数m和n的最大公约数,把它存在变量t中

#include "stdio.h"
#include "math.h"
#include "conio.h"
#include "stdlib.h"
void main()
{ int m=76,n=40,t,i;
/***********begin***********/

/************end************/
printf("The Highest Common Divisor of %d and %d is %d\n",m,n,t);
NONO(m,n,t);
}

NONO( int m,int n,int t)
{ FILE *f;
f=fopen("E:\\exam\\01300136\\PROGOUT.DAT","w");
fprintf(f,"Maximal Common Divisor Of %d and %d is %d\n",m,n,t);
fclose(f);
}

变量t存放 最大公约数 前两天才给他们写的
if(m<n)
{
if(n%m==0) t=m; //整除
else
{ for(i=m/2;i>=1;i--) { if(n%i==0&&m%i==0) {t=i;break;} } }
}
else //m>=n时
{
if(m%n==0) t=n;
else
{ for(i=n/2;i>=1;i--){if(m%i==0&&n%i==0) {t=i;break;} } }
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2020-02-10
//输入两个正整数m和n,求其最大公约数
#include
void
main()
{
int
t,r,n,m,temp;
printf("请输入两个正整数n,m:");
scanf("%d,%d",&n,&m);
//把大数放在n中,小数放在m中
if(n
评论
0
0
加载更多
第2个回答  2009-06-11
//输入两个正整数m和n,求其最大公约数
#include <stdio.h>

void main()
{
int t,r,n,m,temp;
printf("请输入两个正整数n,m:");
scanf("%d,%d",&n,&m);

//把大数放在n中,小数放在m中
if(n<m)
{
temp=n;
n=m;
m=temp;
}

//展转相除法,求最大公约数
while(m!=0)
{
r=n%m;
n=m;
m=r;
}
t=n;
printf("它们的最大公约数是%d\n",t);
第3个回答  2019-06-20
int
num[1000];
int
n;
int
mcd(a,b)
//辗转相除法求最大公约数
int
a,b;
{
int
s;
if(b>a)
{a=a+b;
b=a-b;
a=a-b;
}
s=a%b;
while(s!=0)
{
a=b;
b=s;
s=a%b;
}
return
b;
}
int
input()
{
int
i;
scanf("%d",&n);
for(i=0;i
max)
max=num[i];
return
max;
}
int
main()
{
int
f;
int
i;
int
m;
input();
f=mcd(num[0],num[1]);
for(i=2;i
评论
0
0
加载更多