Runaround Numbers

Runaround numbers are integers with unique digits, none of which is zero (e.g., 81362) that also have an interesting property, exemplified by this demonstration:

  • If you start at the left digit (8 in our number) and count that number of digits to the right (wrapping back to the first digit when no digits on the right are available), you'll end up at a new digit (a number which does not end up at a new digit is not a Runaround Number). Consider: 8 1 3 6 2 which cycles through eight digits: 1 3 6 2 8 1 3 6 so the next digit is 6.
  • Repeat this cycle (this time for the six counts designed by the `6') and you should end on a new digit: 2 8 1 3 6 2, namely 2.
  • Repeat again (two digits this time): 8 1
  • Continue again (one digit this time): 3
  • One more time: 6 2 8 and you have ended up back where you started, after touching each digit once. If you don't end up back where you started after touching each digit once, your number is not a Runaround number.

Given a number M (that has anywhere from 1 through 9 digits), find and print the next runaround number higher than M, which will always fit into an unsigned long integer for the given test data.

PROGRAM NAME: runround

INPUT FORMAT

A single line with a single integer, M

SAMPLE INPUT (file runround.in)

81361

OUTPUT FORMAT

A single line containing the next runaround number higher than the input value, M.

SAMPLE OUTPUT (file runround.out)

81362


这道题NOWCOW翻译的太坑爹了,害得我W了很多次。。。。。。。。这道题也写晕了
其实只要记录你访问的次数,再加上验证是否所有数字都已访问过即可
结果还是很励志的

USER: BRUCE LEE [kaisada2]
TASK: runround
LANG: C++

Compiling...
Compile: OK

Executing...
   Test 1: TEST OK [0.000 secs, 3216 KB]
   Test 2: TEST OK [0.000 secs, 3216 KB]
   Test 3: TEST OK [0.000 secs, 3216 KB]
   Test 4: TEST OK [0.000 secs, 3216 KB]
   Test 5: TEST OK [0.000 secs, 3216 KB]
   Test 6: TEST OK [0.000 secs, 3216 KB]
   Test 7: TEST OK [0.011 secs, 3216 KB]

时间比网上见到的都要短些

View Code
  1 /*
  2 ID:kaisada2
  3 PROG:runround
  4 LANG:C++
  5 */
  6 #include<iostream>
  7 #include<string.h>
  8 #include<algorithm>
  9 #include<cstdio>
 10 #include<cstdlib>
 11 #include<cstring>
 12 
 13 
 14 using namespace std;
 15 
 16 
 17 int n;
 18 
 19 int a[20];
 20 int b[20];
 21 int k=0;
 22 int now;
 23 bool use[101];
 24 int q=0;
 25 
 26 
 27 
 28 int mod(int i)
 29 {
 30     int sum=1;
 31     for(int j=1;j<=i;j++)
 32     {
 33        sum=sum*10;
 34     }
 35     return sum;
 36 }
 37 
 38 
 39 void change()
 40 {
 41     q++;
 42     int index=1;
 43     a[index]++;
 44     while(1)
 45     {
 46        if(a[index]!=10)
 47        break;
 48        if(a[index]==10)
 49        {
 50           a[index]=1;
 51           q+=mod(index-1);
 52           a[index+1]++;
 53           index++;
 54           if(index>k)
 55           k=index;
 56        }
 57     }
 58 }
 59 
 60 int get(int n)
 61 {
 62     if(n-a[n]>0)
 63     {
 64       if(a[n-a[n]]==a[n])
 65       return -1;
 66       use[n-a[n]]=true;          
 67       return n-a[n];
 68     }
 69     else
 70     {
 71       int q1=n-a[n];
 72       while(q1<=0)
 73       {
 74          q1+=k;
 75       }
 76       if(q1==n||a[q1]==a[n])
 77       {
 78          return -1;
 79       }
 80       use[q1]=true;
 81       return q1;
 82     }
 83 }  
 84 
 85 int wx()
 86 {
 87    int ok=1;
 88    for(int i=1;i<=k;i++)
 89    {
 90       if(use[i]!=true)
 91       {
 92          ok=0;
 93          break;
 94       }
 95    }
 96    return ok;
 97 }
 98 
 99 
100 
101 int check()
102 {
103     memset(use,0,sizeof(use));
104     int newn;
105     now=k;
106     use[now]=true;
107     int r=0;
108     while(1)
109     {
110        r++;
111        newn=get(now);
112        if(newn==-1)
113        return 0;
114        now=newn;
115        if(now==k&&r==k&&wx())
116        {
117           return 1;
118        }
119        if(r>k)
120        return 0;
121     }
122 }   
123       
124       
125 int ch()
126 {
127     bool wk[10];
128     memset(wk,0,sizeof(wk));
129     int ok=1;
130     for(int i=1;i<=k;i++)
131     {
132        if(wk[a[i]]==true)
133        {
134            ok=0;
135            break;
136        }
137        wk[a[i]]=true;
138     }
139     return ok;
140 } 
141        
142 
143 int main()
144 {
145     freopen("runround.in","r",stdin);
146     freopen("runround.out","w",stdout);
147     cin>>n;
148     int x=n;
149     while(x!=0)
150     {
151        k++;
152        a[k]=x%10;
153        x=x/10;
154     }
155     while(1)
156     {
157         change();
158         while(!ch())
159         {
160            change();
161         }     
162         if(check())
163         {
164            cout<<n+q<<endl;
165            break;
166         }
167     } 
168     return 0;;
169 }
原文地址:https://www.cnblogs.com/spwkx/p/2613122.html