POJ 1930 Dead Fraction (小数化分数)
来源:互联网 发布:ssh怎么指定端口号 编辑:程序博客网 时间:2024/05/14 23:35
Dead Fraction
Time Limit: 1000MS Memory Limit: 30000KTotal Submissions: 2080 Accepted: 661
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).
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/91/51186531/2500000
- - 话说真不会这东西~ 然后百度一发 - - 瞬间 这题真水。。。。
例:把混循环小数0.123˙68˙化成分数:
解:0.123˙68˙=(0.12368+0.00000˙68˙)
=(12368/100000)+(68/9900000)
=[(12368/99000)-(12368/990000)]+(68/9900000)
=(12368/99000)+[(68/9900000)-(12368/9900000)]
=(12368/99000)-(12300/9900000)
=(12368-123)/99000
公式
用9和0做分母,首先有一个循环节有几位数字就几个9,接着有几个没加入循环的数就加几个0,再用第二个循环节以前的小数部分组成的数与小数部分中不循环部分组成的数的差做分子,比如0.43,3的循环,有一位数没加入循环,就在9后面加一个0做分母,再用43减4做分子,得 90分之39,0.145,5的循环就用9后面加2个0做分母,再用145减14做分子,得900分之131,0.549,49的循环,就 用99后面加1个0做分母,用549减5做分子,最后得990分之545,以此类推,能约分的要化简。
有个坑点 就是题目说不会全0 但是事实上 有全0的 case。。。。
AC代码如下:
//// POJ 1930 Dead Fraction//// Created by TaoSama on 2015-03-24// Copyright (c) 2015 TaoSama. All rights reserved.//#include <algorithm>#include <cctype>#include <cmath>#include <cstdio>#include <cstdlib>#include <cstring>#include <iomanip>#include <iostream>#include <map>#include <queue>#include <string>#include <set>#include <vector>#define CLR(x,y) memset(x, y, sizeof(x))using namespace std;const int INF = 0x3f3f3f3f;const int MOD = 1e9 + 7;const int N = 1e5 + 10;string s;int pow(int n) { int ret = 1; for(int i = 1; i <= n; ++i) ret *= 10; return ret;}int main() {#ifdef LOCAL freopen("in.txt", "r", stdin);//freopen("out.txt","w",stdout);#endif ios_base::sync_with_stdio(0); while(cin >> s) { if(s == "0") break; s = s.substr(2, s.size() - 5); int n = atoi(s.c_str()), sz = s.size(); if(n == 0) cout << "0/1" << endl; else { int ansx, ansy = INF; for(int i = 1; i <= sz; ++i) { string xx = s.substr(0, sz - i); int x = n - atoi(xx.c_str()); int y = pow(sz) - pow(sz - i); int gcd = __gcd(x, y); x /= gcd; y /= gcd; if(y < ansy) ansx = x, ansy = y; } cout << ansx << '/' << ansy << endl; } } return 0;}
0 0
- POJ 1930 Dead Fraction(小数化分数)
- POJ 1930 Dead Fraction (小数化分数)
- POJ -1930 Dead Fraction (GCD+循环小数化分数)
- poj 1930 Dead Fraction 混循环小数化分数 数论
- xtu 1236 Fraction(小数化分数)
- XTU 1236 Fraction(小数化分数)
- POJ 1930 Dead Fraction(循环小数转换成分数)
- POJ 1930 Dead Fraction
- POJ-1930-Dead Fraction
- POJ 1930Dead Fraction
- POJ 1930 - Dead Fraction(数学)
- POJ-1930 Dead Fraction(简单数论)
- poj 1930 无限小数化分数
- POJ 1930 Dead Fraction【数学】
- POJ 1930 Dead Fraction 数论
- POJ 1930 Dead Fraction (无限循环小数转化为分数)
- POJ 1930 Dead Fraction 已翻译
- poj 1930 Dead Fraction 循环小数的处理
- C++友元函数
- QQ通信原理及QQ是怎么穿透内网进行通信的?
- MYSQL5.1升级到5.5及utf8mb4使用方法
- HDU 1698 Just a Hook(线段树区间更新)
- 十种排序方法总结
- POJ 1930 Dead Fraction (小数化分数)
- js 练习小知识点
- HDU 4635 - Strongly connected(强连通)
- 【Access2003】表的新建、用sql语句查询;关闭警告信息;修改数据库密码;修复数据库
- 关联查询SQL的一次优化过程
- Java生成定长随机字符串
- 5-1简述智能卡的发展,以及射频电子标签在其中所处的位置
- 顺序表创建插入删除操作及实现
- 文章标题