zoukankan      html  css  js  c++  java
  • USACO Section 2.4 Fractions to Decimals (fracdec)

    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)

    思路:找循环节,恶心的输出!!!!
    Executing...
       Test 1: TEST OK [0.000 secs, 4008 KB]
       Test 2: TEST OK [0.000 secs, 4008 KB]
       Test 3: TEST OK [0.000 secs, 4008 KB]
       Test 4: TEST OK [0.000 secs, 4008 KB]
       Test 5: TEST OK [0.000 secs, 4008 KB]
       Test 6: TEST OK [0.000 secs, 4008 KB]
       Test 7: TEST OK [0.000 secs, 4008 KB]
       Test 8: TEST OK [0.000 secs, 4008 KB]
       Test 9: TEST OK [0.000 secs, 4008 KB]
    
    All tests OK.
      1 /*
      2 ID:wuhuaju2
      3 PROG:fracdec
      4 LANG:C++
      5 */
      6 
      7 #include <cstdio>
      8 #include <iostream>
      9 #include <cstdlib>
     10 #include <algorithm>
     11 #include <cstring>
     12 using namespace std;
     13 
     14 const int maxn=100010;
     15 int mod,d,x,y,cnt,t,pos,tot;
     16 char a[maxn],ans[maxn],c[maxn];
     17 int b[maxn];
     18 bool f[maxn],flag;
     19 
     20 void close()
     21 {
     22     fclose(stdin);
     23     fclose(stdout);
     24     exit(0);
     25 }
     26 
     27 void work()
     28 {
     29     while (1)
     30     {
     31         t=x;
     32         x*=10;
     33         mod =x % y;
     34         x=x / y;
     35         if (f[t]==true || mod==0)
     36         {
     37             if (mod==0)
     38             {
     39                 cnt++;
     40                 a[cnt]=x+48;
     41                 flag=true;
     42             }
     43             break;
     44         }
     45         cnt++;
     46         a[cnt]=x+48;
     47         b[cnt]=t;
     48         f[t]=true;
     49         x=mod;
     50     }
     51     /*
     52     printf("t:%d\n",t);
     53     for (int i=1;i<=cnt;i++)
     54         cout<<b[i]<<" ";
     55     cout<<endl;
     56     */
     57     for (int i=1;i<=cnt;i++)
     58         if (t==b[i])
     59         {
     60             pos=i;
     61             break;
     62         }
     63     if (flag)
     64     {
     65     for (int i=1;i<=cnt;i++)
     66         printf("%c",a[i]);
     67     cout<<endl;
     68     close();
     69     }
     70     tot=0;
     71     for (int i=1;i<=cnt;i++)
     72     {
     73         if (pos==i)
     74         {
     75             tot++;
     76             ans[tot]='(';
     77         }
     78         tot++;
     79         ans[tot]=a[i];
     80     }
     81     tot++;
     82     ans[tot]=')';
     83     for (int i=1;i<=tot;i++)
     84     {
     85         printf("%c",ans[i]);
     86         if (i % 76==0)
     87             printf("\n");
     88     }
     89     cout<<endl;
     90 }
     91 
     92 void init ()
     93 {
     94 freopen("fracdec.in","r",stdin);
     95 freopen("fracdec.out","w",stdout);
     96    scanf("%d%d",&x,&y);
     97     int d=x/y;
     98     x=x-d*y;
     99     cnt=0;
    100     while (1)
    101     {
    102         mod=d % 10;
    103         d=d / 10;
    104         cnt++;
    105         c[cnt]=mod+48;
    106         if (d==0)
    107             break;
    108     }
    109     for (int i=1;i<=cnt;i++)
    110         a[i]=c[cnt-i+1];
    111     cnt++;
    112     a[cnt]='.';
    113 }
    114 
    115 int main ()
    116 {
    117     init();
    118     work();
    119     close();
    120     return 0;
    121 }
  • 相关阅读:
    SSD报告
    House of Spirit学习调试验证与实践
    怎样改动SharePoint管理中心的语言
    PuTTY连接Linuxserver常常断线解决方式
    HDU 2819 Swap (行列匹配+输出解)
    HDOJ--1061--Rightmost Digit
    POJ Cow Exhibition
    程序员之---C语言细节24(段错误、类型提升、sizeof &#39;A&#39;)
    java之 ------ 图形界面(三)
    Oracle 安装报错 [INS-06101] IP address of localhost could not be determined 解决方法
  • 原文地址:https://www.cnblogs.com/cssystem/p/2886311.html
Copyright © 2011-2022 走看看