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. DEDECMS中,文章页直接输出字段名

    文章页中,可直接输出字段名

  2. sql模糊查询

    SQL 模糊查询 在进行数据库查询时,有完整查询和模糊查询之分. 一般模糊查询语句如下: SELECT 字段 FROM 表 WHERE 某字段 Like 条件 其中关于条件,SQL提供了四种匹配模式: ...

  3. c# 取得ip地址和网关

    /// <summary> /// 得到本机IP /// </summary> private string GetLocalIP() { //本机IP地址 string st ...

  4. jquery easyui combox实用方法记录

    // combogrid刷新 $(“#cc").combogrid('grid').datagrid('load'); // combogrid设置默认选中哪一行 $('#cc').comb ...

  5. 使用winform来递归实现资源管理器

    这里主要是对TreeView控件的学习 所使用到的方法: string path=@"E:\歌词"; //获得指定文件夹下所有文件夹的名称,这是带路径的,如:E:\歌词\11111 ...

  6. 层叠水平(stacking level)

    运用上图的逻辑,上面的题目就迎刃而解,inline-blcok 的 stacking level 比之 float 要高,所以无论 DOM 的先后顺序都堆叠在上面. 不过上面图示的说法有一些不准确,按 ...

  7. Mysql 正则获取字段的交集【转】

    问题描述 比如table1中有两条记录 name no a    2,9 b    8,10 然后有一串字符串,是0,1,2,3,4 然后通过一条sql,找出no为2,9的记录来``` 因为字符串中有 ...

  8. 使用自定义的item、Adapter和AsyncTask、第三方开源框架PullToRefresh联合使用实现自定义的下拉列表(从网络加载图片显示在item中的ImageView)

    AsyncTask使用方法详情:http://www.cnblogs.com/zzw1994/p/4959949.html 下拉开源框架PullToRefresh使用方法和下载详情:http://ww ...

  9. 比较不错的JS 曲线图

    fashion chart   falsh文件支持,无需考虑兼容 Highcharts(纯JS,很漂亮 效果很好) Highcharts是一个制作图表的纯Javascript类库,主要特性如下: 兼容 ...

  10. MySQL多实例-精典故障案例

    很久以前搭建过MySQL多实例,记得当时很顺利,呵呵!今天公司因为业务需要,我再一次搭建多实例.安装完MySQL后,初始化两个实例时,出现如下报错: 150915  1:10:36 [ERROR] C ...