1468. Fraction
Time limit: 1.0 second
Memory limit: 64 MB
Memory limit: 64 MB
There are two numbers,
a and
b. It is required to calculate the value
a/
b and write it in the
k-based number system.
Input
The input consists of several tests (not more than 100) separated by a line break. Each test consists of three integers 1 ≤
a ≤ 10000, 1 ≤
b ≤ 10000, 2 ≤
k ≤ 36. There are two zeros in the last line.
Output
For each test, output a line containing the representation of the number
a/
b in the
k-based number system. The integer part must be separated from the fractional part by a point. If the number
a/
b is whole, then there should be no point. The output must not contain insignificant zeros. If the fraction
a/
b is periodic, then its period must be given in parentheses.
Sample
input | output |
---|---|
10 5 2 1 2 8 8780 1 29 2794 6083 23 157 7 19 1 12 10 0 0 | 10 0.4 ACM 0.(ACM) 13.(82DAG5) 0.08(3) |
Problem Author: Dmitry Zhukov
Problem Source: Ural SU and Orel STU Contest. Petrozavodsk Summer Session, August 2006
Problem Source: Ural SU and Orel STU Contest. Petrozavodsk Summer Session, August 2006
Tags: none
)
Difficulty: 750
Printable version
Submit solution
Discussion (6)
All submissions (2013) All accepted submissions (619) Solutions rating (415)
All submissions (2013) All accepted submissions (619) Solutions rating (415)
题意:将a除以b用k进制表示
题解:模拟进行...若用带 C11 字眼的编译器交就会超时...其他编译器AC,
本人不知道是什么原因,有人知道的话望留言
#include<stdio.h>
#include<string.h>
int c[10005];
int mark[1000005];
int main()
{
int x,y,k,i,j,a,b,l,r;
while(scanf("%d%d",&x,&y),x+y>0)
{
scanf("%d",&k);
a=x/y;
b=x%y;
if(a==0) printf("0");
else
{
i=0;
while(a)
{
c[i++]=a%k;
a=a/k;
}
for(j=i-1;j>=0;j--)
if(c[j]<10) printf("%d",c[j]);
else printf("%c",'A'+c[j]-10);
}
l=r=0;
if(b)
{
memset(mark,-1,sizeof(mark));
printf(".");
b=b*k;
while(b)
{
if(mark[b]!=-1)
{
l=mark[b];
break;
}
mark[b]=r;
c[r++]=b;
b=b%y;
b=b*k;
if(b==0) l=r;
}
for(i=0;i<l;i++)
if(c[i]<10) printf("%d",c[i]);
else printf("%c",'A'+c[i]-10);
if(l<r)
{
printf("(");
for(i=l;i<r;i++)
if(c[i]<10) printf("%d",c[i]);
else printf("%c",'A'+c[i]-10);
printf(")");
}
}
printf("\n");
}
}