poj1930 数论
2024-08-30 13:38:25
Dead Fraction
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 1258 | Accepted: 379 |
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/9
1/5
1186531/2500000 题意:最后一位表示循环节,
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm> #define inf 1000000000
#define ll long long using namespace std;
char ch[];
ll ans1,ans2;
ll gcd(ll a,ll b)
{
return b==?a:gcd(b,a%b);
}
void solve(ll a,ll b,ll c,ll d)
{
ll t1=a*d+b*c,t2=b*d,t=gcd(t1,t2);
t1/=t;t2/=t;
if(t2<ans2)ans2=t2,ans1=t1;
}
int main()
{
while(~scanf("%s",ch+))
{
ans2=(ll)1e60;
int n=strlen(ch+);
if(n==)break;
ll b=,a=;
for(int i=;i<=n-;i++)
a=a*+ch[i]-'',b*=;//三个.
ll t=b/*;
for(ll i=;i<=b;i*=,t=t+(b/i)*)
solve(a/i,b/i,a%i,t);
printf("%lld/%lld\n",ans1,ans2);
}
}
最新文章
- python简单搭建HTTP Web服务器
- css 超过宽度显示...
- Value must be an existing directory配置tomcat问题
- 在项目中那个少用if else 语句,精简代码,便于维护的方法(1)
- 第六章:Javascript对象
- Android设计模式(1)----单例模式
- powershell创建并加载配置文件
- iOS iOS8新特性--UIPopoverPresentationController
- Unix中$$、$@、$#、$*的意思
- Mysql 权限修改何时生效
- C语言文件操作函数大全
- nefu 197 关键字检索(kmp算法)
- 【转】jqGrid学习之安装
- TCP 和 UDP
- 15 Actionbar的显示和隐藏
- SQL语句练习题【主供自己学习、记忆】
- Chromium Embedded Framework (CEF)_3.2171.2069_v20170606_x86.tar.xz
- Unity 思考问题的办法
- svn解决不能clean的方法
- PAT 1097 Deduplication on a Linked List[比较]