1846 Circulator

Accept: 24    Submit: 97
Time Limit: 1000 mSec    Memory Limit : 32768 KB

Problem Description

Give you a integer N and a integer D,your task is to calculate N/d and print the decimal representation. If the decimal representation has a repeating sequence of digits, indicate the sequence by enclosing it in brackets. For example, 1/3 = .33333333...is denoted as 0.(3), and 41/333 = 0.123123123...is denoted as 0.(123). Use xxx.0 to denote an integer.

For example:

1/3=0.(3)

22/5=4.4

1/7=0.(142857)

2/2=1.0

3/8=0.375

45/56=0.803(571428)

Input

The input consists of several test cases. Each case contains a line with two positive numbers N and D. 1 <= N,D <= 100000

Output

For each case, output the decimal expansion, as detailed above. If the expansion exceeds 76 characters in length, print it on multiple lines with 76 characters per line.

Sample Input

1 7

Sample Output

0.(142857)

Source

FOJ有奖月赛-2009年10月——稚鹰翱翔
我的程序:
#include<iostream>
#include<cmath>
using namespace std;
const int maxn=100020;
int v[maxn];//存放余数,看是否能不能出现,要是出现就可以就停止,退出循环
int order[maxn];//记录从哪里开始循环
int ans[maxn];//存放结果
int solve(int n,int m,int &pos)
{
    memset(order,0,sizeof(order));
    memset(v,0,sizeof(v));
    int f=0;
    int cnt=0;
    v[n]=1;n*=10;
    while(n)
    {
        if(v[n%m]==1)
        {
            f=1;ans[++cnt]=n/m;n%=m;
            break;
        }
        else v[n%m]=1;
        ans[++cnt]=n/m;
        n%=m;
        order[n]=cnt;
        n*=10;
    }
    if(f==0)
        pos=-1;
    else pos=order[n];
    return cnt;
}
int get(int n)
{
    return log((double)n);
}
int main()
{
    int n,m;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        int cnt;
        int pos;
        if(n%m==0)
        {
            printf("%d.0/n",n/m);
            continue;
        }
        int zhengshu=n/m;
        n%=m;
        int len=solve(n,m,pos);
        if(zhengshu!=0)
             cnt=get(zhengshu)+1;
        else
            cnt=2;
        if(pos==-1)
        {
            printf("%d.",zhengshu);
            for(int i=1;i<=len;i++)
            {
                printf("%d",ans[i]);
                cnt++;
                if(cnt%76==0)
                    printf("/n");
            }
            if(cnt%76!=0)
                printf("/n");
           
        }
        else
        {
            printf("%d.",zhengshu);
            for(int i=1;i<=pos;i++)
            {
                printf("%d",ans[i]);
                cnt++;
                if(cnt%76==0)
                    printf("/n");
            }
            printf("(");
            cnt++;
            if(cnt%76==0)
                printf("/n");
            for(int j=pos+1;j<=len;j++)
            {
                printf("%d",ans[j]);
                cnt++;
                if(cnt%76==0)
                    printf("/n");
            }
            printf(")");
            cnt++;
            if(cnt%76==0)
                printf("/n");
            if(cnt%76!=0)
                printf("/n");
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值