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 ...
随机推荐
- Phpstorm Alt+Enter 自动导入类
很方便!!!能够自动提示哪些类没有自动加载!!!然后Alt+Enter进行安装!!!
- JavaScript之搜索框
啧啧啧,又到月末了,时间过的真的好快啊︿( ̄︶ ̄)︿现在没课上,天天宅在寝室就这么三件事:吃饭睡觉打豆豆.真感无所事事,无聊至极!突然好怀念那些上课的日子啊!至少不像现在,生活状态全部都搅乱了:以前可 ...
- Linux常用命令--文件操作、权限设置
1.编辑文件 cat aaa.txt 查看aaa.txt文件的内容 head - aaa.txt 查看aaa.txt文件前5行的内容 tail - aaa.txt 展示aaa.txt文件最后10行的内 ...
- jquery.validate验证表单
添加引用 <script src="/${appName}/commons/js/validate/jquery.validate.min.js"></scrip ...
- hdu 1385 floyd字典序
Minimum Transport Cost Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/O ...
- UVA-140 Bandwidth (回溯+剪枝)
题目大意:求一个使带宽最小的排列和最小带宽.带宽是指一个字母到其相邻字母的距离最大值. 题目分析:在递归生成全排列的过程中剪枝,剪枝方案还是两个.一.当前解不如最优解优时,减去:二.预测的理想解不必最 ...
- plsql安装图解
Plsqldev安装步骤
- 057——VUE中vue-router之路由参数默认值的设置
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- iOS多线程GCD详解
在这之前,一直有个疑问就是:gcd的系统管理多线程的概念,如果你看到gcd管理多线程你肯定也有这样的疑问,就是:并发队列怎么回事,即是队列(先进先出)怎么会并发,本人郁闷了好久,才发现其实cgd管理多 ...
- Sublime 中文标题乱码
---title:Sublime 中文标题乱码--- #markdown语法(非Github Flavored) #解决办法: 在用户设置里添加一项,强制不根据 dpi 缩放dpi_scale: 1. ...