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 }

 

转载于:https://www.cnblogs.com/cssystem/archive/2013/01/31/2886311.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值