POJ 1930 Dead Fraction
2016-04-10 09:53
183 查看
要输出分母最小的分数。
所以需要枚举一下哪一个位置是循环的起点。
所以需要枚举一下哪一个位置是循环的起点。
#include<cstdio> #include<cstring> #include<cmath> #include<queue> #include<vector> #include<algorithm> using namespace std; long long a,b; long long f1,f2; long long ans1,ans2; char s[1000]; long long gcd(long long a, long long b) { if (a%b == 0) return b; return gcd(b, a%b); } int main() { while(1) { scanf("%s",s); int len=strlen(s); if(len==1&&s[0]=='0') break; ans2=99999999999999; int pos; for(int i=len-1;i;i--) if(s[i]!='.'){pos=i;break;} for(int i=2;i<=pos;i++) { a=b=0; f1=1; f2=0; for(int j=2;j<=i-1;j++) { a=a*10+(long long)(s[j]-'0'); f1=f1*10; } for(int j=i;j<=pos;j++) { b=b*10+(long long)(s[j]-'0'); f2=f2*10+9; } f2=f2*f1; long long fz=a*f2+b*f1; long long fm=f1*f2; if(fm/gcd(fz,fm)<ans2) { ans1=fz/gcd(fz,fm); ans2=fm/gcd(fz,fm); } } printf("%lld/%lld\n",ans1,ans2); } return 0; }
相关文章推荐
- Ubuntu14.04搭建PXE服务器过程
- ym—— Android 5.0学习之ListView升级版RecyclerView
- Java中hashCode的作用
- LeetCode 227. Basic Calculator II
- 矢量数据存入postgis中
- [设计模式]——静态代理
- C++实现队列
- 模板2
- 第一天:Java源码级实战速成(通过动手实战类、对象等,通过Spark和Hadoop案例代码和源码解析具体指知识的应用、深度详解匿名接口在Spark开发中的运用)
- 签到题 2016.4.10
- Plotting means and error bars (ggplot2)
- 使用AmplifyJS和JQuery编写更好更优雅的javascript事件处理代码
- java中的运算符
- java实现文件导入导出
- IOS NSInvocation应用与理解
- 20160410javaweb之JDBC---DBUtils框架
- java常见运行时错误
- FOJ Common Tangents
- test
- php_D3_“简易聊天室 ”实现的关键技术 详解