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 ...
随机推荐
- boost::multi_index 多索引容器
#include "stdafx.h" #include <string> #include <boost/multi_index_container.hpp&g ...
- 7.HTTP协议
1.什么是url? 1.1 URL是统一资源定位符,表示的是一个资源,(图片 文字 视频 音频 等等) 单个资源介绍--图片 那URL的组成部分是由协议, 域名:端口, 路径和文件名 1.2 url组 ...
- Dell R720 RAID配置
Dell服务器上一般都带有Raid卡,Raid5配置请看下边,亲们 1. 将服务器接上电源,显示器,键盘,并开机 2. 按 ctrl + R进入Raid设置 3. 将光标放置在Raid卡那,按F2,选 ...
- Java中常用的四种线程池
在Java中使用线程池,可以用ThreadPoolExecutor的构造函数直接创建出线程池实例,如何使用参见之前的文章Java线程池构造参数详解.不过,在Executors类中,为我们提供了常用线程 ...
- java中常见的字符串API
java中定义了String类来封装字符串,并提供一系列的操作字符串的方法,他们都位于java.lang包下. package Main; public class String01 { public ...
- 百万年薪python之路 -- MySQL数据库之 MySQL行(记录)的操作(二) -- 多表查询
MySQL行(记录)的操作(二) -- 多表查询 数据的准备 #建表 create table department( id int, name varchar(20) ); create table ...
- Spring Cloud 网关服务 zuul 二
有一点上篇文章忘了 讲述,nacos的加载优先级别最高.服务启动优先拉去配置信息.所以上一篇服务搭建我没有讲述在nacos 中心创建的配置文件 可以看到服务端口和注册中心都在配置文件中配置化 属性信息 ...
- Shiro权限管理框架(四):深入分析Shiro中的Session管理
其实关于Shiro的一些学习笔记很早就该写了,因为懒癌和拖延症晚期一直没有落实,直到今天公司的一个项目碰到了在集群环境的单点登录频繁掉线的问题,为了解决这个问题,Shiro相关的文档和教程没少翻.最后 ...
- Text 尺寸获取
获取text在当前文本内容下应该尺寸: 宽度:text.preferredWidth 高度:text.preferredHeight
- snaic和tornado的简单性能测试
操作系统 : CentOS7.3.1611_x64 Python 版本 : 3.6.8 tornado版本:6.0.2 snaic版本:19.9.0 CPU : Intel(R) Core(TM) i ...