poj 1930 Dead Fraction(循环小数化分数)
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 3478 | Accepted: 1162 |
Description
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
Output
Sample Input
0.2...
0.20...
0.474612399...
0
Sample Output
2/9
1/5
1186531/2500000
Hint
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(循环小数化分数)的更多相关文章
- POJ 1930 Dead Fraction (循环小数-GCD)
题意:给你一个循环小数,化成分数,要求分数的分母最小. 思路:暴力搜一遍循环节 把循环小数化分数步骤: 纯循环小数化分数 纯循环小数的小数部分可以化成分数,这个分数的分子是一个循环节表示的数,分母各位 ...
- POJ 1930 Dead Fraction
POJ 1930 Dead Rraction 此题是一个将无限循环小数转化为分数的题目 对于一个数 x=0.abcdefdef.... 假设其不循环部分的长度为m(如abc的长度为m),循环节的长度为 ...
- poj1930 Dead Fraction
思路: 循环小数化分数,枚举所有可能的循环节,取分母最小的那个. 实现: #include <iostream> #include <cstdio> #include < ...
- UVA 10555 - Dead Fraction(数论+无限循环小数)
UVA 10555 - Dead Fraction 题目链接 题意:给定一个循环小数,不确定循环节,求出该小数用分数表示,而且分母最小的情况 思路:推个小公式 一个小数0.aaaaabbb... 表示 ...
- HDU1717小数化分数2
小数化分数2 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Subm ...
- POJ 1930
Dead Fraction Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 1762 Accepted: 568 Desc ...
- uva 10555 - Dead Fraction)(数论)
option=com_onlinejudge&Itemid=8&category=516&page=show_problem&problem=1496" st ...
- 【HDU】1717 小数化分数2 ——计数原理
小数化分数2 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Subm ...
- HDU 1717 小数化分数2(最大公约数)
小数化分数2 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Subm ...
随机推荐
- reason: image not found的解决方案
在制作framework时遇到真机运行时导致的reason: image not found允许崩溃的问题,下面是我的解决方案: 首先我们分析一下出现这种情况的原因,原因就是framework找不到镜 ...
- Codeforces Round #408 (Div. 2) C. Bank Hacking
http://codeforces.com/contest/796/problem/C Although Inzane successfully found his beloved bone, Zan ...
- Android SDK manager 闪退
在SDK安装目录找到tools文件夹,点击进入找到android.bat右键编辑:找到如下指令: rem Check we have a valid Java.exe in the path. set ...
- [转]VS2015编译的程序在其他机器上缺少msvcp120.dll
http://www.lai18.com/content/1159618.html 1. 今天分享一个自己在开发过程中遇到的困难. 用VS2015开发了一个windows客户端(win32项目),在自 ...
- USB.资料
1.百度搜索 “usb java” 1.1.基于usb4java实现的java下的usb通信 - tomi_mint - 博客园.html(https://www.cnblogs.com/sowhat ...
- java中interrupt、join、sleep、notify、notifyAll、wait详解
首先介绍一下中断概念:举个例子容易理解一点 例子:假如你正在给朋友写信,电话铃响了.这时,你放下手中的笔,去接电话.通话完毕,再继续写信.这个例子就表现了中断及其处理过程:电话铃声使你暂时中止当前的工 ...
- Html中文字过多,单行超出和多行超出显示省略号
本博客主要介绍 前端开发中文本过多,以省略号显示. 效果如图: 单行: <!--单行--> <p class="pl">这个属 ...
- Python在七牛云平台的应用(一)
七牛云:(引用百度的介绍)七牛云是国内领先的企业级公有云服务商,致力于打造以数据为核心的场景化PaaS服务.围绕富媒体场景,七牛先后推出了对象存储,融合CDN加速,数据通用处理,内容反垃圾服务,以及直 ...
- Java网络编程和NIO详解7:浅谈 Linux 中NIO Selector 的实现原理
Java网络编程和NIO详解7:浅谈 Linux 中NIO Selector 的实现原理 转自:https://www.jianshu.com/p/2b71ea919d49 本系列文章首发于我的个人博 ...
- UVA-10600 ACM Contest and Blackout (次小生成树)
题目大意:给一张无向图,找出最小生成树和次小生成树. 题目分析:模板题...方法就是枚举所有的比最小生成树中两端点之间的最长边还要长的边,用它替换,再取一个最小的值便是次小生成树了. 代码如下: # ...