uva 10555 - Dead Fraction)(数论)
题目大意:给出一个小数,从...開始能够是不论什么数字,可是保证是无限循环小数。将该小数用分式的形式表示,而且要求分母尽量大。
解题思路:这题主要是怎么将无限循环小数转换成分式,这种:
- 有小数0.abcdEEE,未循环部分长4。循环节为E,E的长度为i(如果)
- abcd+E999…(i位9)10i
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
const int maxn = 105;
const ll INF = 0x3f3f3f3f3f3f3f;
char s[maxn];
ll gcd (ll a, ll b) {
return b ?
gcd(b, a%b) : a;
}
int main () {
while (scanf("%s", s) == 1 && strcmp(s, "0")) {
int len = strlen(s)-5;
ll ansu, ansd = INF;
for (int i = 0; i < len; i++)
s[i] = s[i+2];
for (int i = 0; i < len; i++) {
ll d = 1, u = 0;
for (int j = 0; j < i; j++) {
d = d * 10;
u = u * 10 + s[j] - '0';
}
ll x = 0, y = 0;
for (int j = i; j < len; j++) {
x = x * 10 + s[j] - '0';
y = y * 10 + 9;
}
d = d * y;
u = u * y + x;
ll g = gcd(d, u);
u /= g;
d /= g;
if (d < ansd) {
ansd = d;
ansu = u;
}
}
printf("%lld/%lld\n", ansu, ansd);
}
return 0;
}
uva 10555 - Dead Fraction)(数论)的更多相关文章
- UVA 10555 - Dead Fraction(数论+无限循环小数)
UVA 10555 - Dead Fraction 题目链接 题意:给定一个循环小数,不确定循环节,求出该小数用分数表示,而且分母最小的情况 思路:推个小公式 一个小数0.aaaaabbb... 表示 ...
- poj 1930 Dead Fraction(循环小数化分数)
Dead Fraction Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 3478 Accepted: 1162 Des ...
- UVA 10627 - Infinite Race(数论)
UVA 10627 - Infinite Race option=com_onlinejudge&Itemid=8&page=show_problem&category=516 ...
- uva 10560 - Minimum Weight(数论)
题目连接:uva 10560 - Minimum Weight 题目大意:给出n,问说至少须要多少个不同重量的砝码才干称量1~n德重量,给出所选的砝码重量,而且给出k,表示有k个重量须要用上述所选的砝 ...
- POJ 1930 Dead Fraction
POJ 1930 Dead Rraction 此题是一个将无限循环小数转化为分数的题目 对于一个数 x=0.abcdefdef.... 假设其不循环部分的长度为m(如abc的长度为m),循环节的长度为 ...
- UVA 11754 - Code Feat(数论)
UVA 11754 - Code Feat 题目链接 题意:给定一个c个x, y1,y2,y3..yk形式,前s小的答案满足s % x在集合y1, y2, y3 ... yk中 思路:LRJ大白例题, ...
- UVA 718 - Skyscraper Floors(数论)
UVA 718 - Skyscraper Floors 题目链接 题意:在一个f层高的楼上,有e个电梯,每一个电梯有x,y表示y + k * x层都能够到,如今要问从a层是否能到达b层(中间怎么换乘电 ...
- uva 10692 - Huge Mods(数论)
题目链接:uva 10692 - Huge Mods 题目大意:给出一个数的次方形式,就它模掉M的值. 解题思路:依据剩余系的性质,最后一定是行成周期的,所以就有ab=abmod(phi[M])+ph ...
- UVA 12009 - Avaricious Maryanna(数论)
UVA 12009 - Avaricious Maryanna 题目链接 题意:给定一个n.求出n个数位组成的数字x,x^2的前面|x|位为x 思路:自己先暴力打了前几组数据,发现除了1中有0和1以外 ...
随机推荐
- Android 笔记
一.MTK Android version在文件下build/core/version_defaults.xml下定义. 二.Android 重新编译frameworks/base/core/res资 ...
- Linux filesystem structures.
1. / – Root Every single file and directory starts from the root directory. Only root user has write ...
- ORACLE 解析xml字符串-转载的
--------------------------方法一------------------------------------- 1.xml字符串 /* <orderlist> ...
- iOS9 中的一些适配问题
1.URL scheme白名单:在info文件中加入LSApplicationQueriesSchemes(Array),添加需要的scheme,如微信:weixin.wechat 支付宝:alipa ...
- 4.HTTP入门
什么是http协议查看http协议的工具http协议内容Http请求请求行http协议版本请求资源请求方式GET方式提交POST方式提交请求头3.3 实体内容3.4 HttpServletReques ...
- QQ情侣头像~
- How to scroll the window using JQuery $.scrollTo() function
$('html, body').animate({scrollTop: $("#page").offset().top}, 2000); http://stackoverflow. ...
- 用Guava辅助Throwable异常处理
Guava的 Throwables 工具常常可以让exception处理更方便. Propagation 有时候,你会想把捕获的exception抛到上一个try/catch块.对于 RuntimeE ...
- ubuntu10.04版本下android源码的编译
首先是网址:http://software.intel.com/en-us/blogs/2012/03/06/hands-on-notesbuild-android-x86-ics-4-virtual ...
- 正确设置网站title、keywords、description(转载)
本文转载自蚂蚁HR(www.mayihr.com) 优化技巧是老师在课堂上教不了你的,而自己也不可能在练习中领悟,最便捷的方法就是听取别人的经验,所以转载一下 1.title(网站标题) title, ...