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. [微信开发] - 从最新的appid,appsecret读取配置信息

    设置好form表单,填写参数传入Java后端做为实例bean,接着存储倒数据库. 当微信端接口配置提交时,Java接口从数据库获取最新的配置信息,可以根据increaseID,也可以设置时间段, 这里 ...

  2. git status出现 fatal: Not a git repository (or any of the parent directories): .git

    fatal: Not a git repository (or any of the parent directories): .git 提示说没有.git这样一个目录,解决办法如下: git ini ...

  3. 使用 while 循环

    与 for 循环不同,while 循环会一直运行,直到给定条件不满足为止.例如,下面的 while 循环从 x=0 开始.每次循环都会检查 x<=10 是否成立.如果成立,就执行循环内部的表达式 ...

  4. AtomicLong可以被原子地读取和写入的底层long值的操作

    java.util.concurrent.atomic.AtomicLong类提供了可以被原子地读取和写入的底层long值的操作,并且还包含高级原子操作. AtomicLong支持基础long类型变量 ...

  5. hadoop 集群配置--增加减少新的机器不重启

    增加机器不重启操作如下: 首先,把新节点的 IP或主机名 加入主节点(master)的 conf/slaves 文件. 然后登录新的从节点,执行以下命令: $ cd path/to/hadoop $ ...

  6. 改变VO中的sql

    cuxOptionVO.setFullSqlMode(cuxOptionVO.FULLSQL_MODE_AUGMENTATION); cuxOptionVO.setQuery(null); cuxOp ...

  7. IOS-多线程(NSOperation)

    一.基础用法 // // ViewController.m // IOS_0120_NSOperation // // Created by ma c on 16/1/20. // Copyright ...

  8. 002PHP文件处理——文件处理 is_dir mkdir getcwd chdir rmdir

    <?php /** * 66 文件处理 is_dir mkdir getcwd chdir rmdir */ //is_dir() 判断一个目录是否存在 //var_dump(is_dir('6 ...

  9. LRY_FX_Assist(辅助EA)

    功能说明 辅助EA就是别的EA没有功能用这个EA来弥补,比如说风控设置(预付款.浮亏.加仓层数等达到多少进行操作),移动止损(包括隐藏移动止损),启动马丁加仓等.这个EA不能自己独立开单,只能辅助其它 ...

  10. OMAP4之DSP核(Tesla)软件开发学习(一)

    目的:       目前手上正在OMAP4上做东西,由于涉及到大量运算,交给arm A9双核发现运算速度很慢,不能满足需求.故考虑将大量运算任务(比如FIR.FFT.卷积.图像处理.向量运算等)交给O ...