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. POJ 2287 Tian Ji -- The Horse Racing(贪心)

    题意:田忌和齐王有n匹马,进行n局比赛,每局比赛输者给胜者200,问田忌最多能得多少钱. 分析:如果田忌最下等的马比齐王最下等的马好,是没必要拿最下等的马和齐王最好的马比的.(最上等马同理) 因此,如 ...

  2. NDK 通过java调用so文件

    首先我们来看so文件的来源 1. 自己写.c文件,然后生成so库 2. 引用别人的静态库,或者动态库来生成新的jni调用库. 我们先来看最简单的编写一个jni调用的so库,包含一个获取字符串的方法,通 ...

  3. ☆RHEL6创建软raid的使用☆——经典之作

    raid主要的种类 1.raid0  扩展卷   raid 0又称Stripee或Striping,中文译为集带工作方式, 有时也可以理解为拼凑. 它是将要存取的数据以条带状的形式尽量平均分配到多个硬 ...

  4. 使用C++读取UTF8及GBK系列的文本方法及原理

    作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4374404.html 1.读取UTF-8编码文本原理 首先了解UTF-8的编码方式,UTF- ...

  5. VS2010恢复默认编辑环境的设置

    VS2010恢复默认编辑环境的设置 VS2010在安装完成后初次打开的时候可以设置自己常用的环境为默认打开的编辑环境, 也可以在打开IDE以后通过如下步骤设置默认环境: Tools->Impor ...

  6. webstorm 自定义代码模板

    Ctrl+Shift+A查找设置live 添加代码片段Live Template 编写代码的时候 缩写+Tab 即可输出代码片段 缩写启动对应代码片段的钥匙. 描述代码片段的名字. 模板文本代码片段的 ...

  7. webBrowser执行js的方法,并返回值,c#后台取值

    private void Form1_Load(object sender, EventArgs e) { webBrowser1.Navigate(Application.StartupPath + ...

  8. Silverlight 中DataGrid中全选与非全选问题

    问题:当点击全选时,全选所有的复选框,但是滚动屏幕时,却复选框就会取消选中 一.解决方法(将要展示的实体数据模型添加bool属性,在数据绑定时添加click时间,盘带选中的状态,就可以了) 1. xa ...

  9. PHP获取当前日期或指定日期的所在月的第一天和最后一天

    <?php function getthemonth($date) { $firstday = date('Y-m-01', strtotime($date)); $lastday = date ...

  10. iis 下的 selfssl

    当然,如果你想省掉所有这些麻烦也行,最简单的在IIS启动SSL的方法只要3步: 1. 下载 IIS 6.0 Resource Kit Tools: http://www.microsoft.com/d ...