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. Java并发编程概要

    线程开的越多,则性能越好吗? 未必,影响多线程性能的因素有:上下文切换,竞争/死锁,资源限制等.对于这些因素要均衡考量,才能获得较好的性能. 并发控制/线程间的通信方式 基本的并发控制原语有 vola ...

  2. 存储过程及Comm.cs类的创建

    2013-09-25 13:08:59 一.准备工作 首先创建一个数据库,如创建“试用期公务员管理”数据库:再创建一个Comm.cs类,添加代码如下: using System;using Syste ...

  3. Linux 本地yum源搭建和网络yum源搭建

    一.本地yum源搭建 首先挂载上光盘 [root@www /]# mount /dev/cdrom /media/cdrom/ 系统默认已经安装了可使用yum的软件包,所以可以直接配置: [root@ ...

  4. Application,Session和Cookie

    做ASP.NET,肯定会和这几个对象打交道,这些也是基础面试的常见题目,总结一下还是必要的,好在大神已经总结好了,直接参考就好了: http://www.cnblogs.com/breezeblew/ ...

  5. wpf 仿QQ音乐歌词卡拉OK

    最近用WPF做了个音乐播放器,读取歌词.歌词同步都已经实现了.卡拉OK逐字变色 也实现了,但是逐字变色时不能根据歌手唱的快慢来逐字显示.请问各位大神,这个如何解决,有何思路?(附上我做的界面) 感谢各 ...

  6. System V消息队列

    消息的基本属性 System V的消息属性包含在一个msqid_ds的结构中 struct msqid_ds{ struct ipc_cerm msg_perm; //读取权限, 0644, 0777 ...

  7. GITHUB 提交错误 Error: Permission denied (publickey) 解决

    1.  在开发机上生成自己的密钥 ssh-keygen -b 1024 -t rsa -b 指密钥对长度  -t 指加密方式 Enter file in which to save the key ( ...

  8. C++实现glut绘制点、直线、多边形、圆

    C++实现glut绘制点.直线.多边形.圆 必备环境 glut.h 头文件 glut32.lib 对象文件库 glut32.dll 动态连接库 程序说明 C++实现了用glut画点.画直线.画多边形和 ...

  9. 第二章 Spring MVC入门

    2.1.Spring Web MVC是什么 Spring Web MVC是一种基于Java的实现了Web MVC设计模式的请求驱动类型的轻量级Web框架,即使用了MVC架构模式的思想,将web层进行职 ...

  10. C/C++中的可变参函数

    可变参函数最好的实例:printf();参数可变 包含的头文件: C语言中:#include<stdarg.h> C++中的可变参的头文件:#include<cstdarg>, ...