uva 10555 - Dead Fraction)(数论)
2014-07-06 00:43
197 查看
题目链接:uva 10555 - Dead Fraction
题目大意:给出一个小数,从...开始可以是任何数字,但是保证是无限循环小数,将该小数用分式的形式表示,并且要求分母尽量大。
解题思路:这题主要是怎么将无限循环小数转换成分式,这样的:
有小数0.abcdEEE,未循环部分长4,循环节为E,E的长度为i(假设)
abcd+E999…(i位9)10i
题目大意:给出一个小数,从...开始可以是任何数字,但是保证是无限循环小数,将该小数用分式的形式表示,并且要求分母尽量大。
解题思路:这题主要是怎么将无限循环小数转换成分式,这样的:
有小数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(数论+无限循环小数)
- uva 10539 - Almost Prime Numbers(数论)
- UVA 1426 - Discrete Square Roots(数论)
- UVA 10140 - Prime Distance(数论)
- UVA 10627 - Infinite Race(数论)
- 数论uva575-Skew Binary
- UVA 10168 Summation of Four Primes(数论)
- UVA 10042 Smith Numbers(数论)
- uva 1434 - YAPTCHA(数论)
- UVA 10042 Smith Numbers 数论
- uva11404(数论_三角形计数)
- UVa 160 Factors and Factorials (数论)
- 数论寻找等差素数UVA 10650
- UVALive_5110_Square-Free Numbers 数论?水题?
- UVALive 5964 LCM Extreme(数论 欧拉函数)
- UVA 1319 - Maximum(数论+贪心)
- UVa 10692 - Huge Mod (数论 + 欧拉定理扩展)
- UVA 11426 GCD - Extreme (II) (数论|欧拉函数)