uva 202 Repeating Decimals
题意:输入整数a和b,(0<=a,b<=3000),输出a/b的循环表示以及循环节长度,输出详情见原题
分析:如果每次模拟除法,结束的时候就是被除数曾经出现过。因为数最大为3000,开一个数组保存每个被除数出现的顺序就行了
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<iostream> 2 #include<map> 3 #include<queue> 4 #include<cstdio> 5 #include<cstring> 6 using namespace std; 7 8 int vis[3005]; 9 10 int main(){ 11 int a,b; 12 while(cin>>a>>b){ 13 memset(vis,0,sizeof(vis)); 14 int num=1; 15 queue<int> q; 16 int n=a%b; 17 q.push(a/b); 18 while(!vis[n]){ 19 vis[n]=num++; 20 n*=10; 21 q.push(n/b); 22 n%=b; 23 } 24 printf("%d/%d = %d.",a,b,q.front());q.pop(); 25 int tt=1; 26 while(!q.empty()){ 27 if(tt==vis[n]&&tt<=50) 28 printf("("); 29 if(tt>50){ 30 cout<<"..."; 31 break; 32 } 33 tt++; 34 cout<<q.front();q.pop(); 35 } 36 cout<<")\n"; 37 printf(" %d = number of digits in repeating cycle\n\n",num-vis[n]); 38 } 39 return 0; 40 }