a/b + c/d
Time Limit : 1000/1000ms (Java/Other)Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 181Accepted Submission(s) : 123
Font:Times New Roman|Verdana|Georgia
Font Size:←→
Problem Description
Input
Output
Sample Input
2 1 2 1 3 4 3 2 3
Sample Output
5 6 2 1
【解题思路】模拟题
#include <iostream> using namespace std; int up,down; int gcd(int a, int b) { for (; (a = a % b) && (b = b % a);); return (a | b); } void add(int a, int b, int c, int d) { if(b==d){ up = a+c; down = b; } else{ up = a*d+c*b; down = b*d; } int ggcd = gcd(up,down); up /= ggcd;down/=ggcd; } int main() { //freopen("test.txt","r",stdin); int n, a, b, c, d; cin >> n; while (n--) { cin >> a >> b >> c >> d; add(a,b,c,d); cout<<up<<" "<<down<<endl; } return 0; }
分数加法与简化
本文介绍了一种解决两个分数相加并将其结果简化为最简形式的方法。通过给出的示例代码,我们可以看到如何实现这一过程,包括计算通分后的分子与分母,以及使用辗转相除法找到最大公约数来简化分数。
1446

被折叠的 条评论
为什么被折叠?



