#训练6# 2-N个数求和

题目

#训练6# 2-N个数求和

思路

gcd
样例三!!long long !!要全给成ll

代码

#include <bits/stdc++.h>

#define maxx 199
typedef long long ll;
using namespace std;
ll up[maxx], down[maxx];

ll gcd(ll a, ll b) //最大公因数
{
    if (a % b == 0)
        return b;
    return gcd(b, a % b);
}

int main()
{
    int n;
    cin >> n;
    char c;
    for (int i = 0; i < n; ++i)
    {
        cin>>up[i]>>c>>down[i];
    }
    ll aaa = 0;
    for (int i = 0; i < n - 1; ++i)
    {
        aaa = gcd(abs(down[i]), abs(down[i + 1]));
        aaa = abs(down[i] / aaa * down[i + 1]);
    }
    //cout<<aaa;
    ll ans_up = 0;
    for (int i = 0; i < n; ++i)
    {
        ans_up += (aaa / down[i] * up[i]);
    }
    ll sum = gcd(ans_up, aaa);
    // cout<<sum;
    ans_up /= sum;
    aaa /= sum;

    if (ans_up % aaa == 0)
        cout << ans_up / aaa << endl;
    else if (ans_up > aaa)
            cout << ans_up / aaa << ' ' << ans_up % aaa << "/" << aaa << endl;
    else
        cout << ans_up << "/" << aaa << endl;
    return 0;
}
上一篇:<mvc:default-servlet-handler>标签的作用 - springmvc


下一篇:关于SpringMVC在Controller层方法的参数解析详解