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
(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

题目大意:给出几个分数求和。

//又是分数求和的题目,细节会很多啊。

#include <iostream>
#include <map>
using namespace std; int n;
int nums[]; int getSum(int s,int f){
if(f>=n){//如果已经到了尽头,那么就对s约分并且输出
//还是需要先根据分母找出所有的最小公倍数呢?
//一点也不会。写不下去。
}
} int main() { cin>>n;
for(int i=;i<n;i++){
cin>>nums[i];
}
cout<<getSum(,); return ;
}

代码转自:https://www.liuchuo.net/archives/2108

#include <iostream>
#include <cstdlib>
using namespace std;
long long gcd(long long a, long long b) {return b == ? abs(a) : gcd(b, a % b);}
int main() {
long long n, a, b, suma = , sumb = , gcdvalue;//注意数据类型的定义。
scanf("%lld", &n);
for(int i = ; i < n; i++) {
scanf("%lld/%lld", &a, &b);
gcdvalue = gcd(a, b);//找到最大公约数进行约分。
a = a / gcdvalue;
b = b / gcdvalue;
suma = a * sumb + suma * b;//分子
sumb = b * sumb;//计算分母
gcdvalue = gcd(suma, sumb);
sumb = sumb / gcdvalue;//再约分。
suma = suma / gcdvalue;
}
long long integer = suma / sumb;
suma = suma - (sumb * integer);//计算余下的分子。
if(integer != ) {
printf("%lld", integer);
if(suma != ) printf(" ");//如果余下的分子不为0.
}
if(suma != )
printf("%lld/%lld", suma, sumb);
if(integer == && suma == )
printf("");
return ;
}

//学习了,需要经常复习吧,要不还是不会。

PAT 1081 Rational Sum[分子求和][比较]的更多相关文章

  1. PAT 1081 Rational Sum

    1081 Rational Sum (20 分)   Given N rational numbers in the form numerator/denominator, you are suppo ...

  2. PAT Advanced 1081 Rational Sum (20) [数学问题-分数的四则运算]

    题目 Given N rational numbers in the form "numerator/denominator", you are supposed to calcu ...

  3. PAT甲题题解-1081. Rational Sum (20)-模拟分数计算

    模拟计算一些分数的和,结果以带分数的形式输出注意一些细节即可 #include <iostream> #include <cstdio> #include <algori ...

  4. PAT 甲级 1081 Rational Sum (数据不严谨 点名批评)

    https://pintia.cn/problem-sets/994805342720868352/problems/994805386161274880 Given N rational numbe ...

  5. 【PAT甲级】1081 Rational Sum (20 分)

    题意: 输入一个正整数N(<=100),接着输入N个由两个整数和一个/组成的分数.输出N个分数的和. AAAAAccepted code: #define HAVE_STRUCT_TIMESPE ...

  6. PAT (Advanced Level) 1081. Rational Sum (20)

    简单模拟题. #include<cstdio> #include<cstring> #include<cmath> #include<vector> # ...

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

    题目如下: Given N rational numbers in the form "numerator/denominator", you are supposed to ca ...

  8. 1081. Rational Sum (20)

    the problem is from PAT,which website is http://pat.zju.edu.cn/contests/pat-a-practise/1081 the code ...

  9. 1081 Rational Sum(20 分)

    Given N rational numbers in the form numerator/denominator, you are supposed to calculate their sum. ...

随机推荐

  1. IPC之SystemV

    svipc - System V interprocess communication mechanisms linux实现的System V interprocess communication ( ...

  2. C++读取Sql Server

    代码如下: // ReadSqlConsole.cpp: 主项目文件. #include "stdafx.h" #include <iostream> #include ...

  3. java模拟http请求上传文件,基于Apache的httpclient

    1.依赖 模拟http端的请求需要依赖Apache的httpclient,需要第三方JSON支持,项目中添加 <dependency> <groupId>org.apache& ...

  4. (转载)【C#4.0】dynamic和var及object

    dynamic a = 10;a = a + 10;Console.WriteLine(a.GetType()); 此段代码会输出 System.Int32,第二行不需要类型转换,因为在运行时识别类型 ...

  5. 时间戳(Unix时间)

    /// <summary> /// 时间戳与DateTime互转 /// </summary> public class UnixOfTimeHelper { /// < ...

  6. bootstrap基础学习九篇

    现在学学bootstrap响应式实用工具 Bootstrap 提供了一些辅助类,以便更快地实现对移动设备友好的开发.这些可以通过媒体查询结合大型.小型和中型设备,实现内容对设备的显示和隐藏. 需要谨慎 ...

  7. K-Means算法Demo

    简介:本Demo是参照这个网站上的Demo自己用Java实现的.将Java打包为Jar,再将Jar转为exe,源代码及程序Demo下载请点我. K-Means算法简介 我尽量用通俗易懂但不规范的语言来 ...

  8. 【微信小游戏】文件系统,远程加载资源打破4M限制

    一.前提 微信小游戏,对游戏包体的大小有严格是限制,上传文件大小<4M,但是本地缓存文件有50M空间,也就是说我们可以将一些资源放到网上,然后缓存到本地. 二.官方概念 文件系统 文件系统是小程 ...

  9. PHP和JS判断手机还是电脑访问

    当用户使用手机等移动终端访问网站时,我们可以通过程序检测用户终端类型,如果是手机用户,则引导用户访问适配手机屏幕的移动站点.本文将介绍分别使用PHP和JAVASCRIPT代码判断用户终端类型. PHP ...

  10. 面试10大算法汇总——Java篇

    问题导读 1 字符串和数组 2 链表 3 树 4 图 5 排序 6 递归 vs 迭代 7 动态规划 8 位操作 9 概率问题 10 排列组合 11 其他 -- 寻找规律 英文版 以下从Java角度解释 ...