Dead Fraction
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 1258   Accepted: 379

Description

Mike is frantically scrambling to finish his thesis at the last minute. He needs to assemble all his research notes into vaguely coherent form in the next 3 days. Unfortunately, he notices that he had been extremely sloppy in his calculations. Whenever he needed to perform arithmetic, he just plugged it into a calculator and scribbled down as much of the answer as he felt was relevant. Whenever a repeating fraction was displayed, Mike simply reccorded the first few digits followed by "...". For instance, instead of "1/3" he might have written down "0.3333...". Unfortunately, his results require exact fractions! He doesn't have time to redo every calculation, so he needs you to write a program (and FAST!) to automatically deduce the original fractions. 
To make this tenable, he assumes that the original fraction is always the simplest one that produces the given sequence of digits; by simplest, he means the the one with smallest denominator. Also, he assumes that he did not neglect to write down important digits; no digit from the repeating portion of the decimal expansion was left unrecorded (even if this repeating portion was all zeroes).

Input

There are several test cases. For each test case there is one line of input of the form "0.dddd..." where dddd is a string of 1 to 9 digits, not all zero. A line containing 0 follows the last case.

Output

For each case, output the original fraction.

Sample Input

0.2...
0.20...
0.474612399...
0

Sample Output

2/9
1/5
1186531/2500000 题意:最后一位表示循环节,
 #include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm> #define inf 1000000000
#define ll long long using namespace std;
char ch[];
ll ans1,ans2;
ll gcd(ll a,ll b)
{
return b==?a:gcd(b,a%b);
}
void solve(ll a,ll b,ll c,ll d)
{
ll t1=a*d+b*c,t2=b*d,t=gcd(t1,t2);
t1/=t;t2/=t;
if(t2<ans2)ans2=t2,ans1=t1;
}
int main()
{
while(~scanf("%s",ch+))
{
ans2=(ll)1e60;
int n=strlen(ch+);
if(n==)break;
ll b=,a=;
for(int i=;i<=n-;i++)
a=a*+ch[i]-'',b*=;//三个.
ll t=b/*;
for(ll i=;i<=b;i*=,t=t+(b/i)*)
solve(a/i,b/i,a%i,t);
printf("%lld/%lld\n",ans1,ans2);
}
}
 
 

最新文章

  1. python简单搭建HTTP Web服务器
  2. css 超过宽度显示...
  3. Value must be an existing directory配置tomcat问题
  4. 在项目中那个少用if else 语句,精简代码,便于维护的方法(1)
  5. 第六章:Javascript对象
  6. Android设计模式(1)----单例模式
  7. powershell创建并加载配置文件
  8. iOS iOS8新特性--UIPopoverPresentationController
  9. Unix中$$、$@、$#、$*的意思
  10. Mysql 权限修改何时生效
  11. C语言文件操作函数大全
  12. nefu 197 关键字检索(kmp算法)
  13. 【转】jqGrid学习之安装
  14. TCP 和 UDP
  15. 15 Actionbar的显示和隐藏
  16. SQL语句练习题【主供自己学习、记忆】
  17. Chromium Embedded Framework (CEF)_3.2171.2069_v20170606_x86.tar.xz
  18. Unity 思考问题的办法
  19. svn解决不能clean的方法
  20. PAT 1097 Deduplication on a Linked List[比较]

热门文章

  1. MFC U盘检测
  2. red5 重新分配 ip
  3. leetcode_1049. Last Stone Weight II_[DP]
  4. Prim算法解决最小生成树
  5. vue2.0动画
  6. Injection of autowired dependencies failed;错误解决
  7. Java生成固定长度的随机字符串(以大小写字母和数字)
  8. Feign-请求不同注册中心的服务
  9. vs 2017 boost 安装目录 非安装
  10. s:iterator的多层迭代