您的位置:首页 > 其它

USACO-Fractions to Decimals

2016-06-21 11:29 218 查看
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)

题意:给一个分数,输出小数,循环小数部分要有括号。

模拟

代码:

/*
ID:iam666
PROG:fracdec
LANG:C++
*/
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <iostream>
using namespace std;
//bool Hash[10002];
//long a[10002],b[10005];
int main (void)
{
//freopen("fracdec.in","r",stdin);
//freopen("fracdec.out","w",stdout);
long n,m;
cin>>n>>m;
bool Hash[100002];
memset(Hash,false,sizeof(Hash));
Hash[0]=true;
long d=n%m;
if(d==0)
{
printf("%ld.0\n",n/m);
return 0;
}
long a[100002],b[100005];
long l=0,p=0;
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
a[++p]=n/m;
long ii=0;
long yy=a[1];
if(yy==0)
ii++;
while(yy>0)
{
ii++;
yy/=10;
}
ii++;//位数
printf("%ld.",a[1]);
b[p]=d;
while(!Hash[d])
{
Hash[d]=true;
d=d*10;
a[++p]=d/m;
d=d%m;
b[p]=d;
}
long i=1;
long bo=false;
for(long j=1;j<=p;j++)
{
if(b[j]==d)
{
l=j+1;
//l=j;
bo=true;
break;
}
}
for(i=2;i<=p;i++)
{
if(i==l&&bo)
{
printf("(");
i--;
ii++;
bo=false;
}
else
{
printf("%ld",a[i]);
ii++;

if(ii%76==0)
printf("\n");}
}
if(d)
printf(")\n");
else
if(ii%76)
printf("\n");
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  USACO