Home > Old Blog Posts > USACO 2.4.5 Fractions to Decimals

USACO 2.4.5 Fractions to Decimals


模拟手算长除法,其实就是一道高精度计算题,判重可以用一个布尔数组实现。

代码:

/*
ID: dementr1
PROG: fracdec
LANG: C++
*/
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
ifstream fin(“fracdec.in”);
ofstream fout(“fracdec.out”);
int y[100001]={},s[100001]={},ynum=0,snum=0,r=0;
struct link
{
bool appear;
int dis;
} data[100001];
string ss;
void divide(int a, int b)
{
int i;
bool print=false;
y[0]=a%b;
s[0]=a/b;
int start;
while(1)
{
snum++;
s[snum]=(y[ynum]*10)/b;
y[ynum+1]=(y[ynum]*10)%b;
ynum++;
if(data[y[ynum]].appear)
{
print=true;
break;
}
data[y[ynum]].appear=true;
data[y[ynum]].dis=snum;
}
int forstart,forcmp;
start=data[y[ynum]].dis;
if(s[start]==s[snum])
{
forstart=snum-1;
forcmp=start;
}
else
{
forstart=snum;
forcmp=start+1;
}
if(forstart==forcmp) print=false;
int t[10],tmp=0;
if(s[0]==0) ss+=’0′;
while(s[0]>0)
{
t[tmp++]=s[0]%10;
s[0]/=10;
}
for(i=tmp-1;i>=0;i–) ss+=(t[i]+’0′);
ss+=”.”;
bool another=false;
for(i=1;i<=forstart;i++)
{
if(i==forcmp&&i!=forstart&&print)
{
ss+=”(“;
}
if(!(i==forcmp&&i==forstart&&s[i]==0))
{
ss+=(s[i]+’0′);
another=true;
}
}
if(print) ss+=”)”;
if(!another) ss+=”0″;
for(i=0;i<ss.length();i++)
{
fout<<ss[i];
if(i%76==75) fout<<endl;
}
if(ss.length()%76!=0) fout<<endl;
}
int main()
{
int a,b,i;
fin>>a>>b;
for(i=0;i<=100000;i++) data[i].appear=false;
divide(a,b);
fin.close();
fout.close();
return 0;
}

Categories: Old Blog Posts
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: