江琛

N个数相加(分子分母型)

#include<bits/stdc++.h>
using namespace std;
struct {
int son;
char ch;
int mom;
}a[105];
int main() {
int n,up=0,down=1;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i].son >> a[i].ch >> a[i].mom;
up = up * a[i].mom + down* a[i].son;
down *= a[i].mom;//通分
int gck =abs(__gcd(up,down));
up /= gck;
down /= gck;//约分
}//循环结束
if (up % down == 0) {
cout << up / down;//结果是一个整数
}
else {
if (up / down) cout << up / down << " ";//结果大于1
cout << up - up / down * down << "/" << down;
}
}

posted on 2022-03-06 11:40  江琛  阅读(65)  评论(0编辑  收藏  举报

导航