当前位置:网站首页>1081 rational sum (20 points) points add up to total points

1081 rational sum (20 points) points add up to total points

2022-07-06 12:08:00 Python ml

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
long long gcd(long long a,long long b){
    
    return b==0?abs(a):gcd(b,a%b);
}
int main() {
       
    long long n,a,b,suma = 0, sumb = 1, gcdvalue;
    cin>>n;
    for(int i=0;i<n;i++){
               // Add up the scores 
        scanf("%lld/%lld",&a,&b);
        gcdvalue=gcd(a,b);      // After entering, the numerator and denominator will be divided into 
        a=a/gcdvalue;
        b=b/gcdvalue;
        suma=a*sumb+suma*b;     // And the previous cumulative sum suma/sumb Carry out general division and addition 
        sumb=b*sumb;
        gcdvalue=gcd(suma,sumb);   // Then sum it up and divide it all 
        suma=suma/gcdvalue;
        sumb=sumb/gcdvalue;
    }
    long long integer=suma/sumb;         // Output in the form of true fraction 
    suma=suma-integer*sumb;
    if(integer!=0){
    
        cout<<integer;
        if(suma!=0){
    
            cout<<" ";
        }
    } 
    if(suma!=0) cout<<suma<<"/"<<sumb;
    if(suma==0&&integer==0)cout<<0;
    system("pause");
    return 0;
}
原网站

版权声明
本文为[Python ml]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202131602327266.html