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

来源:百度知道 编辑:UC知道 时间:2024/06/08 14:26:48
#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;} } }
}

//输入两个正整数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);