nyoj 111-分数加减法 (gcd, switch, 模拟,数学)
111-分数加减法
内存限制:64MB
时间限制:1000ms
特判: No
通过数:20
提交数:54
难度:2
题目描述:
输入描述:
输入包含多行数据
每行数据是一个字符串,格式是"a/boc/d"。
其中a, b, c, d是一个0-9的整数。o是运算符"+"或者"-"。 数据以EOF结束
输入数据保证合法
输出描述:
对于输入数据的每一行输出两个分数的运算结果。
注意结果应符合书写习惯,没有多余的符号、分子、分母,并且化简至最简分数
样例输入:
1/8+3/8
1/4-1/2
1/3-1/3
样例输出:
1/2
-1/4
0
C/C++ AC:
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <stack>
#include <set>
#include <map>
#include <queue>
#include <climits> using namespace std; int gcd(int a, int b)
{
if (a < ) a = -a;
if (b < ) b = -b;
if (b == ) return a;
return gcd(b, a%b);
} int main()
{
int a, b, c, d, e, f;
char ch;
while (~scanf("%d/%d%c%d/%d", &a, &b, &ch, &c, &d))
{
if (b == d)
{
f = b;
switch (ch)
{
case '+':
e = a + c; break;
default:
e = a - c;
}
int temp = gcd(e, f);
e /= temp, f /= temp;
if (e == ) // 特殊判断1,分子为0
printf("0\n");
else if (f == ) // 特殊判断2,分母为1
printf("%d\n", e);
else
printf("%d/%d\n", e, f);
}
else
{
f = b * d;
switch (ch)
{
case '+':
e = a * d + c * b; break;
default:
e = a * d - c * b;
}
int temp = gcd(e, f);
e /= temp, f /= temp;
if (e == )
printf("0\n");
else if (f == )
printf("%d\n", e);
else
printf("%d/%d\n", e, f);
}
}
}
nyoj 111-分数加减法 (gcd, switch, 模拟,数学)的更多相关文章
- NYOJ题目111分数加减法
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAsEAAAKBCAIAAAA5i+FPAAAgAElEQVR4nO3dPXLbugMv7LsJ916Iay ...
- [LeetCode] Fraction Addition and Subtraction 分数加减法
Given a string representing an expression of fraction addition and subtraction, you need to return t ...
- [LeetCode] 592. Fraction Addition and Subtraction 分数加减法
Given a string representing an expression of fraction addition and subtraction, you need to return t ...
- ACM 分数加减法
分数加减法 时间限制:3000 ms | 内存限制:65535 KB 难度:2 描述 编写一个C程序,实现两个分数的加减法 输入 输入包含多行数据 每行数据是一个字符串,格式是" ...
- poj 3979 分数加减法
分数加减法 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13666 Accepted: 4594 Descriptio ...
- nyoj_111_分数加减法_201311281341
分数加减法 时间限制:3000 ms | 内存限制:65535 KB 难度:2 描述 编写一个C程序,实现两个分数的加减法 输入 输入包含多行数据 每行数据是一个字符串, ...
- Java练习 SDUT-2253_分数加减法
分数加减法 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 编写一个C程序,实现两个分数的加减法 Input 输入包含多 ...
- poj-3899-The Lucky Numbers 模拟+数学
题目链接: http://poj.org/problem?id=3899 题目意思: 求给定区间内,只含4.7的数的个数以及通过反转后在该区间内的个数和. 解题思路: 模拟+数学. 代码解释的很详细, ...
- 南阳理工ACM-OJ 分数加减法 最大公约数的使用
http://acm.nyist.net/JudgeOnline/problem.php?pid=111 简单模拟: #include <iostream> #include <st ...
随机推荐
- Github 上热门的 Spring Boot 项目实战推荐
最近经常被读者问到有没有 Spring Boot 实战项目可以学习,于是,我就去 Github 上找了 10 个我觉得还不错的实战项目.对于这些实战项目,有部分是比较适合 Spring Boot 刚入 ...
- [LUOGU1122] 最大子树和 - 树形动规
题目描述 小明对数学饱有兴趣,并且是个勤奋好学的学生,总是在课后留在教室向老师请教一些问题.一天他早晨骑车去上课,路上见到一个老伯正在修剪花花草草,顿时想到了一个有关修剪花卉的问题.于是当日课后,小明 ...
- Halcon C# 联合编程问题(三)
因为之前遇到的那个halcon处理的图片要转换成ImageSource的问题,迟迟没有找到好的解决方案, 于是决定直接在wpf中使用halcon提供的HWindowControlWPF,用于显示图片. ...
- OSI七层模型和五层TCP/IP协议
1.查公网ip的方法: windows,打开浏览器,访问百度,搜IP即可 linux:curl ifconfig.me 2.OSI七层模型 ==网络工程师:== 物理层 1层,通信介质的信号到数字信号 ...
- 06_基本框架_VMCS_GuestArea
1 Guest-State Area 1.1 References 注释: 其实 内容很多 一般是不背 的:需要的时候来查就行:但是要大致知道是做啥的. 代码 > 设置 Guest Area / ...
- (一)如何理解java面向对象编程
哲学中,事物总是螺旋式上升,波浪式前进.因而编程也逐渐向人类更容易理解的方向前进,多年来人们苦苦追求的编程境界 : 高扩展性(extensibility),高复用性(reuseable).java语言 ...
- .NET如何将字符串分隔为字符
.NET如何将字符串分隔为字符 如果这是一道面试题,答案也许非常简单:.ToCharArray(),这基本正确-- 我们以"AB吉
- Sentinel Getting Started And Integration of Spring Cloud Alibaba Tutorials
原文链接:Sentinel Getting Started And Integration of Spring Cloud Alibaba Tutorials Sentinel Getting Sta ...
- restapi(8)- restapi-sql:用户自主的服务
学习函数式编程初衷是看到自己熟悉的oop编程语言和sql数据库在现代商业社会中前景暗淡,准备完全放弃windows技术栈转到分布式大数据技术领域的.但是在现实中理想总是不如人意,本来想在一个规模较小的 ...
- 深copy
更好的对一个对象进行复制 using System; using System.Collections.Generic; using System.Linq; using System.Text; u ...