c语言程序试题求解~ (急)

一、根据注释将求任意两个正整数的最大公约数(GCD)和最小公倍数(LCM)的程序补充完整。(24分)
[问题分析与算法设计]
手工方式求两个正整数的蝚大公约数的方法是用辗转相除法,在程序中可以模拟这种方式。
#include<stdio.h>
void main()
{
int a,b,num1,num2,temp;
printf("Input a & b:");
scanf("%d%d",&num1,&num2);
if( 1 ) /*找出两个数中的较大值*/
{
temp=num1; 2 ; 3 ; /*交换两个整数*/
}
a=num1; b=num2;
while( 4 ) /*采用辗转相除法求最大公约数*/
{
temp=a%b;
a=b;
b=temp;
}
printf("The GCD of %d and %d is: %d\n",num1,num2, 5 ); /*输出最大公约数*/
printf("The LCM of them is: %d\n", 6 ); /*输出最小公倍数*/
}
上机调试下列输入的运行结果
7.Input a & b: 20 55
8.Input a & b: 17 71

二、根据注释将比较两个分数大小的程序补充完整。(18分)
[问题分析与算法设计]
人工方式下比较分数大小最常用的方法是:进行分数的通分后比较分子的大小。可以编程模拟手式方式。
#include<stdio.h>
int zxgb(int a,int b);
void main()
{
int i,j,k,l,m,n;
printf("Input two FENSHU:\n");
scanf("%d/%d,%d/%d", 9 ); /*输入两个分数*/
m=zxgb(j,l)/j*i; /*求出第一个分数通分后的分子*/
n= 10 /*求出第二个分数通分后的分子*/
if(m>n) printf("%d/%d>%d/%d\n", 11 ); /*比较分子的大小*/
else if(m==n) printf("%d/%d=%d/%d\n",i,j,k,l); /*输出比较的结果*/
else printf("%d/%d<%d/%d\n",i,j,k,l);
}

int zxgb(int a,int b)
{
long int c;
int d;
if(a<b) c=a, 12 ; /*若a<b,则交换两变量的值*/
for(c=a*b;b!=0;)
{
d=b; b=a%b; a=d;
}
return (int)c/a;
}
上级调试下列输入的运行结果
13.输入: 4/5,6/7 输出:
14.输入: 8/4,16/32 输出:

1. num1 < num2
2. num1 = num2
3. num2 = temp
4. b != 0
5. b
6. b*(num1/b)*(num2/b)
7. The GCD of 20 and 55 is:5
The LCM of 20 and 55 is:220
8. The GCD of 20 and 55 is:1
The LCM of 20 and 55 is:1207
温馨提示:答案为网友推荐,仅供参考

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网