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

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

 
 题意:将一个无限循环小数化作分数
  这道题并没有告诉循环节是多少,并且让求分母最小的,所以暴力每个循环节
 #include <stdio.h>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <math.h>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
using namespace std;
typedef long long ll;
const int MAXN = ;
const int INF = 0x3f3f3f3f;
char s[MAXN];
//欧几里得求最大公因数
int gcd(int x, int y)
{
if (x<y) swap(x, y);
return y == ? x : gcd(y, x%y);
}
//快速幂
int q_pow(int a, int b)
{
int r = , base = a;
while (b)
{
if (b & ) r *= base;
base *= base;
b >>= ;
}
return r;
}
int main(void)
{
while (scanf("%s", s) != EOF && strcmp(s, ""))
{
int all = , cnt1 = ;
int len = strlen(s);
for (int i = ; i<len - ; i++, cnt1++)
all = all * + s[i] - '';
//all为 非循环节和循环节连起来的数
int mina = INF, minb = INF; //所求的分子与分母
for (int num = all / , cnt2 = cnt1 - ; cnt2 >= ; num /= , cnt2--)
{
//num为非循环节部分连起来的数 ,a为当前循环节下的分子,b为当前循环节下的分母
int a = all - num, b = q_pow(, cnt2)*(q_pow(, cnt1 - cnt2) - );
int g = gcd(a, b);
//求出分母最小的
if (b / g<minb)
{
minb = b / g;
mina = a / g;
}
}
printf("%d/%d\n", mina, minb);
}
return ;
}
 #include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>
#include <vector>
#include <map>
#include <set>
#include <string>
#include <cmath>
using namespace std;
const int INF=0x3f3f3f3f;
typedef long long ll;
int gcd(int n,int m)//求最大公约数
{
if(m==) return n; //n%m==0(n与m的余数为0)
return gcd(m,n%m);(n是大数,m是小数)
}
int main()
{
int all,num,l,m,n,a,b,k,mis,mns;
char str[];
while(gets(str)&&strcmp(str,""))
{
l=;all=;mis=INF;
for(int i=;str[i]!='.';i++)
{
all=all*+str[i]-;
l++;
}
num=all;
for(int j=;j<=l;j++)
{
num=num/;
a=all-num;
b=(int)pow(,l-j)*(pow(,j)-);
k=gcd(b,a);
if(b/k<mis)
{
mns=a/k;
mis=b/k;
}
}
printf("%d/%d\n",mns,mis);
}
return ;
}

poj 1930 Dead Fraction(循环小数化分数)的更多相关文章

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

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

  2. POJ 1930 Dead Fraction

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

  3. poj1930 Dead Fraction

    思路: 循环小数化分数,枚举所有可能的循环节,取分母最小的那个. 实现: #include <iostream> #include <cstdio> #include < ...

  4. UVA 10555 - Dead Fraction(数论+无限循环小数)

    UVA 10555 - Dead Fraction 题目链接 题意:给定一个循环小数,不确定循环节,求出该小数用分数表示,而且分母最小的情况 思路:推个小公式 一个小数0.aaaaabbb... 表示 ...

  5. HDU1717小数化分数2

    小数化分数2 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Subm ...

  6. POJ 1930

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

  7. uva 10555 - Dead Fraction)(数论)

    option=com_onlinejudge&Itemid=8&category=516&page=show_problem&problem=1496" st ...

  8. 【HDU】1717 小数化分数2 ——计数原理

    小数化分数2 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Subm ...

  9. HDU 1717 小数化分数2(最大公约数)

    小数化分数2 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Subm ...

随机推荐

  1. luogu P1017 进制转换

    感觉这个题 是真的恶心 本来单纯就递归写,发现好难 后来用数组记录 然后考虑 指数为 奇和偶数 分别 <0 和 > 进制的情况 其实 用进制数为3 大概讨论四种情况就可以了 由于最近就是在 ...

  2. 使用Python登陆QQ邮箱发送垃圾邮件 简单实现

    参考:Python爱好者 知乎文章 需要做的是: 1.邮箱开启SMTP功能 2.获取授权码 上述两步百度都有. 源码: #!/usr/bin/env python from email.mime.te ...

  3. Python学习札记(二十八) 模块1

    参考:模块 NOTE 1.模块:一个.py文件称为一个模块. 2.代码模块化的意义:a.提升程序的可维护性 b.不用重复造轮子 3.避免模块冲突,解决方法:引入了按目录来组织模块的方法,称为包(Pac ...

  4. UVa 1658 海军上将(最小费用最大流)

    https://vjudge.net/problem/UVA-1658 题意: 给出一个v个点e条边的有向加权图,求1~v的两条不相交(除了起点和终点外公共点)的路径,使得权和最小. 思路:把2到v- ...

  5. Eclipse.修改项目的JDK版本

    1.我实际使用过程中,只是修改了 项目右键-->Properties-->左侧选择"Java Compiler" -->右侧的"JDK Complian ...

  6. 2-12-配置squid代理服务器加快网站访问速度

    本节所讲内容: squid服务器常见概念 squid服务器安装及相关配置文件 实战:配置squid正向代理服务器 实战:配置透明squid代理提升访问速度 实战:配置squid反向代理加速度内网web ...

  7. Lua 中的 function、closure、upvalue

    Lua 中的 function.closure.upvalue function,local,upvalue,closure 参考: Lua基础 语句 lua学习笔记之Lua的function.clo ...

  8. LeetCode 46

    // 又是可以用回溯法做的一道题.class Solution { public: vector<vector<int>> permute(vector<int>& ...

  9. POJ 3279 Filptile dfs

    题目链接:http://poj.org/problem?id=3279 大意:给出一块n*m的棋盘.里面放满了棋子.有1和0两种状态.给出初始状态,翻动的时候会把当前位置和当前位置的上下左右共五个位置 ...

  10. Chrome浏览器插件开发-关于案例

    前言 关于案例 下一章 版本更新提示案例 一.前言 上章我们提到过开发一个插件所需要的步骤: Chrome浏览器插件开发-淘宝自动登录 并且还介绍了如何在页面上面注入脚本代码,并且成功的完成用户名和密 ...