【u237】分数化小数

Time Limit: 1 second
Memory Limit: 128 MB

【问题描述】

写一个程序,输入一个形如N/D的分数(N是分子,D是分母),输出它的小数形式。如果小数有循环节的话,把循环节放在一个对
括号中,例如:
1/3=0.33333333 写成0.(3)
41/333=0.123123123 写成0.(123)
用xxx.0表示整数
典型的转化例子:
1/3=0.(3)
22/5=4.4
22/5=4.4
2/2=1.0
3/8=0.375
45/46=0.803(571428)
【输入格式】

单独的一行包括被空格分开的N和D,1<=N,D<=100000。

【输出格式】

输出一行小数的表示方法上面已经说得很明白了。

【数据规模】

Sample Input1

45 56

Sample Output1

0.803(571428)

【题目链接】:http://noi.qz5z.com/viewtask.asp?id=u237

【题解】

一下内容摘抄自网络

/*
22 / 7 = 3.142857142857142857...
如果我们细心观察一下22/7的演算过程,我们便会明白为何这分数必然是循环的。在计算22/7的第一步骤中,我们先得商3和余数1。接着我们把余数1倍大为10,然后计算10/7,得商1和余数3。接着我们把余数3倍大为30 ,然后计算30/7,得商4和余数2。接着我们又把余数2倍大为20,然后计算20/7......如此类推。因此,在计算 22/7时,我们实际上是在不断做10/7或20/7或30/7...。可是,由于任何数除以7所得的余数只有7种可能(即0、 1、2、3、4、5和6)(注2),这样下去必然会重复出现之前的计算。当出现重复时,接着下来的计算便会跟之前做过的计算一模一样,因而出现循环小数的情况。例如,在22/7的运算中,当计算至小数点后第6位时,得商7 和余数1,接着我们把余数1倍大为10,然后计算10/7,但是此一计算在之前已做过,接下来的计算结果必然是 142857,因此22/7必然是不断重复出现142857这组数字的循环小数。 
*/


所以我们进行d(分子)次模拟手算除法的过程就肯定能够找到余数的循环节;(不能直接在结果里找循环节);
然后输出两个循环节之间的数就好;
要注意输出1/2=0.5的情况;控制输出下就好;(能整除就直接输出.0吧)

【完整代码】

#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <set>
#include <map>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <queue>
#include <vector>
#include <stack>
#include <string>
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pp push_back
#define fi first
#define se second

using namespace std;

typedef pair<int,int> pii;
typedef pair<LL,LL> pll;

void rel(LL &r)
{
    r = 0;
    char t = getchar();
    while (!isdigit(t) && t!='-') t = getchar();
    LL sign = 1;
    if (t == '-')sign = -1;
    while (!isdigit(t)) t = getchar();
    while (isdigit(t)) r = r * 10 + t - '0', t = getchar();
    r = r*sign;
}

void rei(int &r)
{
    r = 0;
    char t = getchar();
    while (!isdigit(t)&&t!='-') t = getchar();
    int sign = 1;
    if (t == '-')sign = -1;
    while (!isdigit(t)) t = getchar();
    while (isdigit(t)) r = r * 10 + t - '0', t = getchar();
    r = r*sign;
}

const int MAXN = 1e5+10;
const int dx[5] = {0,1,-1,0,0};
const int dy[5] = {0,0,0,-1,1};
const double pi = acos(-1.0);

LL n,d;
int bo[MAXN];
LL a[MAXN];

int main()
{
    //freopen("F:\rush.txt","r",stdin);
    cin >> n >> d;
    memset(bo,0,sizeof(bo));
    printf("%d.",n/d);
    if (n%d==0)
        puts("0
");
    else
    {
        LL now = n%d;
        int l = 1,r = 1;
        bool find = false;
        rep1(i,1,d)
        {
            r = i;
            bo[now] = i;
            now = now*10;
            a[i] = now/d;
            now %= d;
            if (bo[now])
            {
                r = i;
                l = bo[now];
                find = true;
                break;
            }
        }
        rep1(i,1,l-1)
            cout << a[i];
        if (now!=0) cout << "(";
        if (now!=0)
            rep1(i,l,r)
                cout << a[i];
        if (now!=0) cout << ")";
    }
    return 0;
}
原文地址:https://www.cnblogs.com/AWCXV/p/7626938.html