解答:
因为除数是固定的,所以只要判断被除数是否出现过即可。
#include<iostream> #include <cstdio> #include <cstring> using namespace std; int m,n,vis[5000],res[5000],temp[5000],num,t,x; int main() {while(scanf("%d%d",&m,&n)==2) {memset(vis,0,sizeof(vis));memset(res,0,sizeof(res));memset(temp,0,sizeof(temp));x = m/n;num=0;t=m;m=m%n; while(vis[m]==0&&m!=0){vis[m]=1;temp[m]=num;res[num]=m*10/n;num++;m=m*10%n; }if (m==0){printf("%d/%d = %d.",t,n,x);for (int i=0;i<num;++i)cout<<res[i];printf("(0)\n 1 = number of digits in repeating cycle\n");}else{printf("%d/%d = %d.",t,n,x);for(int i=0;i<temp[m];++i) printf("%d",res[i]);printf("(");if(num-temp[m]>50){for (int i=temp[m];i<50+temp[m];++i) printf("%d",res[i]);printf("...");}elsefor (int i=temp[m];i<num;++i) printf("%d",res[i]);printf(")\n %d = number of digits in repeating cycle\n",num-temp[m]);}printf("\n");}return 0; }