111-分数加减法

内存限制:64MB
时间限制:1000ms
特判: No

通过数:20
提交数:54
难度: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

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, 模拟,数学)的更多相关文章

  1. NYOJ题目111分数加减法

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

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

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

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

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

  4. ACM 分数加减法

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

  5. poj 3979 分数加减法

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

  6. nyoj_111_分数加减法_201311281341

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

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

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

  8. poj-3899-The Lucky Numbers 模拟+数学

    题目链接: http://poj.org/problem?id=3899 题目意思: 求给定区间内,只含4.7的数的个数以及通过反转后在该区间内的个数和. 解题思路: 模拟+数学. 代码解释的很详细, ...

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

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

随机推荐

  1. 手机号码格式化显示javascript

    /*手机代码格式化一般与Object.oninput=function(){}连用*/ function phoneFormat(phone){ if(phone.length <= 3 ){ ...

  2. Windows 批量修改文件后缀名

    利用ren 文件名替换命令 for循环去批处理 @echo off for %%m in (*) do ( if not "%%m"=="temp.bat"( ...

  3. 算法问题实战策略 DICTIONARY

    地址 https://algospot.com/judge/problem/read/DICTIONARY 解法 构造一个26字母的有向图 判断无回路后 就可以输出判断出来的字符序了 比较各个字母的先 ...

  4. [洛谷P3613]睡觉困难综合症

    写码30min,调码3h的题.. 好在最后查出来了 , , n, x, y, z); 改成了 , , n, mark[x], y, z); 然后$40\rightarrow 100$ #include ...

  5. 使用 pdf.js 在网页中加载 pdf 文件

    在网页中加载并显示PDF文件是最常见的业务需求.例如以下应用场景:(1)在电商网站上购物之后,下载电子发票之前先预览发票.(2)电子商务管理系统中查看发布的公文,公文文件一般是PDF格式的文件. 目前 ...

  6. Java 异常(二) 自定义异常

    上篇文章介绍了java中异常机制,本文来演示一下自定义异常 上篇文章讲到非运行时异常和运行时异常,下面我们来看一下简单实现代码. 首先,先来看下演示目录 非运行时异常 也称 检查时异常 public ...

  7. HTTP 结构详解

    转至 :https://blog.csdn.net/u010256388/article/details/68491509?utm_source=copy   引用 学习Web开发不好好学习HTTP报 ...

  8. Ubuntu svn 安装 Rabbitvcs

    先添加源 sudo add-apt-repository ppa:rabbitvcs/ppa 必要的话在源清单里面也添加一下 sudo gedit /etc/apt/sources.list 内容是 ...

  9. Linux之ELF文件初探

    对比windowsPE文件与概述 在windows中可执行文件是pe文件格式,Linux中可执行文件是ELF文件,其文件格式是ELF文件格式,在Linux下的ELF文件除了可执行文件(Excutabl ...

  10. ElasticSearch 中文分词插件ik 的使用

    下载 IK 的版本要与 Elasticsearch 的版本一致,因此下载 7.1.0 版本. 安装 1.中文分词插件下载地址:https://github.com/medcl/elasticsearc ...