bjfu1011 Convert decimal to fraction
来源:互联网 发布:2017年java就业前景 编辑:程序博客网 时间:2024/06/05 09:50
Convert decimal to fraction
时间限制(C/C++):1000MS/3000MS 运行内存限制:65536KByte
总提交:202 测试通过:47
总提交:202 测试通过:47
描述
As we all know, every decimal can be converted to a fractional number.
So you please write a program to do this, not only ordinary decimal, but also circulating decimal, and remember that the result must be fraction in lowest term.
输入
The input consists of multiple test cases.
Each test case consists a Pure decimal (decimal without integer part), and the cyclic part should be enclosed buy brackets.And the length of each data would be less than 15 characters.
输出
For each test case, print each fraction in lowest term in a line.
样例输入
0.250.(5)0.32(692307)
样例输出
1/45/917/52
题目来源
ben
题意:小数变分数,括号内的无限循环。
AC代码:
#include<iostream> #include<math.h> #include<string> using namespace std; void gcd(__int64 m,__int64 n){ __int64 a=m,b=n,c; while(b!=0){ c=b; b=a%b; a=c; } if(m==0){ printf("0\n"); return; } if(a!=0) printf("%I64d/%I64d\n",m/a,n/a); return; } __int64 powhaha(__int64 l){ __int64 ans=1,a=10; while(l>0){ if(l%2==1) ans*=a; l/=2; a*=a; } return ans; } int main(){ __int64 d,num,wei,len,i,g; string s; while(cin>>s){ num=wei=0; d=g=0; len=s.length(); for(i=2;i<len;i++) if(s[i]=='('){ g++; break; } if(g==0){ for(i=2;i<len;i++) num=num*10+s[i]-48; wei=powhaha(len-2); gcd(num,wei); } else if(s[2]=='('){ for(i=3;i<len-1;i++) num=num*10+s[i]-48; wei=powhaha(len-4)-1; gcd(num,wei); } else{ for(i=2;i<len;i++){ if(s[i]!='('){ wei=wei*10+s[i]-48; d=d*10+9; } else break; } for(i=2;i<len;i++){ if(s[i]==')') break; if(s[i]!='(') num=num*10+s[i]-48; else continue; } num=num-wei; wei=powhaha(len-4)-1; wei=wei-d; gcd(num,wei); } } return 0; }
0 0
- bjfu1011 Convert decimal to fraction
- Fraction to Recurring Decimal
- Fraction to Recurring Decimal
- Fraction to Recurring Decimal
- Fraction to recurring decimal
- Fraction to Recurring Decimal
- Fraction to Recurring Decimal
- Fraction to Recurring Decimal
- Fraction to Recurring Decimal
- Fraction to Recurring Decimal
- Fraction to Recurring Decimal
- Fraction to Recurring Decimal
- Fraction to Recurring Decimal
- Fraction to Recurring Decimal
- Fraction to Recurring Decimal
- Fraction to Recurring Decimal
- Fraction to Recurring Decimal
- Fraction to Recurring Decimal
- 分享常见网站的爬虫程序下载地址[微信|微博|企信|知乎|优酷|京东]
- 洛谷 P1403 约数研究
- Markdown 存在目录文档模板
- Cadence教程4——环形振荡器
- 结合spring进行异常封装
- bjfu1011 Convert decimal to fraction
- 编程珠玑第二章问题A,B,C
- HTTP Header 详解
- (数字图像处理)腐蚀/膨胀/开运算/闭运算
- 黄阶低级 - 《 Java 核心》- 4.2 使用预定义类 - 修改0次
- ANDROID应用架构之MVP实现
- Android LayoutInflater详解
- Java中继承关系的构造函数的调用顺序
- detached HEAD意义详解