求助关于PTA1009求和运算的问题
黑暗小车 2018-12-18 03:01:37 题目就不说了,代码发上来,c语言萌新,自己写的代码,一直找不见问题在哪。
#include<stdio.h>
long int public_times(int a,int b)
{
if(a<0)
a=-a;
if(b<0)
b=-b;
long int times=1,i=1;
while(i<=a&&i<=b)
{
for(i=2;i<=a&&i<=b;i++)
{
if(a%i==0&&b%i==0)
{
a/=i;b/=i;
times=i*times;
break;
}
}
}
return times;
}
int main()
{
long int son[101],mother[101],number,i,times=0,andson=0,andmother=1;
char c;
scanf("%ld",&number);
for(i=0;i<number;i++)
scanf("%ld%c%ld",&son[i],&c,&mother[i]);
for(i=0;i<number;i++)
{times=public_times(andmother,mother[i]);
andmother=(andmother*mother[i])/times;}
for(i=0;i<number;i++)
andson=andson+(son[i]*andmother)/mother[i];
times=public_times(andson,andmother);
andson=andson/times;andmother=andmother/times;
times=andson/andmother;
andson=andson%andmother;
if(times)
printf("%ld",times);
if(times&&andson)
printf(" ");
if(andson)
printf("%ld/%ld",andson,andmother);
if(times==0&&andson==0)
{printf("0");}
return 0;
}
本人也是刚入csdn的学生,不足地方多多指教。