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. UVa 11489 整数游戏

    https://vjudge.net/problem/UVA-11489 题意: 给出一个数字串n,两个人轮流从中取出一个数字,要求每次取完之后剩下的数是3的倍数,不能取数者输. 思路: 要想取掉一个 ...

  2. Codeforces Round #275 (Div. 2) A,B,C,D

    A. Counterexample time limit per test 1 second memory limit per test 256 megabytes input standard in ...

  3. python 执行字符串中的python代码

    mycode = 'print("hello world")' code = """ def mutiply(x,y): return x*y pri ...

  4. SpringBoot创建多模块方式以及打包方式

    springboot重构多模块的步骤 模型层:model 持久层:persistence 表示层:web 步骤: 正常创建一个springboot项目 修改创建项目的pom文件,将jar修改为pom ...

  5. Rails 5 Test Prescriptions 第14章 Testing Exteranl Services(中断。)

    external testing strategy ✅ the service integration test✅ introduce VCR✅ Client Unit Tests ❌ Why an ...

  6. linux下给cpu加压

    计算pi: time (echo "scale=500;4*a(1)"|bc -l -q) #!/bin/bashfor i in `seq 1 1000`do    (time ...

  7. 2016 CCPC Hangzhou Onsite

    A:题意:n个格子排成一排,每个a[i],要求重排成k个,每个人数相同,合并两个和划分成两个(可以不等)都是花费为1,问最小花费 题解:从前往后贪心即可,由于哪个地方忘开ll,wa了,全改成ll就过了 ...

  8. 控制反转(IOC)模式

    控制反转(Inversion of Control):提倡实现松耦合层.组件和类的设计原则,颠倒程序的控制流程.IoC使用分离执行特定问题处理代码的概念: IoC意味着将你设计好的对象交给容器控制,而 ...

  9. 谈一谈手机WebApp的fixed属性(手机上的固定栏)【转】

    1.iphone/android原生app常见结构 似乎,所有的手机应用,都遵循这样的布局:固定的顶部+固定的底部+可滚动在中间区域.这种“雷同”的模式让人恶心,却不得不承认这是一种很规矩却又很实用的 ...

  10. 【转】Javascript中的this

    作者: 阮一峰 日期: 2010年4月30日 this是Javascript语言的一个关键字. 它代表函数运行时,自动生成的一个内部对象,只能在函数内部使用.比如, function test(){ ...