思路:

循环小数化分数,枚举所有可能的循环节,取分母最小的那个。

实现:

 #include <iostream>
#include <cstdio>
#include <string>
using namespace std; const int INF = 0x3f3f3f3f; int gcd(int a, int b)
{
return !b ? a : gcd(b, a % b);
} int lcm(int a, int b)
{
return a / gcd(a, b) * b;
} int toNum(string s)
{
int n = s.length();
int res = ;
for (int i = ; i < n; i++)
{
res += s[i] - '';
if (i != n - )
res *= ;
}
return res;
} int main()
{
string s;
while (cin >> s, s != "")
{
s = s.substr(, s.length() - );
int n = s.length();
int minn = INF;
int minm = ;
for (int i = ; i < n; i++)
{
int x = toNum(s.substr(i, n - i));
int y = ;
for (int j = ; j < n - i - ; j++)
{
y *= ;
y += ;
}
for (int j = ; j < i; j++)
{
y *= ;
}
int p = toNum(s.substr(, i));
int q = ;
for (int j = ; j < i; j++)
{
q *= ;
}
int l = lcm(y, q);
int tmp = l / y * x + l / q * p;
int g = gcd(tmp, l);
tmp /= g;
l /= g;
if (l < minn)
{
minn = l;
minm = tmp;
}
}
cout << minm << "/" << minn << endl;
}
return ;
}

poj1930 Dead Fraction的更多相关文章

  1. uva 10555 - Dead Fraction)(数论)

    option=com_onlinejudge&Itemid=8&category=516&page=show_problem&problem=1496" st ...

  2. poj 1930 Dead Fraction(循环小数化分数)

    Dead Fraction Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 3478   Accepted: 1162 Des ...

  3. UVA 10555 - Dead Fraction(数论+无限循环小数)

    UVA 10555 - Dead Fraction 题目链接 题意:给定一个循环小数,不确定循环节,求出该小数用分数表示,而且分母最小的情况 思路:推个小公式 一个小数0.aaaaabbb... 表示 ...

  4. Dead Fraction [POJ1930]

    题意: 很有意思的一道题,,将一个无限循环小数转化成分母最小的精确分数值....,循环的部分不一定是最后一位. Sample Input 0.2... 0.20... 0.474612399... 0 ...

  5. POJ 1930 Dead Fraction

    POJ 1930 Dead Rraction 此题是一个将无限循环小数转化为分数的题目 对于一个数 x=0.abcdefdef.... 假设其不循环部分的长度为m(如abc的长度为m),循环节的长度为 ...

  6. Mathematics:Dead Fraction(POJ 1930)

    消失了的分式 题目大意:某个人在赶论文,需要把里面有些写成小数的数字化为分式,这些小数是无限循环小数(有理数),要你找对应的分母最小的那个分式(也就是从哪里开始循环并不知道). 一开始我也是蒙了,这尼 ...

  7. POJ 1930 Dead Fraction (循环小数-GCD)

    题意:给你一个循环小数,化成分数,要求分数的分母最小. 思路:暴力搜一遍循环节 把循环小数化分数步骤: 纯循环小数化分数 纯循环小数的小数部分可以化成分数,这个分数的分子是一个循环节表示的数,分母各位 ...

  8. poj1930 数论

    Dead Fraction Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 1258   Accepted: 379 Desc ...

  9. POJ 1930

    Dead Fraction Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 1762   Accepted: 568 Desc ...

随机推荐

  1. Android签名机制之---签名验证过程具体解释

    一.前言 今天是元旦,也是Single Dog的嚎叫之日,仅仅能写博客来祛除寂寞了,今天我们继续来看一下Android中的签名机制的姊妹篇:Android中是怎样验证一个Apk的签名. 在前一篇文章中 ...

  2. UIButton的图片和文字相对位置调整

    通常.假设直接设置UIButton的图片和文字,默认的两者相对位置可能不是我们想要的,那么须要进行调整. 须要用到的函数例如以下: UIEdgeInsetsMake(CGFloat top, CGFl ...

  3. JavaScript操作符(关系操作符、相等操作符和条件操作符)

    关系操作符用于对两个值进行比较,返回一个布尔值.关系操作符包括大于(>),小于(<),大于等于(>=),小于等于(<=).当关系操作符用于非数值时,也要先进行数值的转换.如 v ...

  4. Activity动态添加Fragment时遇到的问题

    1.Activity动态调用代码 TitleFragement a = new TitleFragement();        getFragmentManager().beginTransacti ...

  5. c/c++内存使用原则

    1 no malloc no free 2 no new no delete 如果对象不是new出来的,那么这个对象在生命周期结束后会自动调用析构函数自己释放自己的内存,不需要delete. 但是如果 ...

  6. Calling a parent window function from an iframe

    I want to call a parent window JavaScript function from an iframe. <script>function abc(){ ale ...

  7. POJ 1269 Intersecting Lines(线段相交,水题)

    id=1269" rel="nofollow">Intersecting Lines 大意:给你两条直线的坐标,推断两条直线是否共线.平行.相交.若相交.求出交点. ...

  8. Error CREATEing SolrCore 'new_core': Unable to create core [new_core] Caused by: Can't find resource 'solrconfig.xml' in classpath or 'D:\solr\solr-7.2.1\server\solr\new_core'

    \solr-7.2.1\server\solr\configsets\_default  下的conf  复制到:   \solr-7.2.1\server\solr\new_core

  9. bleve搜索引擎源码分析之索引——mapping真复杂啊

    接下来看看下面index部分的源码实现: data := struct { Name string Des string }{ Name: "hello world this is bone ...

  10. BZOJ4561: [JLoi2016]圆的异或并 计算几何+treap

    因为本题保证两圆之间只有相包含或相离(不用担心两圆重合 因为我没有RE) 所以每个圆之间的相对位置是确定的  也就是可以按极角排序的, 所以可以按横坐标排序后 扫描同时用treap维护加圆删圆(即遇到 ...