PAT1088:Rational Arithmetic
1088. Rational Arithmetic (20)
For two rational numbers, your task is to implement the basic arithmetics, that is, to calculate their sum, difference, product and quotient.
Input Specification:
Each input file contains one test case, which gives in one line the two rational numbers in the format "a1/b1 a2/b2". The numerators and the denominators are all in the range of long int. If there is a negative sign, it must appear only in front of the numerator. The denominators are guaranteed to be non-zero numbers.
Output Specification:
For each test case, print in 4 lines the sum, difference, product and quotient of the two rational numbers, respectively. The format of each line is "number1 operator number2 = result". Notice that all the rational numbers must be in their simplest form "k a/b", where k is the integer part, and a/b is the simplest fraction part. If the number is negative, it must be included in a pair of parentheses. If the denominator in the division is zero, output "Inf" as the result. It is guaranteed that all the output integers are in the range of long int.
Sample Input 1:
2/3 -4/2
Sample Output 1:
2/3 + (-2) = (-1 1/3)
2/3 - (-2) = 2 2/3
2/3 * (-2) = (-1 1/3)
2/3 / (-2) = (-1/3)
Sample Input 2:
5/3 0/6
Sample Output 2:
1 2/3 + 0 = 1 2/3
1 2/3 - 0 = 1 2/3
1 2/3 * 0 = 0
1 2/3 / 0 = Inf 思路 分数的四则运算,难点主要是在输出数据的格式化(参见代码stringFormat函数)上,比较考验耐心和逻辑,仔细就行。 代码
#include<iostream>
using namespace std;
typedef long long ll; ll a,b,c,d,gcdvalue; ll gcd(ll a,ll b)
{
return b == 0?abs(a):gcd(b,a % b);
} string stringFormat(ll a,ll b)
{
string res = "";
gcdvalue = gcd(a,b);
a /= gcdvalue;
b /= gcdvalue;
if(a * b < 0)
res += "(-";
ll c = abs(a), d = abs(b);
if(c/d == 0)
{
if( c % d == 0)
res += "0";
else
res += to_string(c) + "/" +to_string(d);
}
else
{
res += to_string(c/d);
if(c % d != 0)
{
res += " " + to_string(c % d) + "/" +to_string(d);
}
}
if(a * b < 0)
res += ")";
return res;
} int main()
{
scanf("%lld/%lld %lld/%lld",&a,&b,&c,&d);
ll sum1,sum2,sub1,sub2,mul1,mul2,div1,div2;
//sum
sum1 = a * d + b * c;
sum2 = b * d;
sub1 = a * d - b * c;
sub2 = b * d;
mul1 = a * c;
mul2 = b * d;
div1 = a * d;
div2 = b * c;
string a1 = stringFormat(a,b);
string a2 = stringFormat(c,d);
cout << a1 << " + " << a2 <<" = " << stringFormat(sum1,sum2) << endl;
cout << a1 << " - " << a2 <<" = " << stringFormat(sub1,sub2) << endl;
cout << a1 << " * " << a2 <<" = " << stringFormat(mul1,mul2) << endl;
if(a2 != "0")
cout << a1 << " / " << a2 <<" = " << stringFormat(div1,div2) << endl;
else
cout << a1 << " / " << a2 <<" = " << "Inf" << endl;
}
PAT1088:Rational Arithmetic的更多相关文章
- pat1088. Rational Arithmetic (20)
1088. Rational Arithmetic (20) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue F ...
- PAT 1088 Rational Arithmetic[模拟分数的加减乘除][难]
1088 Rational Arithmetic(20 分) For two rational numbers, your task is to implement the basic arithme ...
- PAT_A1088#Rational Arithmetic
Source: PAT A1088 Rational Arithmetic (20 分) Description: For two rational numbers, your task is to ...
- A1088. Rational Arithmetic
For two rational numbers, your task is to implement the basic arithmetics, that is, to calculate the ...
- 1088 Rational Arithmetic(20 分)
For two rational numbers, your task is to implement the basic arithmetics, that is, to calculate the ...
- PAT Rational Arithmetic (20)
题目描写叙述 For two rational numbers, your task is to implement the basic arithmetics, that is, to calcul ...
- PAT 1088. Rational Arithmetic
For two rational numbers, your task is to implement the basic arithmetics, that is, to calculate the ...
- PAT甲级——A1088 Rational Arithmetic
For two rational numbers, your task is to implement the basic arithmetics, that is, to calculate the ...
- PAT Advanced 1088 Rational Arithmetic (20) [数学问题-分数的四则运算]
题目 For two rational numbers, your task is to implement the basic arithmetics, that is, to calculate ...
随机推荐
- linux测试noatime对文件访问时间的影响
linux测试noatime对文件访问时间的影响 文件(如abc)有3个时间: # stat abc Access: 2015-04-16 19:30:13.665970572 +0800 Modif ...
- Mac OS X 10.8.4下面XZ Utils(*.tar.xz)压缩解压缩命令工具的安装
主要参考:http://bbs.chinaunix.net/thread-3610738-1-1.html 现在很多找到的软件都是tar.xz的格式的,xz 是一个使用 LZMA压缩算法的无损数据压缩 ...
- Picasso 图片加载库
Picasso 英文意思国外一个很有名的画家毕加索的名字,国外项目取名还是很有意思的! 从github新下载的picasso项目有依赖其他第三方开源项目okhttp和okio,这两个项目也是相当经典的 ...
- Boyer-Moore算法
1.概述 在用于查找子字符串的算法当中,BM(Boyer-Moore)算法是目前相当有效又容易理解的一种,一般情况下,比KMP算法快3-5倍. BM算法在移动模式串的时候是从左到右,而进行比较的时候是 ...
- Android TV listView焦点平滑移动
先上TV上效果图 Mark下思路: package com.test.ui; import java.lang.reflect.Method; import android.annotation.Su ...
- javac编译同一个包内的java文件
问题描述:包a.b.c里有d.java e.java f.java三个文件,其中d中包含main. 错误: 第一种:javac d.java 报错:里面用到其他类,找不到 第二种:javac *.ja ...
- obj-c编程13:归档
这篇归档内容的博文也挺有趣的,笨猫对好玩的东西一向感兴趣啊!如果用过ruby就会知道,obj-c里的归档类似于ruby中的序列化概念,不过从语法的简洁度来说,我只能又一次呵呵了. 下面大家将会看到2种 ...
- Java中使用有返回值的线程
在创建多线程程序的时候,我们常实现Runnable接口,Runnable没有返回值,要想获得返回值,Java5提供了一个新的接口Callable,可以获取线程中的返回值,但是获取线程的返回值的时候,需 ...
- 【大前端攻城狮之路】JavaScript函数式编程
转眼之间已入五月,自己毕业也马上有三年了.大学计算机系的同学大多都在北京混迹,大家为了升职加薪,娶媳妇买房,熬夜加班跟上线,出差pk脑残客户.同学聚会时有不少兄弟已经体重飙升,开始关注13号地铁线上铺 ...
- index() checkbox单选问题
index() 只对兄弟节点有用 如果这种结构要选择checkbox 时用prop附加属性 removeAttr清楚属性 $('.checkbox').prop('checked',true) $(' ...