pat 甲级 1081 Rational Sum

复习最大公因数和最小公倍数写法
特殊点0的地方要考虑
最开始是想读进字符串里,然后再转数字
然后getline出来问题了??没明白为什么getline(cin,a);不行
还是用了scanf

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <vector>
#include <cstring>

using namespace std;

int n;

int gcd(long long a, long long b){
    return b==0 ? a : gcd(b,a%b);
}

int icm(long long a, long long b){
    return a*b/gcd(a,b);
}

vector<int> up, down;


int main(){

    scanf("%d",&n);
    long long a1 = 0, b1 = 1;
    for(int i=0; i<n; i++){
        long long a,b;
        scanf("%lld / %lld",&a,&b);
        if(a == 0) continue;
        a1 = a1 * b + a * b1; 
        b1 = b * b1; 
        int c = gcd(a1,b1);
        a1 = a1 / c;
        b1 = b1 / c;
        //cout << c << " c "<< a1 << " eee " << b1 << endl;
    }
    //if(a1 == 0 && b1 == 0 )
    long long h = a1 / b1;
    a1 = a1 - b1 * h;
    if(h != 0) {
        printf("%lld",h);
        if(a1 != 0) printf(" ");
    }
    if(a1 != 0) printf("%lld/%lld",a1,b1);
    if(h == 0 && a1 == 0) printf("0");


    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值