【算法笔记】B1034 有理数四则运算
本题要求编写程序,计算 2 个有理数的和、差、积、商。
输入格式:
输入在一行中按照 a1/b1 a2/b2 的格式给出两个分数形式的有理数,其中分子和分母全是整型范围内的整数,负号只可能出现在分子前,分母不为 0。
输出格式:
分别在 4 行中按照 有理数1 运算符 有理数2 = 结果 的格式顺序输出 2 个有理数的和、差、积、商。注意输出的每个有理数必须是该有理数的最简形式 k a/b,其中 k 是整数部分,a/b 是最简分数部分;若为负数,则须加括号;若除法分母为 0,则输出 Inf。题目保证正确的输出中没有超过整型范围的整数。
输入样例 1:
2/3 -4/2
输出样例 1:
2/3 + (-2) = (-1 1/3)
2/3 - (-2) = 2 2/3
2/3 * (-2) = (-1 1/3)
2/3 / (-2) = (-1/3)
输入样例 2:
5/3 0/6
输出样例 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 <iostream>
#include <algorithm>
using namespace std;
typedef long long ll; ll gcd(ll a, ll b){
if(a%b==) return b;
else gcd(b,a%b);
}
struct Fraction{
ll up, down;
}a, b;
Fraction reduction(Fraction f){
if(f.down < ){
f.up = -f.up;
f.down = -f.down;
}
if(f.up == ) f.down = ;
else{
int d = gcd(abs(f.up), abs(f.down));
f.up /= d;
f.down /= d;
}
return f;
}
Fraction add(Fraction f1, Fraction f2){
Fraction result;
result.up = f1.up * f2.down + f1.down * f2.up;
result.down = f1.down * f2.down;
return result;
}
Fraction minu(Fraction f1, Fraction f2){
Fraction result;
result.up = f1.up * f2.down - f1.down * f2.up;
result.down = f1.down * f2.down;
return result;
}
Fraction multi(Fraction f1, Fraction f2){
Fraction result;
result.up = f1.up * f2.up;
result.down = f1.down * f2.down;
return result;
}
Fraction divide(Fraction f1, Fraction f2){
Fraction result;
result.up = f1.up * f2.down;
result.down = f1.down * f2.up;
return result;
}
void show(Fraction f){
f = reduction(f);
if(f.up < ) printf("(");
if(f.down == ) printf("%lld",f.up);
else if(abs(f.up) > abs(f.down)){
printf("%lld %lld/%lld", f.up / f.down, abs(f.up) % f.down, f.down);
}else{
printf("%lld/%lld", f.up, f.down);
}
if(f.up < ) printf(")");
}
void showResult(Fraction f1, Fraction f2, Fraction result, char sign){
show(f1);
cout<<" "<<sign<<" ";
show(f2);
cout<<" = ";
if(result.down == ) cout<<"Inf";
else show(result);
cout<<endl;
}
int main(){
scanf("%lld/%lld %lld/%lld", &a.up, &a.down, &b.up, &b.down);
showResult(a, b, add(a, b), '+');
showResult(a, b, minu(a, b), '-');
showResult(a, b, multi(a, b), '*');
showResult(a, b, divide(a, b), '/');
return ;
}
【算法笔记】B1034 有理数四则运算的更多相关文章
- PAT B1034 有理数四则运算 (20 分)
本题要求编写程序,计算 2 个有理数的和.差.积.商. 输入格式: 输入在一行中按照 a1/b1 a2/b2 的格式给出两个分数形式的有理数,其中分子和分母全是整型范围内的整数,负号只可能出现在分子前 ...
- 算法笔记_229:有理数的循环节(Java)
目录 1 问题描述 2 解决方案 1 问题描述 1/7 = 0.142857142... 是个无限循环小数.任何有理数都可以表示为无限循环小数的形式. 本题目要求即是:给出一个数字的循环小数表示法 ...
- PAT-乙级-1034. 有理数四则运算(20)
1034. 有理数四则运算(20) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 本题要求编写程序,计算2个有理 ...
- 用java具体代码实现分数(即有理数)四则运算
用java具体代码实现分数(即有理数)四则运算 1,背景 Java老师布置了一个关于有理数运算的题目,因为参考书上有基本代码,所以自己主要是对书上代码做了一点优化,使其用户交互性更加友好以及代码封装性 ...
- 学习Java 以及对几大基本排序算法(对算法笔记书的研究)的一些学习总结(Java对算法的实现持续更新中)
Java排序一,冒泡排序! 刚刚开始学习Java,但是比较有兴趣研究算法.最近看了一本算法笔记,刚开始只是打算随便看看,但是发现这本书非常不错,尤其是对排序算法,以及哈希函数的一些解释,让我非常的感兴 ...
- 算法笔记--数位dp
算法笔记 这个博客写的不错:http://blog.csdn.net/wust_zzwh/article/details/52100392 数位dp的精髓是不同情况下sta变量的设置. 模板: ]; ...
- 算法笔记--lca倍增算法
算法笔记 模板: vector<int>g[N]; vector<int>edge[N]; ][N]; int deep[N]; int h[N]; void dfs(int ...
- 算法笔记--STL中的各种遍历及查找(待增)
算法笔记 map: map<string,int> m; map<string,int>::iterator it;//auto it it = m.begin(); whil ...
- 算法笔记--priority_queue
算法笔记 priority_queue<int>que;//默认大顶堆 或者写作:priority_queue<int,vector<int>,less<int&g ...
随机推荐
- Python使用signal模块实现定时执行
在liunx系统中要想每隔一分钟执行一个命令,最普遍的方法就是crontab了,如果不想使用crontab,经同事指点在程序中可以用定时器实现这种功能,于是就开始摸索了,发现需要一些信号的知识... ...
- mongodb学习-创建唯一索引(在已存在的集合创建)
如果在已存在的集合创建,可能会存在相同的值如下: 我们可以使用(2.x版本) db.users.ensureIndex({uid:1, name:1}, {unique:true, dropDups: ...
- Redis 数据库学习
安装(mac) 使用homebrew安装,命令是:brew install redis. 安装完成后启动命令:brew services start redis. 使用命令redis-cli进入red ...
- org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'deptDao_a' defined in class path resource [beansAndHibernate.xml]: Cannot resolve reference to bean 'sessionFact
Error creating bean with name 'deptDao_a' defined in class path 因为更改了类的名字,所以其setter方法没有更改,需要 private ...
- jQuar总结10:jQuery操作元素的属性
jQuery操作元素的属性 1 设置单个属性 //html <div></div> //js $('div').attr('id', 'box'); $('div').attr ...
- a 标签 name 属性 页面定位 (一)
a(§b) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ...
- java 访问HTTPS rest服务
import java.io.*;import java.net.*;import java.security.cert.CertificateException;import java.securi ...
- NIOS II下基于中断的UART接收和发送设计示例代码
#include "sys/alt_stdio.h" #include "altera_avalon_uart_regs.h" #include "s ...
- [LeetCode 题解]: Permutation Sequcence
The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
- hello world! hello blog!
2015年12月21日 16:42:15 博客开启!