pat 1088 Rational Number

2024-02-11 16:38
文章标签 number pat rational 1088

本文主要是介绍pat 1088 Rational Number,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1088. Rational Arithmetic (20)

时间限制
200 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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

#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
using namespace std;char one[30], two[30];
long long fenzi[3], fenmu[3];
int flag[3];long long swap(long long &a, long long &b)
{long long tmp = b;b = a;a = tmp;
}
long long gcd(long long a, long long b)
{if(a < b){long long tmp = b;b = a;a = tmp;}while(b != 0){long long c = a%b;a = b;b = c;}return a;
}void getNum(int index, char str[])
{int i;int len = strlen(str);for(i = 0; str[i] != '/'; i++){if(str[i] >= '0' && str[i] <= '9')fenzi[index] = fenzi[index] * 10 + str[i] - '0';}fenzi[index] = fenzi[index]*flag[index];i++;for(; i < len; i++){if(str[i] >= '0' && str[i] <= '9')fenmu[index] = fenmu[index]*10+str[i]-'0';}	
}void outPut(long long a, long long b)
{int tmp = 0;if(a == 0){printf("0");return ;}long long yueshu = gcd(abs(a), abs(b));a = a / yueshu;b = b / yueshu;if(b < 0)a = -a, b = -b;if(a < 0){printf("(-");a = -a;if(a / b > 0)printf("%lld",a/b), tmp = 1;if(a%b == 0)printf(")");else{if(tmp == 1)printf(" %lld/%lld)", a%b, b);elseprintf("%lld/%lld)", a%b, b);}} else {if(a / b > 0)printf("%lld", a/b), tmp = 1;if(a%b != 0){if(tmp == 1)printf(" %lld/%lld", a%b, b);elseprintf("%lld/%lld", a%b, b);}}
}
int main()
{while(scanf("%s %s", one, two) != EOF){memset(fenzi, 0, sizeof(fenzi));memset(fenmu, 0, sizeof(fenmu));flag[1] = 1, flag[2] = 1;int i, j, len1, len2;if(one[0] == '-')flag[1] = -1;if(two[0] == '-')flag[2] = -1;getNum(1, one);getNum(2, two);outPut(fenzi[1], fenmu[1]);printf(" + ");outPut(fenzi[2], fenmu[2]);printf(" = ");outPut(fenzi[1]*fenmu[2]+fenzi[2]*fenmu[1], fenmu[1]*fenmu[2]);printf("\n");outPut(fenzi[1], fenmu[1]);printf(" - ");outPut(fenzi[2], fenmu[2]);printf(" = ");outPut(fenzi[1]*fenmu[2]-fenzi[2]*fenmu[1], fenmu[1]*fenmu[2]);printf("\n");outPut(fenzi[1], fenmu[1]);printf(" * ");outPut(fenzi[2], fenmu[2]);printf(" = ");outPut(fenzi[1]*fenzi[2], fenmu[1]*fenmu[2]);printf("\n");outPut(fenzi[1], fenmu[1]);printf(" / ");outPut(fenzi[2], fenmu[2]);printf(" = ");if(fenmu[1]*fenzi[2] != 0)outPut(fenzi[1]*fenmu[2], fenmu[1]*fenzi[2]);elseprintf("Inf");printf("\n");}return 0;
}


这篇关于pat 1088 Rational Number的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/700289

相关文章

usaco 1.2 Name That Number(数字字母转化)

巧妙的利用code[b[0]-'A'] 将字符ABC...Z转换为数字 需要注意的是重新开一个数组 c [ ] 存储字符串 应人为的在末尾附上 ‘ \ 0 ’ 详见代码: /*ID: who jayLANG: C++TASK: namenum*/#include<stdio.h>#include<string.h>int main(){FILE *fin = fopen (

题目1380:lucky number

题目1380:lucky number 时间限制:3 秒 内存限制:3 兆 特殊判题:否 提交:2839 解决:300 题目描述: 每个人有自己的lucky number,小A也一样。不过他的lucky number定义不一样。他认为一个序列中某些数出现的次数为n的话,都是他的lucky number。但是,现在这个序列很大,他无法快速找到所有lucky number。既然

Jenkins 通过 Version Number Plugin 自动生成和管理构建的版本号

步骤 1:安装 Version Number Plugin 登录 Jenkins 的管理界面。进入 “Manage Jenkins” -> “Manage Plugins”。在 “Available” 选项卡中搜索 “Version Number Plugin”。选中并安装插件,完成后可能需要重启 Jenkins。 步骤 2:配置版本号生成 打开项目配置页面。在下方找到 “Build Env

滑雪 POJ 1088

回溯 + DFS #include<cstdio>#include<cstring>#include<iostream>using namespace std;#define MAXD 100 + 10int R,C;int G[MAXD][MAXD];int d[MAXD][MAXD] = {0}; /*到达 i j 时候的最大长度*/int max_size = 0;#

【Hdu】Minimum Inversion Number(逆序,线段树)

利用线段树在nlogn的时间复杂度内求一段数的逆序。 由于给的序列是由0 ~ n -1组成的,求出初始的逆序之后可以递推出移动之后的逆序数。 #include<cstdio>#include<iostream>#include<cstring>#include<algorithm>using namespace std;typedef long long LL;const in

【JavaScript】基本数据类型与引用数据类型区别(及为什么String、Boolean、Number基本数据类型会有属性和方法?)

基本数据类型   JavaScript基本数据类型包括:undefined、null、number、boolean、string。基本数据类型是按值访问的,就是说我们可以操作保存在变量中的实际的值。 1)基本数据类型的值是不可变的 任何方法都无法改变一个基本类型的值,比如一个字符串: var name = "change";name.substr();//hangconsole.log

PAT甲级-1044 Shopping in Mars

题目   题目大意 一串项链上有n个钻石,输入给出每个钻石的价格。用m元买一个连续的项链子串(子串长度可为1),如果不能恰好花掉m元,就要找到最小的大于m的子串,如果有重复就输出多个,按递增顺序输出子串的前端和后端索引。 原来的思路 取连续的子串使和恰等于m,没有恰等于就找最小的大于。可以将子串依次累加,使得每个位置都是起始位置到该位置的序列和,整个数组显递增顺序,就可以用右边减左边

PAT (Advanced Level) Practice——1011,1012

1011:  链接: 1011 World Cup Betting - PAT (Advanced Level) Practice (pintia.cn) 题意及解题思路: 简单来说就是给你3行数字,每一行都是按照W,T,L的顺序给出相应的赔率。我们需要找到每一行的W,T,L当中最大的一个数,累乘的结果再乘以0.65,按照例子写出表达式即可。 同时还需要记录每一次选择的是W,T还是L

ORA-24067: exceeded maximum number of subscribers for queue ADMIN.SMS_MT_QUEUE

临时处理办法: delete from aq$_ss_MT_tab_D;delete from aq$_ss_MT_tab_g;delete from aq$_ss_MT_tab_h;delete from aq$_ss_MT_tab_i;delete from aq$_ss_MT_tab_p;delete from aq$_ss_MT_tab_s;delete from aq$

SQLSERVER排名函数RANK,DENSE_RANK,NTILE,ROW_NUMBER

SQL SERVER排名函数RANK,DENSE_RANK,NTILE,ROW_NUMBER 前言 本文意于用实例数据帮助理解SQL SERVER排名函数RANK,DENSE_RANK,NTILE,ROW_NUMBER。 准备工作 创建测试表:   ? 1 2 3 4 5 create table test( id int identity(1,1)