calculate the repeating decimal cycle section
Enter integers A and B (0<=a<=3000,1<=b<=3000), output A/b repeating decimal representation, and the length of the loop section.
For example, a=5,b=43, decimal notation is 0. (116279069767441860465), and the loop byte length is 21
You can use arrays to store numbers and simulate vertical division to solve them.
The AC code is attached:
1#include <iostream>2#include <cstring>3 using namespacestd;4 5 Const intmax=3050;6 7 intR[max],u[max],s[max];8 9 intMain () {Ten intn,m,t; One while(cin>>n>>m) { At=N; -Memset (R,0,sizeof(R)); -memset (U,0,sizeof(U)); thememset (s,-1,sizeof(s)); - intans=0; -r[ans++]=n/m;//r arrays are used to store numbers -n=n%m; + while(!u[n]&&n) {//Simulate Vertical Division -U[n]=ans;//The u Array records the location of the repetition point (accession number) +S[ans]=n;//s used to judge at which point the repetition Ar[ans++]=Ten*n/m; atn=Ten*n%m; - } -cout<<t<<"/"<<m<<" "<<"="<<" "<<r[0]<<"."; - for(intI=1; i<ans&&i<= -; i++){ - if(n&&s[i]==n)//start the loop when the condition is established -cout<<"("; incout<<R[i]; - } to if(!n)//can be removed at the best +cout<<"(0"; - if(ans> -)//don't forget to use more than 50 bits ... thecout<<"..."; *cout<<")"<<Endl; $ intSum=n? (Ans-u[n]):1;//Total number of digits minus the number of bits that start the loop is the number of loop bitsPanax Notoginsengcout<<" "<<sum<<"= number of digits in repeating cycle"<<endl<<Endl; - } the return 0; +}
UVa 202 Repeating decimals