ACM 分数加减法
分数加减法
- 描述
- 编写一个C程序,实现两个分数的加减法
- 输入
- 输入包含多行数据
每行数据是一个字符串,格式是"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 特别注意2/1+4/1这种整除的情况#include <iostream>
#include <string>
#include <algorithm>
using namespace std; int gcd(int a,int b){
if(a < ) a = -a;
if(a < b) swap(a,b);
while(b){
int t = b;
b = a%b;
a = t;
}
return a;
} int main(){
string str;
while(cin>>str){
int a = str[]-'', b = str[]-'', c=str[]-'', d=str[]-'';
int numerator = a*d+c*b,denominator=d*b;
if(str[] == '-') numerator = a*d-c*b;
int v = gcd(numerator,denominator);
numerator /=v;
denominator /=v;
if(numerator == ) cout<<<<endl;
else if(denominator == ) cout<<numerator<<endl;
else cout<<numerator<<"/"<<denominator<<endl;
}
}
ACM 分数加减法的更多相关文章
- poj 3979 分数加减法
分数加减法 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13666 Accepted: 4594 Descriptio ...
- [LeetCode] Fraction Addition and Subtraction 分数加减法
Given a string representing an expression of fraction addition and subtraction, you need to return t ...
- nyoj_111_分数加减法_201311281341
分数加减法 时间限制:3000 ms | 内存限制:65535 KB 难度:2 描述 编写一个C程序,实现两个分数的加减法 输入 输入包含多行数据 每行数据是一个字符串, ...
- [LeetCode] 592. Fraction Addition and Subtraction 分数加减法
Given a string representing an expression of fraction addition and subtraction, you need to return t ...
- Java练习 SDUT-2253_分数加减法
分数加减法 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 编写一个C程序,实现两个分数的加减法 Input 输入包含多 ...
- NYOJ题目111分数加减法
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAsEAAAKBCAIAAAA5i+FPAAAgAElEQVR4nO3dPXLbugMv7LsJ916Iay ...
- 南阳理工ACM-OJ 分数加减法 最大公约数的使用
http://acm.nyist.net/JudgeOnline/problem.php?pid=111 简单模拟: #include <iostream> #include <st ...
- POJ 3979 分数减法【数学问题的探讨】
将a/b和c/d简化一下就可以了 分数加减法 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12588 Accepted ...
- 【南阳OJ分类之语言入门】80题题目+AC代码汇总
小技巧:本文之前由csdn自动生成了一个目录,不必下拉一个一个去找,可通过目录标题直接定位. 本文转载自本人的csdn博客,复制过来的,排版就不弄了,欢迎转载. 声明: 题目部分皆为南阳OJ题目. 代 ...
随机推荐
- Python 小游戏 Bunny
最近在学习Python,所以上网找了一个小程序练练手. 关于这款名为[Bunny]的小游戏,详细请看下面的链接: http://www.oschina.net/translate/beginning- ...
- 设计模式学习之组合模式(Composite,结构型模式)(10)
转载地址:http://www.cnblogs.com/zhili/p/CompositePattern.html 一.引言 在软件开发过程中,我们经常会遇到处理简单对象和复合对象的情况,例如对操作系 ...
- HTML5学习之文档结构和语义(一)
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...
- x86架构的android手机兼容性问题
x86架构的android手机兼容性问题 http://www.cnblogs.com/guoxiaoqian/p/3984934.html 自从CES2012上Intel发布了针对移动市场的Medf ...
- 无废话ExtJs 入门教程十一[下拉列表:Combobox]
无废话ExtJs 入门教程十一[下拉列表:Combobox] extjs技术交流,欢迎加群(201926085) 继上一节内容,我们在表单里加了个一个下拉列表: 1.代码如下: 1 <!DOCT ...
- 无废话ExtJs 入门教程三[窗体:Window组件]
无废话ExtJs 入门教程三[窗体:Window组件] extjs技术交流,欢迎加群(201926085) 1.代码如下: 1 <!DOCTYPE html PUBLIC "-//W3 ...
- [LeetCode] Min Stack
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...
- NPOI读写Excel
1.整个Excel表格叫做工作表:WorkBook(工作薄),包含的叫页(工作表):Sheet:行:Row:单元格Cell. 2.NPOI是POI的C#版本,NPOI的行和列的index都是从0开始 ...
- 【leetcode】plus One
问题描述: Given a non-negative number represented as an array of digits, plus one to the number. The dig ...
- win7-32 系统 + VS2010 配置 glew
网上下载的程序,运行时报错: C1083: 无法打开包括文件:“gl\glew.h”: No such file or directory. 百度一下,发现需要配置 glew 库. 方法如下: 下载 ...