日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

1081. Rational Sum (20) -最大公约数

發布時間:2023/12/18 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 1081. Rational Sum (20) -最大公约数 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題目如下:

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


題目要求對分數進行處理,題目的關鍵在于求取最大公約數,最初我采用了循環出現超時,后來改用輾轉相除法,解決了此問題。需要注意的是分子為負數的情況,為方便處理,我們把負數取絕對值,并且記錄下符號,最后再輸出。

輾轉相除法如下:

給定數a、b,要求他們的最大公約數,用任意一個除以另一個,得到余數c,如果c=0,則說明除盡,除數就是最大公約數;如果c≠0,則用除數再去除以余數,如此循環下去,直至c=0,則除數就是最大公約數,直接說比較抽象,下面用例子說明。

設a=25,b=10,c為余數

①25/10,c=5≠0,令a=10,b=5。

②10/5,c=0,則b=5就是最大公約數。

求取最大公約數的代碼如下:

long getMaxCommon(long a, long b){long yu;if(a == b) return a;while(1){yu = a % b;if(yu == 0) return b;a = b;b = yu;} }

完整代碼如下:

#include <iostream> #include <stdio.h> #include <vector>using namespace std;struct Ration{long num;long den;Ration(long _n, long _d){num = _n;den = _d;}};long getMaxCommon(long a, long b){long yu;if(a == b) return a;while(1){yu = a % b;if(yu == 0) return b;a = b;b = yu;} }int main(){int N;long num,den;long maxDen = -1;cin >> N;vector<Ration> rations;for(int i = 0; i < N; i++){scanf("%ld/%ld",&num,&den);rations.push_back(Ration(num,den));if(maxDen == -1){maxDen = den;}else{// 找maxDen和當前的最小公倍數if(den == maxDen) continue;else if(maxDen > den){if(maxDen % den == 0) continue;}else{if(den % maxDen == 0){maxDen = den;continue;}}maxDen = maxDen * den;}}num = 0;for(int i = 0; i < N; i++){num += rations[i].num * (maxDen / rations[i].den);}if(num == 0) {printf("0\n");return 0;}bool negative = num < 0;if(negative) num = -num;if(num >= maxDen){long integer = num / maxDen;long numerator = num % maxDen;if(numerator == 0){if(negative)printf("-%ld\n",integer);elseprintf("%ld\n",integer);return 0;}long common = getMaxCommon(numerator,maxDen);if(negative){printf("%ld -%ld/%ld\n",integer,numerator/common,maxDen / common);}else{printf("%ld %ld/%ld\n",integer,numerator/common,maxDen / common);}}else{long common = getMaxCommon(num,maxDen);if(negative)printf("-%ld/%ld\n",num/common,maxDen/common);elseprintf("%ld/%ld\n",num/common,maxDen/common);}return 0; }

轉載于:https://www.cnblogs.com/aiwz/p/6154051.html

總結

以上是生活随笔為你收集整理的1081. Rational Sum (20) -最大公约数的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。