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 }
  • 相关阅读:
    Linux的常用目录学习笔记
    htm,html,xhtml,xml,xsl,dhtml,shtm和shtml的区分
    js中迭代元素特性与DOM中的DocumentFragment类型 笔记
    查找算法--线性结构的查找方法
    有关rand(),srand()产生随机数学习总结
    nodejs版本管理工具nvm使用说明
    解决微信端公众号网页获取短信验证码ajax重复调用两次的问题
    判断安卓苹果ipad,iphone,微信
    css的对号错号,也就是勾和叉
    flex的一些方法注释或者叫flex笔记
  • 原文地址:https://www.cnblogs.com/zjbztianya/p/2913616.html
Copyright © 2011-2022 走看看