1088 Rational Arithmetic (20 point(s))

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 i 阅读详情

1088 Rational Arithmetic (20 point(s))

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<stdio.h> 
#define ll long long
ll gcd(ll a,ll b){
	if(b==0) return a;
	else return gcd(b,a%b);
} 
void output(ll a,ll b){//a 分子 b分母 
	if(a==0){
		printf("0");return;
	} 
	bool isNeg = false;
	if(a>0&&b<0){
		isNeg = true; b = -b;
	}else if(a<0&&b>0){
		isNeg = true; a = -a;
	}else if(a<0&&b<0){
		a=-a;b=-b;
	}
	ll f = gcd(a,b);
	a = a/f; b = b/f;
	ll i = a / b;
	a = a - i * b;
	if(isNeg){
		printf("(-");
		if(i!=0){
			printf("%lld",i);
		}
		if(a!=0){
			if(i!=0) printf(" ");
			printf("%lld/%lld",a,b);
		}
		printf(")");
	}else{
		if(i!=0){
			printf("%lld",i);
		}
		if(a!=0){
			if(i!=0) printf(" ");
			printf("%lld/%lld",a,b);
		}		
	}
}
void sum(ll a1,ll b1,ll a2,ll b2){
	ll b = b1*b2;
	ll a = a1*b2+a2*b1;
	output(a1,b1);printf(" + ");
	output(a2,b2);printf(" = ");
	output(a,b);printf("\n");
}
void difference(ll a1,ll b1,ll a2,ll b2){
	ll b = b1*b2;
	ll a = a1*b2-a2*b1;
	output(a1,b1);printf(" - ");
	output(a2,b2);printf(" = ");
	output(a,b);printf("\n");
}
void product(ll a1,ll b1,ll a2,ll b2){
	ll b = b1*b2;
	ll a = a1*a2;
	output(a1,b1);printf(" * ");
	output(a2,b2);printf(" = ");
	output(a,b);printf("\n");
}
void quotient(ll a1,ll b1,ll a2,ll b2){
	output(a1,b1);printf(" / ");
	output(a2,b2);printf(" = ");
	if(a2==0){
		printf("Inf\n");
	}
	else{
		ll b = b1*a2;
		ll a = a1*b2;
		output(a,b);printf("\n");
	}
} 
int main(void){
	ll a1,a2,b1,b2;
	scanf("%lld/%lld",&a1,&b1);
	scanf("%lld/%lld",&a2,&b2);
	sum(a1,b1,a2,b2);
	difference(a1,b1,a2,b2);
	product(a1,b1,a2,b2);
	quotient(a1,b1,a2,b2);
	return 0;
}

 

【信息科学与工程学】【数据科学】数据科学领域——第三篇 数学08 几何学00总篇 编号英文名称中文名称简要说明/归属1Foundations of Geometry几何基础公理化方法, Hilbert《几何基础》2Euclidean Geometry (Synthetic)欧几里得几何(综合法)《几何原本》传统3Analytic Geometry解析几何坐标方法, Descartes, Fermat4Plane Geometry平面几何三角形、圆、共点共线等经典问题5Solid Geometry立体几何三维空间中的图形与度量6Non-Euclidean Geometry非欧几何平行公设不 阅读详情

相关推荐

政府科技管理者如何提升科技成果转化项目进场交易的备案效率?.docx

政府科技管理者如何提升科技成果转化项目进场交易的备案效率?

1088 Rational Arithmetic (20point(s)) - C语言 PAT 甲级

1. 先约分,后输出 2. 约分过程需要找最大公约数

有三只土豆 226

山区道路边坡落石隐患检测数据集VOC+YOLO格式241张1类别.md

【重要提示】本页面仅为资源介绍,不含外部下载地址 亲爱的 优快云 用户: 感谢你浏览本资源页面,在这里提前向大家说明相关情况,避免后续产生误解。本资源我设置的初衷是 0 积分免费分享文档介绍,希望可以无偿给到有需要的朋友。但 优快云 平台存在自动调控机制,系统会根据资源下载热度、文件体积、平台规则等因素,自动修改资源所需下载积分,页面显示为 1 积分、2 积分或更高积分都属于平台自动调整结果,并非我手动修改设置,我没有权限自主锁定积分数值。 如果你打开页面后,发现下载所需积分不为 0,请务必谨慎考虑是否下载。一旦你支付积分完成下载,后续遇到资源内容和描述不符、文件损坏等问题,我无法承担对应的积分损失,也没有渠道为你办理积分退还。在此强烈建议,只有页面明确显示 0 积分的时候,再进行下载操作。 同时特别说明:这份资源介绍文案里,没有附带任何外部下载地址。资源本身是通过 优快云 官方上传通道上传的附件,正常情况下直接点击页面自带的下载按钮即可获取文件。页面找不到外部下载链接属于正常情况,并非资源缺失。 如果因为平台积分显示异常,导致你被扣取积分,积分退款、积分异常相关问题,请直接联系 优快云 官方客服咨询处理。平台规则由官方制定,我无法干预积分变动,望各位理解。

1088 Rational Arithmetic (20 point(s))

#include&lt;iostream&gt; #include&lt;cstdio&gt; using namespace std; typedef long long ll; ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a % b); } void show(ll a, ll b) { if(b &lt; 0) { a = -...

徐伯莱 230

高空作业栏杆检测数据集VOC+YOLO格式230张3类别.md

【重要提示】本页面仅为资源介绍,不含外部下载地址 亲爱的 优快云 用户: 感谢你浏览本资源页面,在这里提前向大家说明相关情况,避免后续产生误解。本资源我设置的初衷是 0 积分免费分享文档介绍,希望可以无偿给到有需要的朋友。但 优快云 平台存在自动调控机制,系统会根据资源下载热度、文件体积、平台规则等因素,自动修改资源所需下载积分,页面显示为 1 积分、2 积分或更高积分都属于平台自动调整结果,并非我手动修改设置,我没有权限自主锁定积分数值。 如果你打开页面后,发现下载所需积分不为 0,请务必谨慎考虑是否下载。一旦你支付积分完成下载,后续遇到资源内容和描述不符、文件损坏等问题,我无法承担对应的积分损失,也没有渠道为你办理积分退还。在此强烈建议,只有页面明确显示 0 积分的时候,再进行下载操作。 同时特别说明:这份资源介绍文案里,没有附带任何外部下载地址。资源本身是通过 优快云 官方上传通道上传的附件,正常情况下直接点击页面自带的下载按钮即可获取文件。页面找不到外部下载链接属于正常情况,并非资源缺失。 如果因为平台积分显示异常,导致你被扣取积分,积分退款、积分异常相关问题,请直接联系 优快云 官方客服咨询处理。平台规则由官方制定,我无法干预积分变动,望各位理解。

ti-cgt-c2000-22.6.0.LTS-windows-x64-installer

ti-cgt-c2000-22.6.0.LTS-windows-x64-installer

PAT(A)1088 Rational Arithmetic (20point(s))

Sample Input 2/3 -4/2 Sample Output 2/3 + (-2) = (-1 1/3) 2/3 - (-2) = 2 2/3 2/3 * (-2) = (-1 1/3) 2/3 / (-2) = (-1/3) 思路: 模拟就行,情况比较多,写一个函数会比较好。 代码 #include <bits/stdc++.h> #include <cst...

Sqssq的博客 224

深入解析CAN2.0A与CAN2.0B协议:帧格式与通信机制对比

本文深入对比了CAN2.0A与CAN2.0B协议的核心差异,重点解析了11位标准帧与29位扩展帧的帧格式、标识符长度及通信机制。文章阐述了CAN2.0B向下兼容的特性,并探讨了在汽车电子、工业控制等复杂网络中,如何根据项目需求、兼容性及实时性要求,在标准帧与扩展帧之间做出合理选择。

weixin_29163797的博客 395

A1088 Rational Arithmetic (20 point(s))/B1034 有理数四则运算 (20 point(s)) 笔记

#include<stdio.h> #include<iostream> using namespace std; long long gcd(long long a, long long b) { if (b == 0) return a; else return gcd(b, a%b); } class Fraction { public: long long...

fire_for_you的博客 194

FPGA进位链(Carry Chain)实战:从Xilinx CARRY8原语到性能优化全解析

本文深入解析了FPGA进位链(Carry Chain)的原理与应用,重点剖析了Xilinx CARRY8原语的端口功能与内部机制。通过实测数据揭示了进位链的真实延迟特性与温度影响,并详细阐述了如何利用DI和S信号进行动态路径优化、实现减法运算与条件进位选择,从而有效提升设计性能。最后,文章探讨了系统级的流水线拆分、布局约束等实战优化策略与常见陷阱。

weixin_29313817的博客 454

MATLAB控制信号发生器(USB-GPIB-HS)GPIB仪器控制教程(含matlab代码)

自动化测试时需要自动产生信号,通过matlab控制信号发生器的频率与功率,以实现自动化信号生成输出

qq_43686057的博客 1606

甲级PAT 1088 Rational Arithmetic (20 分)(分数四则运算)

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: Ea...

Joyceyang_999的博客 371

pandas,构造MultiIndex的四种方法:from_tuples、from_arrays、from_product、from_frame

1、from_tuples:通过 “元组对” 构造MultiIndex """ 一对元组,为一个索引 """ tuples = [(1, 'red'), (1, 'blue'), (2, 'red'), (2, 'blue')] pd.MultiIndex.from_tuples(tuples, names=('number', 'color')) 2、from_arrays:通过 “列表元素的组合” 构造MultiIndex """ 列中表的元素,按位置 对应组合,形成索引 """ arrays

jieru_liu的博客 5388

1088 Rational Arithmetic (20point(s))

1088 Rational Arithmetic (20point(s)) 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

佳迪斯的博客 115

【图形学】Loop细分算法及半边结构实现(C++)

文章目录Loop 细分算法介绍增加新顶点更新旧顶点算法实现半边结构半边结构的表示方法半边结构的构造方法创建所有顶点创建所有面及半边半边结构的使用 Loop 细分 顾名思义,网格细分是将粗糙网格精细化的过程,如下动图。Loop细分是众多网格细分算法的一种,Loop细分仅仅对三角形网格模型有效。值得注意的是,虽然叫Loop细分,但是不能理解为“循环”细分,叫这个名字是因为作者的名字是这个而已。 下面是一个粗糙的兔子模型1000个面,进行3次细分 算法介绍 Loop细分算法主要分两步进行 增加新顶点:增加

wk_119的博客 8067

命名管道和匿名管道

我们知道进程间是可以通信的。可以达到数据传输、资源共享、事件通知、进程控制等目的。进程间通信主要包括管道、系统IPC,套接字等。 管道分为三种: 普通管道:一是单工,只能单向传输 ,二是仅能在父子进程或者兄弟进程中使用。 流管道:去除了第一种限制,为半双工,可以双向传递, 命名管道:去除了第二种限制 ,可以在很多并不相关的进程 中进行通信。 1、普通管道 int pipe(int f...

ffsiwei的博客 1万+

PAT----A1088 Rational Arithmetic (20point(s))

A1088 Rational Arithmetic (20point(s)) 题意 模拟有理数四则运算。 思路 用两个long类型存储分子和分母,toString()方法封装输出有理数的逻辑。 总结 写的有点乱,有更好的方法吗。 Sample Input1: 2/3 -4/2 Sample Output1: 2/3 + (-2) = (-1 1/3) 2/3 - (-2) = 2 2/3 2/3...

qq_43578870的博客 168

【IC设计实战】去耦电容decap在电源完整性中的关键作用与选型策略

本文深入探讨了去耦电容(decap)在IC设计电源完整性中的核心作用。文章分析了decap如何通过提供本地电荷缓冲来抑制电源噪声与电压波动,并详细阐述了其选型与布局策略,包括MIM、MOM、MOS电容的特性对比,以及如何权衡峰值抑制与建立速度的矛盾,为设计稳定可靠的芯片电源系统提供实践指导。

weixin_29233857的博客 249

PAT A1088/B1034 Rational Arithmetic (20point(s))

题目链接 #include<cstdio> #include<algorithm> using namespace std; long long gcd(long long a,long long b){//最大公约数,用于化简 return !b? a:gcd(b,a%b); } struct fraction{ long long numerator,d...

皮皮瓜的博客 135

科研绘图系列:基因突变瀑布图统计以及可视化

科研绘图系列:基因突变瀑布图统计以及可视化

专注生信领域 1656

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

*PAT_甲级_1088 Rational Arithmetic (20point(s)) (C++)【分数运算/字符串处理】

目录 1,题目描述 题目大意 2,思路 3,AC代码 4,解题过程 1,题目描述 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 Inp...

&再见萤火虫&的博客 214

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

PAT 1088 Rational Arithmetic (20 分)

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

peanWang的博客 350

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

1088. Rational Arithmetic (20)

给出两个分数,求他们的和,差,积,商(分数四则运算); 通过辗转相除gcd获得最大公约数;可以把两个分数化为最简的形式; 重点是输出的格式 如果分母为零,那么输出的是Inf; 如果值为零,那么0 如果是正数,那么化为对应K a/b 或者K 或者a/b (其中a/b为真分数形式) 如果是负数,一定要有括号,化为对应的-K a/b 或者-K 或者-a/b

⊙-→棒棒糖ing .____` 1657

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 c...

Chuyuan_li的博客 484

1088 Rational Arithmetic(20 分)(cj)

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: Eac...

Cute_jinx的博客 216
上一篇: 1042 Shuffling Machine (20 point(s))
下一篇: 1034 Head of a Gang (30 point(s))
coderwait
博客等级 码龄9年 20粉丝 210原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值