天梯赛 L1-009 N个数求和 (20 分)
生活随笔
收集整理的這篇文章主要介紹了
天梯赛 L1-009 N个数求和 (20 分)
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
涉及假分?jǐn)?shù)和負(fù)數(shù),因此是分子絕對(duì)值與分母比較(分母必正)
#include <iostream> #include <cmath> using namespace std;int gcd(int a, int b) {return b ? gcd(b, a % b) : a; }int main() { // ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);int n; scanf("%d", &n);int a, b, c, d;scanf("%d/%d", &b, &a);int t = abs(gcd(a, b));a /= t, b /= t;for (int i = 1; i < n; i ++ ){scanf("%d/%d", &d, &c);int t = abs(gcd(c, d));d /= t, c /= t;b = b * c + a * d;a = a * c;t = abs(gcd(a, b));a /= t, b /= t;}// if (b == 0) cout << 0; // else if (b < a) cout << b << '/' << a; // else // { // if (b % a == 0) cout << b / a; // else cout << b / a << ' ' << b % a << '/' << a; // }if (b == 0) puts("0");else if (abs(b) < a) printf("%d/%d", b, a);else{if (abs(b) % a == 0) printf("%d", b / a);else{printf("%d ", b / a);b -= (b / a * a);printf("%d/%d", b, a);}} }總結(jié)
以上是生活随笔為你收集整理的天梯赛 L1-009 N个数求和 (20 分)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 第十一届蓝桥杯大赛软件赛省赛 C/C++
- 下一篇: 天梯赛 L1-023 输出GPLT (2