Dead Fraction
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 1762   Accepted: 568

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

Hint

Note that an exact decimal fraction has two repeating expansions (e.g. 1/5 = 0.2000... = 0.19999...).

Source

 
将分数分成循环的部分和非循环的部分
设分数为0.i1 i2 i3 i4 .. ik j1 j2 j3 .. jc               其中i1 ~ ik 为非循环的部分    j1 ~ jc为循环部分
非循环的部分可以拆成 b / a 其中 b = ( i1...ik)   a = 10 ^ (k)
循环的部分可以拆成  bb / aa 其中 bb = (j1 .. jc)  aa = 10 ^ (k + c) - 10 ^ ( k);
 
则 所求分数为 b / a + bb / aa     通分得 (b * aa + bb * a) / a * aa       约分得答案,由于据说数据会有全是0的坑爹数据,所以要判断一下
 
 
 #include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream> using namespace std; typedef long long ll; char s[];
ll ten_pow[]; ll gcd(ll x,ll y) {
return y > ? gcd(y,x % y) : x;
} void init() {
ten_pow[] = ;
for(int i = ; i <= ; i++) {
ten_pow[i] = ten_pow[i - ] * ;
} /*for(int i = 0; i <= 9; i++){
printf("%lld\n",ten_pow[i]); }*/
}
void solve() { ll a,b,aa,bb;
ll ans1,ans2 = -;
for(int i = ; i < strlen(s) - ; i++) {
b = ; bb = ;
for(int j = ; j < + i; j++) {
b += (s[j] - '') * ten_pow[ + i - j];
}
for(int j = + i; j < strlen(s); j++){
bb += (s[j] - '') * ten_pow[strlen(s) - - j];
} a = ten_pow[i];
aa = ten_pow[strlen(s) - ] - ten_pow[i];
// printf("b =%lld bb=%lld a = %lld aa = %lld\n",b,bb,a,aa); b = b * aa + bb * a;
a *= aa; ll t = gcd(a,b);
b /= t;
a /= t; //printf("a = %lld b = %lld\n",a,b);
if(ans2 == - || ans2 > a) {
ans2 = a;
ans1 = b;
} } printf("%I64d/%I64d\n",ans1,ans2); } int main() {
init();
// freopen("sw.in","r",stdin); while(~scanf("%s",s) && strlen(s) != ) { int pos = strlen(s) - ;
while(s[pos] == '.') {
s[pos--] = '\0';
} bool flag = ;
for(int i = ; i < strlen(s); i++) {
if(s[i] != '' ) flag = ; } //puts(s); if(!flag) {
printf("0/1\n");
continue;
} solve(); } return ;
}

POJ 1930的更多相关文章

  1. POJ 1930 Dead Fraction

    POJ 1930 Dead Rraction 此题是一个将无限循环小数转化为分数的题目 对于一个数 x=0.abcdefdef.... 假设其不循环部分的长度为m(如abc的长度为m),循环节的长度为 ...

  2. Mathematics:Dead Fraction(POJ 1930)

    消失了的分式 题目大意:某个人在赶论文,需要把里面有些写成小数的数字化为分式,这些小数是无限循环小数(有理数),要你找对应的分母最小的那个分式(也就是从哪里开始循环并不知道). 一开始我也是蒙了,这尼 ...

  3. POJ 1930 Dead Fraction (循环小数-GCD)

    题意:给你一个循环小数,化成分数,要求分数的分母最小. 思路:暴力搜一遍循环节 把循环小数化分数步骤: 纯循环小数化分数 纯循环小数的小数部分可以化成分数,这个分数的分子是一个循环节表示的数,分母各位 ...

  4. poj 1930 Dead Fraction(循环小数化分数)

    Dead Fraction Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 3478   Accepted: 1162 Des ...

  5. ProgrammingContestChallengeBook

    POJ 1852 Ants POJ 2386 Lake Counting POJ 1979 Red and Black AOJ 0118 Property Distribution AOJ 0333 ...

  6. 一些关于中国剩余定理的数论题(POJ 2891/HDU 3579/HDU 1573/HDU 1930)

    2891 -- Strange Way to Express Integers import java.math.BigInteger; import java.util.Scanner; publi ...

  7. poj 题目分类(1)

    poj 题目分类 按照ac的代码长度分类(主要参考最短代码和自己写的代码) 短代码:0.01K--0.50K:中短代码:0.51K--1.00K:中等代码量:1.01K--2.00K:长代码:2.01 ...

  8. POJ题目分类(按初级\中级\高级等分类,有助于大家根据个人情况学习)

    本文来自:http://www.cppblog.com/snowshine09/archive/2011/08/02/152272.spx 多版本的POJ分类 流传最广的一种分类: 初期: 一.基本算 ...

  9. POJ题目细究

    acm之pku题目分类 对ACM有兴趣的同学们可以看看 DP:  1011   NTA                 简单题  1013   Great Equipment     简单题  102 ...

随机推荐

  1. 将mysql的查询结果输出到文件

    在sql命令中我们可以查询到前数行的表,同时也可以将查询结果输出到txt文档 语句:select * from tablename into outfile 'filename.txt'; 例如:se ...

  2. 四位数码管SH5461AS的问题,arduino学习实测.

    arduino入门教程到第16课遇到些问题.效果一直是混乱的状态. 琢磨了半天发现一些问题,和大家分享下 1)接线图,原图没有问题,只是比较含糊,线比较多不好看. 我用红色数字标示数码管的12个脚,并 ...

  3. jQuery增加删除修改tab导航特效

    HTML:         <div class="container iden_top">                <ul>             ...

  4. jQuery学习-----(二)JQuery对象与DOM对象的区别与转换

    1.jQuery对象和DOM对象的区别 DOM对象,即是我们用传统的方法(javascript)获得的对象,jQuery对象即是用jQuery类库的选择器获得的对象; eg: var domObj = ...

  5. DataGridView导入导出excel

    DataGridView导出到Excel #region 方法一 DateGridView导出到csv格式的Excel /// <summary> /// 导出数据到Excel.常用方法, ...

  6. CodeIgniter(CI 3.0)分页类实践记录

    最近在学习B/S,选择了PHP CI框架作为切入点. 在尝试制作个人CMS的时候遇到了需要分页的情况,网上好像搜不到3.0版本以上的例子,下面附上本地实验的代码,供参考. 数据库情况如下: 首先看Co ...

  7. mysql 的数据类型

    mysql 的数据类型(描述的是字段)三大类:一.整型:1.tinyint(M),其中M是显示宽度,需要配合zerofill,就是前面0填充,存储单位为1个字节(8位),无符文是最大能存储范围0000 ...

  8. 在 SQL Server 中的网络数据库文件的支持说明

    其实就是一个学员问SQL Server 是否能存放的于NAS(UAC 的路径下). 官方的回答简略版本为:可以,需要满足一些强制性的硬件要求.但需要考虑一系列的性能的问题. http://suppor ...

  9. PAT IO-02 整数四则运算

    /* *PAT IO-02 整数四则运算 *2015-07-30 *作者:flx413 */ #include<stdio.h> int main() { int a, b; scanf( ...

  10. 如何参与linux 内核开发

    如果想评论或更新本文的内容,请直接联系原文档的维护者.如果你使用英文 交流有困难的话,也可以向中文版维护者求助.如果本翻译更新不及时或者翻 译存在问题,请联系中文版维护者.   英文版维护者: Gre ...