麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 學院 > 開發設計 > 正文

1081. Rational Sum (20)

2019-11-11 04:46:18
字體:
來源:轉載
供稿:網友

題目鏈接:https://www.patest.cn/contests/pat-a-PRactise/1081 Given N rational numbers in the form “numerator/denominator”, you are supposed to calculate their sum.

Input Specification:

Each input file contains one test case. Each case starts with a positive integer N (<=100), followed in the next line N rational numbers “a1/b1 a2/b2 …” where all the numerators and denominators are in the range of “long int”. If there is a negative number, then the sign must appear in front of the numerator.

Output Specification:

For each test case, output the sum in the simplest form “integer numerator/denominator” where “integer” is the integer part of the sum, “numerator” < “denominator”, and the numerator and the denominator have no common factor. You must output only the fractional part if the integer part is 0.

Sample Input 1: 5 2/5 4/15 1/30 -2/60 8/3 Sample Output 1: 3 1/3 Sample Input 2: 2 4/3 2/3 Sample Output 2: 2 Sample Input 3: 3 1/3 -1/6 1/8 Sample Output 3: 7/24 注意點:數據范圍為int,當兩個分母相乘時,最大可以達到long long,所以如果使用int就會溢出,有一個測試點錯誤

#include<cstdio>#include<algorithm>using namespace std;typedef long long ll;const int maxn=110;ll a[maxn],b[maxn];//分別存放分子數組,分母數組 ll gcd(ll x,ll y){//求最大公約數 if(y==0) return x; else return gcd(y,x%y);}int main(){ int n; scanf("%d",&n); for(int i=0;i<n;i++){ scanf("%lld/%lld",&a[i],&b[i]); } ll x=b[0]; for(int i=1;i<n;i++){//求分母數組的最小公倍數 x=x*b[i]/gcd(x,b[i]); } for(int i=0;i<n;i++){ a[i]=a[i]*(x/b[i]); } ll ans1=0,ans2; for(int i=0;i<n;i++){ ans1+=a[i]; } ll t=gcd(abs(ans1),abs(x)); ans1/=t,ans2=x/t;// printf("%d %d/n",ans1,ans2); if(ans2==1){ printf("%d/n",ans1); }else if(abs(ans1)>ans2){ printf("%lld %lld/%lld/n",ans1/ans2,abs(ans1%ans2),ans2); }else{ printf("%lld/%lld/n",ans1,ans2); } return 0; }
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 国产无限资源在线观看 | 黄色视频a级毛片 | 一级黄色片在线看 | 亚洲成人在线视频网 | 欧美国产日韩在线观看成人 | 欧美在线观看视频一区 | 9191色| 一级尻逼视频 | 国产在线欧美日韩 | 国产人成精品综合欧美成人 | 天天鲁在线视频免费观看 | 91av网址| 色毛片| 色妞欧美| 成人毛片视频在线观看 | 国产乱free国语对白 | 久久精品国产99国产精品澳门 | 在线中文字幕观看 | 黄色大片免费看 | 亚久久| 黄wwww| 中文字幕亚洲一区二区三区 | 日本在线免费观看视频 | 麻豆视频在线观看 | 国产精品成人免费一区久久羞羞 | 成人午夜网址 | 久久草在线视频 | 56av国产精品久久久久久久 | 91短视频网页版 | 毛片免费在线 | 一级毛片在线观看免费 | 日韩欧美综合在线 | 黄色网络免费看 | 亚洲国产精品久久久久久久久 | 精品国产91久久久久久 | 91精品久久久久久久久久久 | 毛片网站网址 | 一边吃奶一边摸下娇喘 | 亚洲啊v在线观看 | 精品黑人一区二区三区国语馆 | 久久婷婷一区二区三区 |