option=com_onlinejudge&Itemid=8&category=516&page=show_problem&problem=1496" style="">题目链接: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;
}

最新文章

  1. 函数find(exp)
  2. hp安装oracle报错解决
  3. 远程登录linux不用输入密码的方法
  4. CSS高级知识
  5. 【CodeVS】P1041 car的旅行路线
  6. MVC 项目中为什么会有两个web.config
  7. Android测试框架初步
  8. Linux系统入门学习:在CentOS上安装phpMyAdmin
  9. redis 多实例配置
  10. USACO Party Lamps 【Binary code solvution】【规律】
  11. SQL Server 后续去除功能汇总
  12. Linux安装JDK、MySQL和Tomcat
  13. 3D Slicer中文教程(四)—图像分割
  14. Python学习笔记五
  15. linux Boot目录满了之后的解决方法
  16. C++中引用传递与指针传递的区别
  17. spring boot mybatis没有扫描jar中的Mapper接口
  18. UVALive 6885 Flowery Trails 最短路
  19. SVN版控系统的安装和使用
  20. 从0开始整合SSM框架--2.spring整合mybatis

热门文章

  1. 探究Activity的各回调方法
  2. 加速器eaccelerator不兼容高版本php
  3. 用Hopper搞定Mac迅雷的会员以及离线下载功能
  4. linux术语解析(持续更新)
  5. js 去掉浏览器右击默认事件
  6. Python文件之----XML
  7. Hibernate中分页
  8. 使用Eclipse创建Hibernate工程
  9. 多重部分和的计数dp
  10. angularjs学习笔记三——directive