C++程序求解。。。关于三个数求最大公约数和最小公倍数
#include"stdio.h"main(){inta,b,c,r,g,h,num1,num2,mum3,t;printf("pleaseinput3num:\n");...
#include"stdio.h"
main()
{
int a,b,c,r,g,h,num1,num2,mum3,t;
printf("please input 3 num:\n");
scanf("%d,%d,%d",&num1,&num2,&num3);
if(num1<num2)
{t=num1;
num1=num2;
num2=t;}
if(num1<num3)
{t=num1;
num1=num3;
num3=t;}
if(num2<num3)
{t=num2;
num2=num3;
num3=t;}
a=num1;
b=num2;
c=num3;
while(b!=0)
{r=a%b;
a=b;
b=r;}
g=b;
while(g!=0)
{r=c%g;
c=g;
g=r;
}
printf("它们的最大公约数为:%d\n",g);
printf("它的最小公倍数为:h=%d\n",h=a*b*c/g/g);
}
帮忙看哈哪错了 展开
main()
{
int a,b,c,r,g,h,num1,num2,mum3,t;
printf("please input 3 num:\n");
scanf("%d,%d,%d",&num1,&num2,&num3);
if(num1<num2)
{t=num1;
num1=num2;
num2=t;}
if(num1<num3)
{t=num1;
num1=num3;
num3=t;}
if(num2<num3)
{t=num2;
num2=num3;
num3=t;}
a=num1;
b=num2;
c=num3;
while(b!=0)
{r=a%b;
a=b;
b=r;}
g=b;
while(g!=0)
{r=c%g;
c=g;
g=r;
}
printf("它们的最大公约数为:%d\n",g);
printf("它的最小公倍数为:h=%d\n",h=a*b*c/g/g);
}
帮忙看哈哪错了 展开
展开全部
问题貌似比较多,最大公约数差不多改好了,你的最小公倍数算法有问题
PS:多注意缩进
#include"stdio.h"
int main() {
int a, b, c, r, g, h, num1, num2, num3, t;
printf("please input 3 num:\n");
scanf("%d %d %d", &num1, &num2, &num3);
if(num1 < num2) {
t = num1;
num1 = num2;
num2 = t;
}
if(num1 < num3) {
t = num1;
num1 = num3;
num3 = t;
}
if(num2 < num3) {
t = num2;
num2 = num3;
num3 = t;
}
a = num1;
b = num2;
c = num3;
//printf("%d %d %d\n", a, b, c);
while(b != 0) {
r = a % b;
a = b;
b = r;
}
g = a;
//printf("%d\n", g);
if (g > c) g ^= c ^= g ^= c;
while(g != 0) {
r = c % g;
c = g;
g = r;
}
printf("它们的最大公约数为:%d\n",c);
//printf("它的最小公倍数为:h=%d\n",h=a*b*c/g/g);
}
PS:多注意缩进
#include"stdio.h"
int main() {
int a, b, c, r, g, h, num1, num2, num3, t;
printf("please input 3 num:\n");
scanf("%d %d %d", &num1, &num2, &num3);
if(num1 < num2) {
t = num1;
num1 = num2;
num2 = t;
}
if(num1 < num3) {
t = num1;
num1 = num3;
num3 = t;
}
if(num2 < num3) {
t = num2;
num2 = num3;
num3 = t;
}
a = num1;
b = num2;
c = num3;
//printf("%d %d %d\n", a, b, c);
while(b != 0) {
r = a % b;
a = b;
b = r;
}
g = a;
//printf("%d\n", g);
if (g > c) g ^= c ^= g ^= c;
while(g != 0) {
r = c % g;
c = g;
g = r;
}
printf("它们的最大公约数为:%d\n",c);
//printf("它的最小公倍数为:h=%d\n",h=a*b*c/g/g);
}
追问
我不懂缩进诶,,,
追答
缩进只是编写程序风格问题,阅读起来可以更清楚,可以参考http://wenku.baidu.com/view/505d30260722192e4536f649.html
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询