| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
水题啊水题,PE啊PE,换行注意,贴AC代码Source Code
Problem: 1140 User: yzhw
Memory: 732K Time: 32MS
Language: G++ Result: Accepted
Source Code
# include <iostream>
# include <set>
# include <queue>
using namespace std;
struct node
{
int num,pos,res;
};
class cmp
{
public:
bool operator()(const node &a,const node &b)
{
if(a.num!=b.num) return a.num>b.num;
else return a.res>b.res;
}
};
int gcd(int a,int b)
{
while(b)
{
int t=a%b;
a=b;
b=t;
}
return a;
}
set<node,cmp> refer;
queue<node> q;
int main()
{
int a,b;
while(true)
{
cin>>a>>b;
if(!a&&!b) break;
refer.clear();
while(!q.empty())
q.pop();
int t=gcd(a,b);
a/=t;
b/=t;
int ori=b;
cout<<".";
for(int pos=1;true;pos++)
{
if(pos%50==0) cout<<endl;
a*=10;
node tmp;
tmp.num=a%b;
tmp.pos=pos;
tmp.res=a/b;
if(!tmp.num)
{
cout<<a/b<<endl<<"This expansion terminates."<<endl;
break;
}
if(refer.find(tmp)==refer.end())
{
cout<<a/b;
a=tmp.num;
refer.insert(tmp);
q.push(tmp);
if(q.size()>ori)
{
refer.erase(q.front());
q.pop();
}
}
else
{
if(pos%50!=0) cout<<endl;
cout<<"The last "<<(pos-refer.find(tmp)->pos)<<" digits repeat forever."<<endl;
break;
}
}
}
// system("pause");
return 0;
}
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator