Test instructions for a number from the left, to the right number of the number of this bit, and then walk around, back to the starting point, each number has been accessed and accessed only once, this number is the number of loops
Ask to find the first number of loops larger than n
/*id:modengd1prog:runroundlang:c++*/#include <iostream> #include <math.h> #include <stdio.h># Include <memory.h>using namespace Std;bool isarroud (int x) { int a[20]; int size=0; BOOL VIS[10]; memset (vis,false,sizeof (Vis)); for (size=0;x>0; size++) { a[size+1]=x%10; if (a[size+1]==0) return false; if (Vis[a[size+1]]) return false; Vis[a[size+1]]=true; x/=10; } int tag=0,i; Because of the relationship of the direction stored in the A array, I represents the number of digits from left to right, but the a[size in the array for the I-bit number] position for (i=0;! (tag&1<<i); i= (I+a[size-i])%size) tag|=1<<i; if (tag== (1<<size) -1&&i==0) return true; return false;} int main () { freopen ("runround.in", "R", stdin); Freopen ("Runround.out", "w", stdout); int N; scanf ("%d", &n); while (n++) { if (Isarroud (N)) is break ; } cout<<n<<endl; return 0;}
Usaco runaround Numbers