Mr. Potato Head has been promoted and now is a math professor at the UNAL.

For his first course he is willing to teach hard subjects, so at the moment he is teaching how to add and subtract fractions.

To complete his course the students have to do a long series of exercises, each exercise corresponds to a valid formula containing only additions and subtractions of fractions.

Formally a valid formula is one of the following:

  • A fraction
  • F1+F2F1+F2
  • F1−F2F1−F2
  • (F1)(F1)

where F1F1 and F2F2 are also valid formulas.

Mr. Potato Head knows that the exam would be impossible if fractions are too large or if they are negative, so he decides that for every fraction a/ba/b, 0≤a≤1000≤a≤100 and 0<b≤200<b≤20.

Can you pass the course of Mr. Potato Head?

Input

The input consists of several lines, each line contains a valid formula without spaces.

It is guaranteed that all lines contains valid formulas and the total number of characters in all formulas does not exceed 2∗1052∗105

Output

For each formula output a line with an irreducible fraction a/ba/b, b>0b>0 −− The solution of the corresponding formula

Example

Input
1/2+1/3
1/5-2/10
1/2+(1/2-2/1)
Output
5/6
0/1
-1/1

Note

A fraction is irreducible if its numerator and denominator do not have common divisors greater than 1

题解:只有加减法和括号的运算,可直接从前往后挨个计算,只需判断当前的符号是+或-即可.

#include <bits/stdc++.h>
#define met(a, b) memset(a, b, sizeof(a))
#define ll long long
#define ull unsigned long long
#define rep(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
ll gcd(ll a,ll b){return b==?a:gcd(b,a%b);}
ll lcm(ll a,ll b){return a*b/gcd(a,b);}
typedef pair<ll,ll>P;
const int maxn=;
const double eps=1e-;
const double pi=acos(-);
char s[maxn];
int len;
P ans;
int f,ff;
P cal(P ans,P now)
{
ll son=ans.first*now.second+f*ff*now.first*ans.second;
ll mon=ans.second*now.second;
ll g=gcd(abs(son),abs(mon));
son/=g;
mon/=g;
return P(son,mon);
}
P ok(int pos)
{
ll son=,mon=,l=-;
for(int i=pos-;i>=;i--){
if(s[i]<''||s[i]>''){
l=i;
break;
}
}
for(int i=l+;i<len;i++){
if(s[i]>=''&&s[i]<='')son=son*+s[i]-'';
else break;
}
for(int i=pos+;i<len;i++){
if(s[i]>=''&&s[i]<='')mon=mon*+s[i]-'';
else break;
}
return P(son,mon);
}
int main()
{
while(cin>>s){
stack<int>sta;
len=strlen(s);
ans=P(,);
f=ff=;
for(int i=;i<len;i++){
if(s[i]=='+'){
f=;
}
else if(s[i]=='-'){
f=-;
}
else if(s[i]=='('){
f=;
if(s[i-]=='-'){
ff*=-;
sta.push(-);
}
else{
sta.push();
}
}
else if(s[i]==')'){
int w=sta.top();
ff*=w;
sta.pop();
}
else if(s[i]=='/'){
P now=ok(i);
ans=cal(ans,now);
}
}
cout<<ans.first<<"/"<<ans.second<<endl;
}
return ;
}

F - Fraction Formula Gym - 102307F的更多相关文章

  1. Frightful Formula Gym - 101480F (待定系数法)

    Problem F: Frightful Formula \[ Time Limit: 10 s \quad Memory Limit: 512 MiB \] 题意 题意就是存在一个\(n*n\)的矩 ...

  2. Ceizenpok’s formula Gym - 100633J 扩展Lucas定理 + 中国剩余定理

    http://codeforces.com/gym/100633/problem/J 其实这个解法不难学的,不需要太多的数学.但是证明的话,我可能给不了严格的证明.可以看看这篇文章 http://ww ...

  3. AtCoder Grand Contest 003 F - Fraction of Fractal

    题目传送门:https://agc003.contest.atcoder.jp/tasks/agc003_f 题目大意: 给定一个\(H×W\)的黑白网格,保证黑格四连通且至少有一个黑格 定义分形如下 ...

  4. Atcoder Grand Contest 003 F - Fraction of Fractal(矩阵乘法)

    Atcoder 题面传送门 & 洛谷题面传送门 Yet another AGC F,然鹅这次就没能自己想出来了-- 首先需注意到题目中有一个条件叫做"黑格子组成的连通块是四联通的&q ...

  5. R笔记(1):formula和Formula

    #####开一个新的系列.关于R的一些笔记,就是遇到过的一些问题的简单整理.可能很基本,也可能没什么大的用处,作为一个记录而已.------------------------------------ ...

  6. Gym - 101480 CERC 15:部分题目题解(队内第N次训练)

    -------------------题目难度较难,但挺有营养的.慢慢补. A .ASCII Addition pro:用一定的形式表示1到9,让你计算加法. sol:模拟. solved by fz ...

  7. 2017 United Kingdom and Ireland Programming(Gym - 101606)

    题目很水.睡过了迟到了一个小时,到达战场一看,俩队友AC五个了.. 就只贴我补的几个吧. B - Breaking Biscuits Gym - 101606B 旋转卡壳模板题.然后敲错了. 代码是另 ...

  8. [C2P3] Andrew Ng - Machine Learning

    ##Advice for Applying Machine Learning Applying machine learning in practice is not always straightf ...

  9. objective-c第七章课后练习3

    题:Fraction类对负分数适用吗?例如:1/4-1/2 能否显示成-1/4? //--------类定义实现参考上篇随笔,此处需更改print方法和main主方法部分---------- - (v ...

随机推荐

  1. 2020牛客寒假算法基础集训营4 G音乐鉴赏

    题目描述 作为“音乐鉴赏”课的任课老师,你的课程作为刷学分好课一直受到广泛欢迎.但这一学期,学校制定了新的标准,你的课的优秀率(分数超过90分的人数)被限制在10%以下! 为了应对这个调整,你要求所有 ...

  2. 把a表格的内容读取出来,然后写到b表格

    把a表格的内容读取出来,然后写到b表格 #!/usr/bin/env python3 import sys #控制台要输入的两个参数格式为:python script_name.py 参数1 参数2 ...

  3. 吴裕雄--天生自然JAVA SPRING框架开发学习笔记:Spring CGLlB动态代理

    JDK 动态代理使用起来非常简单,但是它也有一定的局限性,这是因为 JDK 动态代理必须要实现一个或多个接口,如果不希望实现接口,则可以使用 CGLIB 代理. CGLIB(Code Generati ...

  4. 连接mysql的各种方式

    mysql连接操作是客户端进程与mysql数据库实例进程进行通信.从程序设计角度来说,属于进程通信,常用进程通信包括: 管道.Tcp/Ip 套接字.UNIX域套接字. 1.TCP/IP (1)使用最多 ...

  5. Dlib笔记二:matrix或array2d与cv::Mat的互转

    因为经常习惯的用OpenCV来做图像处理,所以难免希望将其他库的图像数据与OpenCV互转,所以今天就记录下这种互转的方法. 1.dlib::matrix/dlib::array2d转cv::Mat ...

  6. Ubuntu Kylin 14.04LTS 开机后卡在登陆界面,可以进入字符界面,或者登陆后鼠标不显示但是管用

    2014年4月27日,距离中期检查还有七天,基本上什么也没做,特别着急,雨已经下了快一天了,中午用美团外卖定的黄焖排骨,MD,什么玩意,那么一点点就18块钱,一看就不值五块钱,发誓再也不吃,最重要的是 ...

  7. lvs和keepalived

    LVS调度算法参考 RR:轮询 WRR :加权轮询 DH :目标地址哈希 SH:源地址hash LC:最少连接 WLC:加权最少连接,默认 SED:最少期望延迟 NQ:从不排队调度方法 LBLC:基于 ...

  8. 关于DSP仿真软件CCS中断点和探针的简单理解

    关于DSP仿真软件CCS中断简单理解 (郑州大学姬祥老师编写) CCS中的2.0版本(实验所用)断点(Break Point) 和探针(Probe Point)之所以能组合使用,是因为我们在实现硬件仿 ...

  9. 吴裕雄--天生自然MySQL学习笔记:MySQL ALTER命令

    需要修改数据表名或者修改数据表字段时,就需要使用到MySQL ALTER命令. root@host# mysql -u root -p password; Enter password:******* ...

  10. C++queue的使用

    C++队列是一种容器适配器,提供了一种先进先出的数据结构. 队列(queue)模板类定义在<queue>头文件中 基本操作: 定义一个queue变量:queue<Type> q ...