zoukankan      html  css  js  c++  java
  • USACO2.4.5Fractions 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)
    题解:就是模拟除法嘛。我们用一个数组记录余数,如果一个已经出现过的余数再次出现,那么循环节就出现了。循环节是从余数上次出现的位置到余数此次出现的位置的前一个位置为止。如果余数为零,那么输出小数部分。如果输入的第一个数能整除第二个数,则不会产生小数部分,那么输出整数部分,然后输出小数点之后再输出一个0即可。
    View Code
     1 /*
     2 ID:spcjv51
     3 PROG:fracdec
     4 LANG:C
     5 */
     6 #include<stdio.h>
     7 #include<stdlib.h>
     8 #include<string.h>
     9 long f[100005];
    10 int  s[100005];
    11 int main(void)
    12 {
    13     freopen("fracdec.in","r",stdin);
    14     freopen("fracdec.out","w",stdout);
    15     long flag,n,d,ans,t,i,sum;
    16     char ss[20];
    17     scanf("%ld%ld",&n,&d);
    18     if(n%d==0)
    19         printf("%ld.0\n",n/d);
    20         else
    21         {
    22             printf("%ld.",n/d);
    23             sprintf(ss,"%ld",n/d);
    24             sum=strlen(ss)+1;
    25             memset(f,0,sizeof(f));
    26             flag=1;
    27             ans=0;
    28             while(flag)
    29             {
    30                 ans++;
    31                 t=n%d;
    32                 if(f[t])
    33                 {
    34                     sum++;
    35                     for(i=1; i<ans; i++)
    36                     {
    37                         sum++;
    38                         if(i==f[t])
    39                             printf("(%d",s[i]);
    40                         else
    41                             printf("%d",s[i]);
    42                         if(sum%76==0) printf("\n");
    43                     }
    44                     printf(")\n");
    45                     flag=0;
    46                     break;
    47                 }
    48                 else
    49                 {
    50                     f[t]=ans;
    51                     n=t*10;
    52                     s[ans]=n/d;
    53                 }
    54                 if(t==0)
    55                 {
    56                     for(i=1; i<ans; i++)
    57                     {
    58                         sum++;
    59                         printf("%d",s[i]);
    60                         if(sum%76==0)
    61                             printf("\n");
    62                     }
    63                     printf("\n");
    64                     flag=0;
    65                     break;
    66                 }
    67 
    68             }
    69         }
    70     return 0;
    71 }
  • 相关阅读:
    docker创建tomcat容器
    【转载】张一鸣:为什么 BAT 挖不走我们的人才?
    Elastic认证考试,请先看这一篇
    vs code 初始化vue项目框架
    Idea集成git常用命令
    pxc搭建mysql集群
    mysql无限级分类
    Java面试题大全
    SpringMVC和Spring
    Redis高级特性
  • 原文地址:https://www.cnblogs.com/zjbztianya/p/2913616.html
Copyright © 2011-2022 走看看