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. idea基本

    IntelliJ IDEA特色功能 IDEA所提倡的是智能编码,是减少程序员的工作,IDEA的特色功能有以下25点:智能的选取在很多时候我们要选取某个方法,或某个循环或想一步一步从一个变量到整个类慢慢 ...

  2. web应用中webapp. root重用问题解决方案

      同一个tomcat服务器里面部署两个JavaEE项目,都是用了log4j做日志.并且web.xml里面都监听了日志信息. 启动服务的时候报错. 于是在web.xml添加以下代码:   <di ...

  3. fluent nhibernate 初体验

    离开.net框架两年时间,发展的很快呀.原先自我感觉良好到以为只差一个MVP的考核什么的,现在觉得真的差好远了. 呵呵,废话就不多说了.这次花了两天时间才拿下fluent nhibernate的fir ...

  4. UIPickerView基本用法

    #import "ViewController.h" #import <UIKit/UIKit.h> @interface ViewController : UIVie ...

  5. 【风马一族_Python】 安装pip与Numpy

    ------------------------------------------------------------------------------------------------- 原因 ...

  6. 4.Servlet_Form表单处理

    1.建项目"3Servlet_Form",src下建包“com.amaker.servlet”,web-root下建Register.html <!DOCTYPE html& ...

  7. C# 解析XML格式的字符串

    public CreateOrderReturnResult GetCreateOrderReturnApi() { var result = new CreateOrderReturnResult( ...

  8. 使用MongoDB的开源项目

    根据谷歌的搜索结果筛选出来的. 统计应用 counlty https://count.ly/ mongopress 开源CMS系统 http://www.mongopress.org/ Rubedo ...

  9. apache+tomcat整合

    一 .Apache与Tomcat的比较 apache支持静态页面,tomcat支持动态的,比如servlet等. 一般使用apache+tomcat的话,apache只是作为一个转发,对jsp的处理是 ...

  10. Python-Day2 Python基础进阶之数据类型

    一.数据类型 Python3 中有六个标准的数据类型: Number(数字) String(字符串) List(列表) Tuple(元组) Sets(集合) Dictionary(字典) Python ...