分数加减法

时间限制:3000 ms  |  内存限制:65535 KB
难度:2
 
描述
编写一个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 分数加减法的更多相关文章

  1. poj 3979 分数加减法

    分数加减法 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13666   Accepted: 4594 Descriptio ...

  2. [LeetCode] Fraction Addition and Subtraction 分数加减法

    Given a string representing an expression of fraction addition and subtraction, you need to return t ...

  3. nyoj_111_分数加减法_201311281341

    分数加减法 时间限制:3000 ms  |           内存限制:65535 KB 难度:2   描述 编写一个C程序,实现两个分数的加减法   输入 输入包含多行数据 每行数据是一个字符串, ...

  4. [LeetCode] 592. Fraction Addition and Subtraction 分数加减法

    Given a string representing an expression of fraction addition and subtraction, you need to return t ...

  5. Java练习 SDUT-2253_分数加减法

    分数加减法 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 编写一个C程序,实现两个分数的加减法 Input 输入包含多 ...

  6. NYOJ题目111分数加减法

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAsEAAAKBCAIAAAA5i+FPAAAgAElEQVR4nO3dPXLbugMv7LsJ916Iay ...

  7. 南阳理工ACM-OJ 分数加减法 最大公约数的使用

    http://acm.nyist.net/JudgeOnline/problem.php?pid=111 简单模拟: #include <iostream> #include <st ...

  8. POJ 3979 分数减法【数学问题的探讨】

    将a/b和c/d简化一下就可以了 分数加减法 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12588   Accepted ...

  9. 【南阳OJ分类之语言入门】80题题目+AC代码汇总

    小技巧:本文之前由csdn自动生成了一个目录,不必下拉一个一个去找,可通过目录标题直接定位. 本文转载自本人的csdn博客,复制过来的,排版就不弄了,欢迎转载. 声明: 题目部分皆为南阳OJ题目. 代 ...

随机推荐

  1. 关闭 Visual Studio 2013 的 Browser Link 功能

    最近公司弄新项目需要用 MVC,就把 IDE 升级到了 Visual Studio 2013,在开发的时候发现有好多请求一个本地49925的端口 . 很奇怪,一开始以为是 Visual Studio ...

  2. Android 线程更新UI报错 : Can't create handler inside thread that has not called Looper.prepare()

    MainActivity中有一个按钮,绑定了save方法 public void save(View view) { String title = titleText.getText().toStri ...

  3. Installing MySQL Server on CentOS

    MySQL is an open-source relational database. For those unfamiliar with these terms, a database is wh ...

  4. .NET Nancy 详解(一) 初识

    Nancy 是一个轻量级的,简单粗暴的framework用来构建基于HTTP的各种服务,兼容.Net和Mono.Nancy的整套设计理念是基于"super-duper-happy-path& ...

  5. html5 notification桌面提醒功能

    html5 notification桌面提醒功能 <!DOCTYPE html> <html lang="en"> <head> <met ...

  6. 报错:1130-host ... is not allowed to connect to this MySql server 开放mysql远程连接 不使用localhost

    执行如下命令报错 mysql -uroot -h${hostIp} -p Enter password:********* ERROR (HY000): Host '$hostIp' is not a ...

  7. Vue#计算属性

    在模板中表达式非常便利,但是它们实际上只用于简单的操作.模板是为了描述视图的结构.在模板中放入太多的逻辑会让模板过重且难以维护.这就是为什么 Vue.js 将绑定表达式限制为一个表达式.如果需要多于一 ...

  8. html table之 全选,全不选

    就是这个小功能让我和组长引发争端,就是这个小功能让我差点"被"辞职,就是这个自封装的js方法让我放下了对组长的敬畏之心,现在分享一下,其实也很简单,但是真的有这么简单吗? < ...

  9. CodeForces 19D Points(线段树+map)

    开始想不通,后来看网上说是set,就有一个想法是对每个x建一个set...然后又想直接建立两重的set就好,最后发现不行,自己想多了...  题意是给你三种操作:add (x y) 平面添加(x y) ...

  10. MySQL主从复制(Master-Slave)与读写分离(MySQL-Proxy)实践

    主服务器上(注:应该是允许从机访问)  GRANT REPLICATION SLAVE ON *.* to ‘rep1’@’192.168.10.131’ identified by ‘passwor ...