usaco题目分享——Fractions to Decimals

  

Fractions to Decimals

Write a program that will accept a fraction of the form N/D, where N is the numerator and D is the denominator 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. Typical conversions are:

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)

PROGRAM NAME: fracdec

INPUT FORMAT

A single line with two space separated integers, N and D, 1 <= N,D <= 100000.

SAMPLE INPUT (file fracdec.in)

45 56

OUTPUT FORMAT

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 OUTPUT (file fracdec.out)

0.803(571428)



就是这样一道比较简单的题目,是usaco上第二章的最后一道题目。(因为最近老师布置了usaco上的题目......如果看英文有问题,可以去nocow上看)
简单来说,这道题目就是一道类似高精小数的题目,只需要模拟除法就可以了。
但是为什么要推荐呢?因为特判和特殊情况很多......还有就是76个字符提一行。
可以试试自己做题考虑是否周到,能一次AC是很不容易的......
下面贴代码(我的代码很丑,自己参考)
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<map>
using namespace std;
int n,m,zhen;
map <int,int> f;
struct like
{
    int w[100005];
}ans;
int wa[10005],wo;
void chu(int x,int y)
{
    int i,j;
    zhen=x/y;
    x=x%y;
    f[x]=1;
    int t=1,anss=0;
    while(x!=0)
    {
        t++;
        x=x*10;
        ans.w[0]++;
        ans.w[ans.w[0]]=x/y;
        x=x%y;
        if(f[x]!=0)
        {
            anss=f[x];
            break;
        }
        f[x]=t;
    }
    int p=0;
    while(zhen>0)
    {
        wo++;
        wa[wo]=zhen%10;
        zhen/=10;
    }
    for(i=wo;i>=1;--i)
    {
        printf("%d",wa[i]);
        p++;
        if(p==76) p=0,printf("\n");
    }
    if(p==0) printf("0"),p++;
    printf(".");p++;
    if(p==76) p=0,printf("\n");
    if(t==1)
    {
        printf("0");
        printf("\n");
        return ;
    }
    if(anss==0)
    {
        for(i=1;i<=ans.w[0];++i)
        {
            printf("%d",ans.w[i]);
            p++;
            if(p==76) p=0,printf("\n");
        }
        if(p!=0) printf("\n");
        return ;
    }
    for(i=1;i<=anss-1;++i)
    {
        printf("%d",ans.w[i]);
        p++;
        if(p==76) p=0,printf("\n");
    }
    printf("(");p++;
    if(p==76) p=0,printf("\n");
    for(i=anss;i<=t-1;++i)
    {
        printf("%d",ans.w[i]);
        p++;
        if(p==76) p=0,printf("\n");
    }
    printf(")");p++;
    printf("\n");
}
int main()
{
    freopen("fracdec.in","r",stdin);
    freopen("fracdec.out","w",stdout);
    int i,j;
    scanf("%d%d",&n,&m);
    for(i=1;i<=10005;++i) ans.w[i]=-1; 
    chu(n,m);
    return 0;
}

 

转载于:https://www.cnblogs.com/hhhhhhhhhh/p/5154721.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值