Test instructions: input integers A and B, (0<=a,b<=3000), output A/b cycle representation and the length of the loop section, output details see the original question
Analysis: If each time the division is simulated, the divisor once appeared. Since the maximum number is 3000, an array is opened to save the order in which each dividend appears.
1#include <iostream>2#include <map>3#include <queue>4#include <cstdio>5#include <cstring>6 using namespacestd;7 8 intvis[3005];9 Ten intMain () { One intb; A while(cin>>a>>b) { -memset (Vis,0,sizeof(Vis)); - intnum=1; thequeue<int>Q; - intn=a%b; -Q.push (A/b); - while(!Vis[n]) { +vis[n]=num++; -n*=Ten; +Q.push (n/b); An%=b; at } -printf"%d/%d =%d.", A,b,q.front ()); Q.pop (); - inttt=1; - while(!Q.empty ()) { - if(tt==vis[n]&&tt<= -) -printf"("); in if(tt> -){ -cout<<"..."; to Break; + } -tt++; thecout<<Q.front (); Q.pop (); * } $cout<<") \ n";Panax Notoginsengprintf"%d = number of digits in repeating cycle\n\n", num-vis[n]); - } the return 0; +}
View Code
UVA 202 repeating decimals