http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1035
Because the result is either a finite number or an infinite loop of decimals, just simulate the process and then judge the loop to jump out and record the number of loops.
1#include <iostream>2#include <string.h>3#include <stdio.h>4#include <map>5 using namespacestd;6 intvis[1001],a[1001];7 intMain ()8 {9 //freopen ("A.txt", "R", stdin);Ten intx,y,z,k; One for(intI=1; i<= +; i++) A { -memset (Vis,0,sizeof(Vis)); -x=1, z=0, k=1; the while(1) - { -y=x%i; -x=x/i; + if(y==0|| Vis[y]) Break; -vis[y]=1; +x= (k-x*i) *Ten; Ak=x; atz++; - //printf ("%d\n", x); - } - if(Vis[y]) a[i]=Z; - Elsea[i]=0; - //printf ("%d\n", A[i]); in } - intn,j=1, ans=0; toscanf"%d",&n); + for(intI=1; i<=n;i++) - { the if(a[i]>ans) * { $ans=A[i];Panax Notoginsengj=i; - } the } +printf"%d\n", j); A return 0; the}
Longest cycle section