自贡最大风力:C语言编程

来源:百度文库 编辑:杭州交通信息网 时间:2024/05/09 10:19:48
写两个函数,分别求两个整数的最大公约数和最小公倍数。
(1)用全局变量的的方法。将两个整数的最大公约数和最小公倍数都设为全局变量。

(2)不用全局变量,两个整数在主函数中输入,利用函数求最大公约数和最小公倍数。

#include<stdio.h>

int min,max; //全局变量
void fun(int a,int b,int *min_c,int *max_d)
{
int temp,t;

if(a<b) //大小数换位置
{
temp=a;
a=b;
b=temp;
}

temp=a*b; //保存二数的积

while(b!=0) //求最大公约数
{
t=a%b;
a=b;
b=t;
}

*min_c=a;
*max_d=temp/(*min_c); //a即为最大公约数
max=*max_d;
min=*min_c;
}

void main()
{
int number1,number2;
int min_multiple=0,max_divisor=0;//局部变量

scanf("%d,%d",&number1,&number2);

fun(number1,number2,&min_multiple,&max_divisor);

printf("The min common multiple is %d\n the Max common divisor is %d\n",min,max);

printf("The min common multiple is %d\n the Max common divisor is %d\n",min_multiple,max_divisor);

}

你看看谭浩强的c语言编程,看完了,看懂了,你就会了。

(1)用全局变量的的方法。将两个整数的最大公约数和最小公倍数都设为全局变量。
#include <math.h>
#include <stdlib.h>
int c,d; /*定义全局变量*/
void grtest(int a,int b)
{
int i,k;
k=min(a,b);
c=1;
for(i=2;i<=k;i++)
{
if((a%i==0)&&(b%i==0))
c=i;
}
}
void lease(int a,int b)
{
int i,k,e,f;
k=min(a,b);
f=max(a,b);
d=e=a*b;
for(i=k;i<e;i+=k)
{
if(i%f==0)
{
d=i;
break;
}
}
}
void main()
{
int a,b;
printf("\nPlease input two integer:");
scanf("%d%d",&a,&b);
grtest(a,b);
lease(a,b);
printf("Greatest common divisor is %d,lease common multiple is %d",c,d);
getch();
}
(2)不用全局变量,两个整数在主函数中输入,利用函数求最大公约数和最小公倍数。
#include <math.h>
#include <stdlib.h>
int grtest(int a,int b)
{
int i,c,d=1;
c=min(a,b);
for(i=2;i<=c;i++)
{
if((a%i==0)&&(b%i==0))
d=i;
}
return d;
}
int lease(int a,int b)
{
int i,c,d,e,f;
c=min(a,b);
f=max(a,b);
d=e=a*b;
for(i=c;i<e;i+=c)
{
if(i%f==0)
{
d=i;
break;
}
}
return d;
}
void main()
{
int a,b,c,d;
printf("\nPlease input two integer:");
scanf("%d%d",&a,&b);
c=grtest(a,b);
d=lease(a,b);
printf("Greatest common divisor is %d,lease common multiple is %d",c,d);
getch();
}
已经运行过了,并且是正确的。编译器TC2.0。

#include<stdio.h>

int min,max; //全局变量
void fun(int a,int b,int *min_c,int *max_d)
{
int temp,t;

if(a<b) //大小数换位置
{
temp=a;
a=b;
b=temp;
}

temp=a*b; //保存二数的积

while(b!=0) //求最大公约数
{
t=a%b;
a=b;
b=t;
}

*min_c=a;
*max_d=temp/(*min_c); //a即为最大公约数
max=*max_d;
min=*min_c;
}

void main()
{
int number1,number2;
int min_multiple=0,max_divisor=0;//局部变量

scanf("%d,%d",&number1,&number2);

fun(number1,number2,&min_multiple,&max_divisor);

printf("The min common multiple is %d\n the Max common divisor is %d\n",min,max);

printf("The min common multiple is %d\n the Max common divisor is %d\n",min_multiple,max_divisor);

}
#include<stdio.h>

int min,max; //全局变量
void fun(int a,int b,int *min_c,int *max_d)
{
int temp,t;

if(a<b) //大小数换位置
{
temp=a;
a=b;
b=temp;
}

temp=a*b; //保存二数的积

while(b!=0) //求最大公约数
{
t=a%b;
a=b;
b=t;
}

*min_c=a;
*max_d=temp/(*min_c); //a即为最大公约数
max=*max_d;
min=*min_c;
}

void main()
{
int number1,number2;
int min_multiple=0,max_divisor=0;//局部变量

scanf("%d,%d",&number1,&number2);

fun(number1,number2,&min_multiple,&max_divisor);

printf("The min common multiple is %d\n the Max common divisor is %d\n",min,max);

printf("The min common multiple is %d\n the Max common divisor is %d\n",min_multiple,max_divisor);

}
#include<stdio.h>

int min,max; //全局变量
void fun(int a,int b,int *min_c,int *max_d)
{
int temp,t;

if(a<b) //大小数换位置
{
temp=a;
a=b;
b=temp;
}

temp=a*b; //保存二数的积

while(b!=0) //求最大公约数
{
t=a%b;
a=b;
b=t;
}

*min_c=a;
*max_d=temp/(*min_c); //a即为最大公约数
max=*max_d;
min=*min_c;
}

void main()
{
int number1,number2;
int min_multiple=0,max_divisor=0;//局部变量

scanf("%d,%d",&number1,&number2);

fun(number1,number2,&min_multiple,&max_divisor);

printf("The min common multiple is %d\n the Max common divisor is %d\n",min,max);

printf("The min common multiple is %d\n the Max common divisor is %d\n",min_multiple,max_divisor);

}

第一个很不错,简洁!
回答者:yqz_b - 助理 二级 4-3 17:12
是错的,运行后的结果不对啊!