usaco 2.4 fracdec 2008.6.30
{
1.刚开始用商来作标识,不是一个好方法,后来用余数作标识。
2.结果可能有很多位,记录结果的,不能用string,而应开一个数组
3.多试几组数据,否则只能过一两组
4.要先检查一下自己的程序无误,确保正确,再试数据,否则,只有一两个简单数据对了,是不能得满分的!}
{
ID:
PROG: fracdec
LANG: PASCAL
}
program p_fracdec;
const maxn=100000;fin='fracdec.in';fout='fracdec.out';
var f1,f2:text;
f:array[0..maxn]of boolean;
b:array[1..maxn]of longint;
a,h:array[1..maxn]of char;
p,s:string;flag:boolean;
lh,lp,n,d,i,j,x,l,r,lb,t,y,ls:longint;
begin
assign(f1,fin);reset(f1);assign(f2,fout);rewrite(f2);
read(f1,n,d);fillchar(f,sizeof(f),false);
str(n div d,s);flag:=false;ls:=length(s);
for i:=1 to ls do a[i]:=s[i];l:=ls+1;a[l]:='.';
x:=n mod d;
ifx=0 then begin inc(l);a[l]:='0';h:=a;lh:=l;end
else
begin
f[x]:=true;lb:=1;b[1]:=x;
repeat
x:=x*10;str(x div d,p);lp:=length(p);
for i:=1 to lp do begin inc(l);a[l]:=p[i];end;
x:=x mod d;inc(lb);b[lb]:=x;
if f[x] then begin flag:=true end else f[x]:=true;
until flag;
i:=0;repeat inc(i);until b[i]=x;
t:=ls+1;y:=t+i-1;lh:=0; writeln(a[t]);
ifx<>0 then
begin
for i:=1 to y do begin inc(lh);h[lh]:=a[i];end;
inc(lh);h[lh]:='(';
for i:=y+1 to lb+t-1 do begin inc(lh);h[lh]:=a[i];end;
inc(lh);h[lh]:=')';
end
else for i:=1 to y do begin inc(lh);h[lh]:=a[i];end;
end;
for i:=1 to lh do if i mod 76=0 then writeln(f2,h[i]) elsewrite(f2,h[i]);writeln(f2);
close(f1);close(f2);
end.