长除法, 当发现余数一样时候, 出现循环.
/**//*
ID: lorelei3
TASK: fracdec
LANG: C++
*/
#include <fstream>
#include <iostream>
#include <string.h>
using namespace std;
const int MAX = 100000;
char ans[MAX],ch[MAX];
typedef struct Num{
bool flag;
int index;
}Num;
Num f[MAX];
int main(){
int a,b,c,i,j,k, start, stop;
bool p=false;;
for(k=0; k<MAX;++k){
f[k].flag = false;
f[k].index = -1;
}
ifstream fin("fracdec.in");
ofstream fout("fracdec.out");
fin>>a>>b;
c = a/b;
a = a%b;
sprintf(ans, "%d.", c);
f[a].flag=true;
f[a].index=0;
int len = strlen(ans);
k=0;
while(true){
a*=10;
c=a/b;
a=a%b;
ch[k] = (char)('0'+c);
if(a==0){
p=true;
break;
}
if(f[a].flag==true){
start = f[a].index;
stop = k;
break;
}else{
f[a].flag = true;
f[a].index = k+1;
k++;
}
}
if(p){
strcat(ans, ch);
}else{
for(i=0, j=len; i<start; ++i, ++j)
ans[j] = ch[i];
ans[j++]='(';
for(i=start; i<=stop; ++i, ++j)
ans[j] = ch[i];
ans[j++]=')';
ans[j]='\0';
}
bool con = true;
i=0;
while(con){
k=76;
while(k--){
if(ans[i]=='\0'){
con=false;
break;
}
else
fout<<ans[i++];
}
fout<<endl;
}
return 0;
}
posted on 2010-12-14 14:43
小阮 阅读(205)
评论(0) 编辑 收藏 引用 所属分类:
USACO