首页 > 其他分享 >PAT_A1081 Rational Sum

PAT_A1081 Rational Sum

时间:2023-10-29 11:11:56浏览次数:38  
标签:ll PAT A1081 numerator long Sample integer Sum lld

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<bits/stdc++.h>
using namespace std;
typedef long long ll;
struct node{
	ll a, b;
};
int gcd(int a, int b){
	return b ? gcd(b, a%b):a;
}
node myadd(node a, node b){
	ll q = a.a*b.b + b.a*a.b;
	ll w = a.b * b.b;
	ll d = gcd(q, w);
	d = d<0 ? -d : d;
	a.a = q/d;
	a.b = w/d;
	return a;
}
node r, t;
int main(){
	r.a=0; r.b=1;
	ll n; scanf("%lld", &n);
	for(ll i = 0; i < n; i++){
		ll a,b,d;
		scanf("%lld/%lld", &a, &b);
		d = gcd(a, b);
		d = d<0 ? -d : d;
		t.a = a/d;
		t.b = b/d;
		r = myadd(r, t);
	}
	if(abs(r.a) >= r.b)
		r.a%r.b==0 ? printf("%lld", r.a/r.b) : printf("%lld %lld/%lld", r.a/r.b, r.a%r.b, r.b);
	else
		r.a == 0 ? printf("0") : printf("%lld/%lld", r.a, r.b);
	return 0;
}

总结
1、 #分数 本题主要考察分数的运算,和分数的输出
2、数据范围为int,但两分母相乘时,最大可达到long long,应该用long long
3、测试点4会检查0的输出
4、[[分数及其运算模板]]

标签:ll,PAT,A1081,numerator,long,Sample,integer,Sum,lld
From: https://www.cnblogs.com/Afinis/p/17795612.html

相关文章

  • PAT 甲级【1015 Reversible Primes】
    考察素数判断考察进制转换importjava.io.IOException;importjava.io.InputStreamReader;importjava.io.StreamTokenizer;publicclassMain{@SuppressWarnings("uncheck")publicstaticvoidmain(String[]args)throwsIOException{StreamTok......
  • PAT甲级【1014 Waiting in Line】
    考察双向链表importjava.io.IOException;importjava.io.InputStreamReader;importjava.io.StreamTokenizer;importjava.util.LinkedList;publicclassMain{@SuppressWarnings("uncheck")publicstaticvoidmain(String[]args)throwsIOExcepti......
  • PAT 甲级【1013 Battle Over Cities】
    本题就是dfs.连通图个数-2;但是java慢,最后一个case超时importjava.io.*;importjava.util.HashSet;importjava.util.Set;publicclassMain{@SuppressWarnings("uncheck")publicstaticvoidmain(String[]args)throwsIOException{StreamToken......
  • PAT_B1008 数组元素循环右移问题
    一个数组A中存有N(>0)个整数,在不允许使用另外数组的前提下,将每个整数循环向右移M(≥0)个位置,即将A中的数据由(A0​A1​⋯AN−1​)变换为(AN−M​⋯AN−1​A0​A1​⋯AN−M−1​)(最后M个数循环移至最前面的M个位置)。如果需要考虑程序移动数据的次数尽量少,要如何设计移动的方法?输入格式:每......
  • 无涯教程-Clojure - re-pattern函数
    re-pattern返回java.util.regex.Pattern的实例。然后将其用于其他模式匹配方法。re-pattern-语法(repatternpat)参数   - "pat"是需要形成的pattern。返回值 - 类型为java.util.regex.Pattern的模式对象。re-pattern-示例(nsclojure.examples.example......
  • [Spring框架学习]SSM 整合,使用maven构建项目的时候,启动项目报错class path resource
    错误:classpathresource[config/spring/springmvc.xml]cannotbeopenedbecauseitdoesnotexist错误原因:找不到我的springmvc.xml,在下面web.xml中是我引用路径,网上找到问题classpath指向路径不是resource路径,所以一直找不到我的xml文件,classpath:到你的class路径......
  • PAT_A1049 Counting Ones【困难】
    Thetaskissimple:givenanypositiveinteger N,youaresupposedtocountthetotalnumberof1'sinthedecimalformoftheintegersfrom1to N.Forexample,given N being12,therearefive1'sin1,10,11,and12.InputSpecification:Eac......
  • PAT_A1104 Sum of Number Segments
    Givenasequenceofpositivenumbers,asegmentisdefinedtobeaconsecutivesubsequence.Forexample,giventhesequence{0.1,0.2,0.3,0.4},wehave10segments:(0.1)(0.1,0.2)(0.1,0.2,0.3)(0.1,0.2,0.3,0.4)(0.2)(0.2,0.3)(0.2,0.3,0.4)......
  • PAT 甲级【1012 The Best Rank】
    本题用java极容易超时,提交了好几次才成功另外9088777750,名次应该是12335,不是12334 importjava.io.*;publicclassMain{@SuppressWarnings("unchecked")publicstaticvoidmain(String[]args)throwsIOException{StreamTokenizer......
  • 003Square(n)Sum(8kyu)from codewars
    Square(n)SumCompletethesquaresumfunctionsothatitsquareseachnumberpassedintoitandthensumstheresultstogether.完成平方和函数,对每个传入其中的数字平方并相加。defsquare_sum(numbers):sums=0foriinnumbers:sums+=i*iretu......