1081 Rational Sum (20分)【分数的四则运算】

news/2024/7/4 1:44:11

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

 解题思路:

这道题会给我们n个分数,要求我们求n个分数的和,注意分数前面可能会有负号。

这是一道模拟分数加法的题目,我们只需要注意分数的计算公式是:

result = \frac{f1.up*f2.down+f2.up*f1.down}{f1.down*f2.down}

然后就是注意每次计算后要注意约分!

#include<iostream>
#include<cstdio>
#include<math.h>
using namespace std;

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

int main()
{
	int n;
	cin >> n;
	int up, down;
	scanf("%d/%d", &up, &down);
	int tmp = gcd(up, down);
	up = up / tmp;
	down = down / tmp;
	for (int i = 1; i < n; i++)
	{
		int up_tmp, down_tmp;
		scanf("%d/%d", &up_tmp, &down_tmp);
		up = up*down_tmp + down*up_tmp;       //计算分子
		down = down*down_tmp;                         //计算分母
		tmp = gcd(down, up);
		up = up / tmp;                                           //分子分母约分
		down = down / tmp;
	}
	if (up%down == 0)                                        //如果是整数
		cout << up / down << endl;
	else                                                                //如果是分数
	{ 
		if (abs(up) > abs(down))                           //如果是假分数
		{
			if (up > 0)
				cout << up / down << " " << up - up / down*down << "/" << down << endl;
			else
			{
				up = -up;
				cout << "-" << up / down << " " << up - up / down*down << "/" << down << endl;
			}
		}
		else
			cout << up << "/" << down << endl;
	}
	return 0;
}

 


http://www.niftyadmin.cn/n/4610387.html

相关文章

记一次错误卸载软件包导致Linux系统崩溃的修复解决过程

首先问题产生的缘由很简单&#xff0c;是我一同事在安装oracle一套软件时&#xff0c;按照要求需要binutils软件包的32位版本&#xff0c;然而在Oracle Linux已经装有64位&#xff0c;按理说是可以安装i686的&#xff0c;我猜应该是32位的版本低于这个已有的64位所以导致冲突而…

个人作业—找水王

题目&#xff1a;三人行设计了一个灌水论坛。信息学院的学生都喜欢在上面交流灌水&#xff0c;传说在论坛上有一个“水王”&#xff0c;他不但喜欢发帖&#xff0c;还会回复其他ID发的每个帖子。坊间风闻该“水王”发帖数目超过了帖子数目的一半。如果你有一张当前论坛的帖子&a…

1059 Prime Factors (25分)【质因数分解】

1059 Prime Factors (25分) Given any positive integer N, you are supposed to find all of its prime factors, and write them in the format N p​1​​​k​1​​​​p​2​​​k​2​​​​⋯p​m​​​k​m​​​​. Input Specification: Each input file contain…

POJO百度百科

POJO(Plain Ordinary Java Object)简单的Java对象&#xff0c;实际就是普通JavaBeans&#xff0c;是为了避免和EJB混淆所创造的简称。 使用POJO名称是为了避免和EJB混淆起来, 而且简称比较直接. 其中有一些属性及其getter setter方法的类,没有业务逻辑&#xff0c;有时可以作为…

找出一个只出现一次的字符

一&#xff0c;问题描述 给定一个字符串&#xff0c;找出一个 其中只出现一次的字符 如"abaccdeff" 只出现一次的字符有 b d e 二&#xff0c;问题分析 ①字符集是个常量 &#xff0c;字符只有那么多。比如ASCII 一共256个&#xff0c;比如 字母表一共只有26个…

ios 开源代码

1、开源代码 http://www.oschina.net/iOS/codingList/365/ios-buttonhttp://www.devdiv.com/iOS_iPhone-iOS6%E6%96%B0%E7%89%B9%E5%BE%81%EF%BC%9A%E5%8F%82%E8%80%83%E8%B5%84%E6%96%99%E5%92%8C%E7%A4%BA%E4%BE%8B%E6%B1%87%E6%80%BB-thread-127965-1-1.htmlhttp://code4app…

Log4J配置文件详解(转载一)

2019独角兽企业重金招聘Python工程师标准>>> 网站要发布了&#xff0c;为了跟踪一些日志&#xff0c;需要用到log4j&#xff0c;于是就研究了一下log4j的配置 先贴自己用的一个配置源文件 log4j.properties log4j.rootLoggerDEBUG, CONSOLE, FILE ## for console …

1063 Set Similarity (25分)【set】

1063 Set Similarity (25分) Given two sets of integers, the similarity of the sets is defined to be N​c​​/N​t​​100%, where N​c​​ is the number of distinct common numbers shared by the two sets, and N​t​​ is the total number of distinct numbers …