POJ 1930 Dead Fraction 数论
2017-05-21 15:48
363 查看
别人说这是小学奥数水平的题目,orz,作者受到了极大的打击。
如果是用笔写的话相信很多人都能将循环小数化成分数。
来个例子吧:0.333.....怎么化成分数呢?
设 x = 0.333......
10x = 3.33......
10x - x = 3
9x = 3
x = 1 / 3
和这个思路类似,对于一般情况,我们可以推导出一个公式(推导出了公式才方便编程)。
引用别人博客的一张图片(顺便引用原作者的一句话:这图片很不美观):http://blog.csdn.net/xinghongduo/article/details/6231107
相信读者看了一遍就会了吧。下面是ac代码:
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <math.h>
using namespace std;
typedef long long int ll;
const int MAX_SIZE = 20;
const int INF = 0x3f3f3f3f;
int GCD(int x, int y)
{
return !y ? x : GCD(y, x % y);
}
// 快速幂
int pow(int a, int n)
{
int res = 1;
while (n)
{
if (n & 1)
res *= a;
a *= a;
n >>= 1;
}
return res;
}
int main()
{
//freopen("test.txt", "r", stdin);
char str[MAX_SIZE];
while (~scanf("%s", str) && strcmp(str, "0"))
{
int all = 0, cnt1 = 0; // all代表abcdefg,cnt1代表all的位数,也就是k + c。
for (unsigned i = 2; i < strlen(str) - 3; i++, cnt1++)
all = all * 10 + str[i] - '0';
int mina = INF, minb = INF;
for (int num = all / 10, cnt2 = cnt1 - 1; cnt2 >= 0; num /= 10, cnt2--) // num代表abcd,cnt2代表num的位数,也就是c。
{
int a = all - num, b = pow(10, cnt2) * (pow(10, cnt1 - cnt2) - 1);
int g = GCD(a, b);
if (b / g < minb)
{
minb = b / g;
mina = a / g;
}
}
printf("%d/%d\n", mina, minb);
}
return 0;
}
如果是用笔写的话相信很多人都能将循环小数化成分数。
来个例子吧:0.333.....怎么化成分数呢?
设 x = 0.333......
10x = 3.33......
10x - x = 3
9x = 3
x = 1 / 3
和这个思路类似,对于一般情况,我们可以推导出一个公式(推导出了公式才方便编程)。
引用别人博客的一张图片(顺便引用原作者的一句话:这图片很不美观):http://blog.csdn.net/xinghongduo/article/details/6231107
相信读者看了一遍就会了吧。下面是ac代码:
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <math.h>
using namespace std;
typedef long long int ll;
const int MAX_SIZE = 20;
const int INF = 0x3f3f3f3f;
int GCD(int x, int y)
{
return !y ? x : GCD(y, x % y);
}
// 快速幂
int pow(int a, int n)
{
int res = 1;
while (n)
{
if (n & 1)
res *= a;
a *= a;
n >>= 1;
}
return res;
}
int main()
{
//freopen("test.txt", "r", stdin);
char str[MAX_SIZE];
while (~scanf("%s", str) && strcmp(str, "0"))
{
int all = 0, cnt1 = 0; // all代表abcdefg,cnt1代表all的位数,也就是k + c。
for (unsigned i = 2; i < strlen(str) - 3; i++, cnt1++)
all = all * 10 + str[i] - '0';
int mina = INF, minb = INF;
for (int num = all / 10, cnt2 = cnt1 - 1; cnt2 >= 0; num /= 10, cnt2--) // num代表abcd,cnt2代表num的位数,也就是c。
{
int a = all - num, b = pow(10, cnt2) * (pow(10, cnt1 - cnt2) - 1);
int g = GCD(a, b);
if (b / g < minb)
{
minb = b / g;
mina = a / g;
}
}
printf("%d/%d\n", mina, minb);
}
return 0;
}
相关文章推荐
- poj 1930 Dead Fraction 混循环小数化分数 数论
- POJ - 1930 Dead Fraction 无限循环小数化分数(数学小知识)
- Dead Fraction POJ - 1930
- POJ 1930 Dead Fraction (小数化分数)
- POJ 1930 Dead Fraction(循环小数化分数)
- Dead Fraction POJ - 1930
- POJ 1930 Dead Fraction 笔记
- POJ 1930 Dead Fraction(小数化分数)
- POJ -1930 Dead Fraction (GCD+循环小数化分数)
- POJ 1930 Dead Fraction (无限循环小数转化为分数)
- POJ - 1930 Dead Fraction(简单数学推理)
- POJ 1930 Dead Fraction
- POJ 1930 Dead Fraction(循环小数转换成分数)
- POJ 1930 Dead Fraction
- POJ 1930 Dead Fraction (循环小数-GCD)
- POJ - 1930 Dead Fraction(数学精度计算)
- Mathematics:Dead Fraction(POJ 1930)
- poj 1930 Dead Fraction 数学 分数 无限循环小数
- POJ 1930 - Dead Fraction(数学)
- POJ - 1930 Dead Fraction(将小数化为分数)