1081 Rational Sum(24行代码+超详细注释)

news/2024/10/18 16:55:19/

分数 20

全屏浏览题目

切换布局

作者 CHEN, Yue

单位 浙江大学

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

代码长度限制

16 KB

时间限制

400 ms

内存限制

64 MB

防溢出:多约分就完事了!!!

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll gcd(ll a,ll b){//求公因数,常用模板 
    return b==0?a:gcd(b,a%b);
}
int main(){
    #ifdef ONLINE_JUDGE
    #else
         freopen("1.txt","r",stdin);
    #endif
    int n;
    cin>>n;
    ll a=0,b=1;
    for(int i=0;i<n;i++){
        ll c,d;
        scanf("%lld/%lld",&c,&d);
        int t=gcd(b,d);//求分母的公因数 
        a=(a*d+b*c)/t,b=b*d/t;//约分 
        t=gcd(a,b);//求分子和分母的公因数 
        a/=t,b/=t;//约分 
    }
    if(b==1)cout<<a<<endl;//若是整数 
    else{//若不是整数 
        if(a>b)printf("%d",a/b);//若是假分式 
        printf("%lld/%lld\n",a,b);//输出剩下真分式 
    }
}


http://www.ppmy.cn/news/107219.html

相关文章

更改测试用例执行顺序的几种自动化方法

前言 在自动化测试中&#xff0c;自动化测试用例设计原则就是执行过程时不能存在依赖顺序&#xff0c;那么如果测试用例需要按照指定顺序执行&#xff0c;这个时候应该怎么做呢&#xff1f;目前单元测试框架中unittest没有办法改变测试用例的执行顺序&#xff0c;但是另一个单…

jupyter notebbok加入conda的虚拟环境

你可以通过以下步骤将conda环境添加到jupyter notebook中&#xff1a; 打开Anaconda Prompt或终端&#xff0c;激活你的conda环境。 安装ipykernel包&#xff0c;该包可以将conda环境添加到jupyter notebook中。 conda install ipykernel将conda环境添加到jupyter notebook中…

大型测试(系统测试)和它的问题(片状测试)

大型测试(系统测试)和它的问题(片状测试) 更大的测试 即使有模拟&#xff0c;单元测试也不能满足我们所有的需求。 为了检查我们的代码与其他代码的行为——或者我们的代码需要使用的外部服务&#xff0c;集成测试是必要的。然而&#xff0c;为了测试整个软件&#xff0c;我们…

模版的分离编译 解决方案

回顾 对于模版&#xff0c;在之前我们就已经讲过&#xff0c;模版不支持分离编译&#xff08;即声明定义不在同一文件中&#xff09;。 类中&#xff0c;我们知道&#xff0c;对于代码量比较小的函数&#xff0c;会默认识别成内联函数&#xff0c;增加代码运行的效率&#xf…

利用matlab对滤波器频率特性分析

【设计目标】对双二阶环路滤波器进行时频域分析和处理的基本方法 【设计工具】MATLAB【设计要求】 1)分析典型的双二阶环路滤波器电路:低通、高通、带通、带阻 2)理论分析各滤波电路的系统函数 3)利用Matlab分析各滤波电路的系统函数的频率特性(幅频、相频)、零极点分…

大数据存储复习

插入数据 insert into classname values(2,“大数据二班”); 建表语句 create table order_his( create_date string, order_id string, order_status string, dw_begin_date string, dw_end_date string )ROW FORMAT DELIMITED FIELDS TERMINATED BY ‘,’; 加载数据到Hiv…

手把手教你做独立t检验

一、案例介绍 为研究国产四类新药阿卡波糖胶囊的降血糖效果&#xff0c;某医院用40名2型糖尿病患者进行同期随机对照试验。研究者将这些患者随机等分到试验组&#xff08;用阿卡波糖胶囊&#xff09;和对照组&#xff08;用拜唐苹胶囊&#xff09;&#xff0c;分别测得试验开始…

个人网站可以申请什么样的https证书?

https证书即SSL证书&#xff0c;网站安装SSL证书可将http协议升级为https加密协议&#xff0c;可保障网站数据信息安全。随着SSL证书技术不断的发展&#xff0c;SSL证书的作用不仅仅在于数据加密&#xff0c;还有利于seo关键词优化&#xff0c;帮助网站进行排名&#xff0c;这也…