求助关于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的学生,不足地方多多指教。
...全文
153 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
黑暗小车 2018-12-18
  • 打赏
  • 举报
回复
黑暗小车 2018-12-18
  • 打赏
  • 举报
回复
黑暗小车 2018-12-18
  • 打赏
  • 举报
回复
自信男孩 2018-12-18
  • 打赏
  • 举报
回复
建议贴出更多信息,比如你的输入信息,输出结果等等;

    for(i=0;i<number;i++)
scanf("%ld%c%ld",&son[i],&c,&mother[i]);

这个地方很可能是你的输入有问题,因为这里有%c,很可能出现你的输入和你的想要的输入不一致。
@风轻云淡_ 2018-12-18
  • 打赏
  • 举报
回复
表示看你的代码真不如看题目来的直接,虽然不明白你想干嘛。
scanf("%ld",&number);
for(i=0;i<number;i++)//这里是不是漏了大括号了,为什么没有大括号会有缩进
scanf("%ld%c%ld",&son[i],&c,&mother[i]);
for(i=0;i<number;i++)
lin5161678 2018-12-18
  • 打赏
  • 举报
回复
既然你题目都不说了
那我就问题也不答了
来呀 互相伤害呀
斜眼.jpg
黑暗小车 2018-12-18
  • 打赏
  • 举报
回复
自己顶下

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧