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:
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.
A single line with a single integer, M
81361
A single line containing the next runaround number higher than the input value, M.
81362
没什么好说的神搜一下即可,注意题意;
/* ID:nealgav1 PROG:runround LANG:C++ */ #include<fstream> #include<cstring> using namespace std; const int mm=1234; bool vis[mm]; int len,_num; int num; bool flag; void dfs(char[],int,int); bool charge(int shu) { len=0; memset(vis,0,sizeof(vis)); char s[33]; while(shu) { //cout<<shu%10; if(shu%10==0||vis[shu%10])return 0; vis[shu%10]=1; s[len]=shu%10+'0'; len++;shu/=10; } int front=0,rear=len-1;char ms; while(front<rear) { ms=s[front];s[front]=s[rear];s[rear]=ms;front++;rear--; } memset(vis,0,sizeof(vis)); _num=0;flag=0; dfs(s,len,0); if(flag) return 1; else return 0; } void dfs(char s[],int lenth,int pos) { int next=s[pos]-'0'; vis[pos]=1;_num++; next=(next+pos)%lenth; if(_num==lenth&&next==0){flag=1;return;} if(!vis[next]) dfs(s,lenth,next); } int main() { ifstream cin("runround.in"); ofstream cout("runround.out"); cin>>num; // cout<<num%10<<"OJ"; for(int i=1;i<=999999;i++) if(charge(++num)) { cout<<num<<"\n"; break; } }
USER: Neal Gavin Gavin [nealgav1] TASK: runround LANG: C++ Compiling... Compile: OK Executing... Test 1: TEST OK [0.000 secs, 3336 KB] Test 2: TEST OK [0.011 secs, 3336 KB] Test 3: TEST OK [0.011 secs, 3336 KB] Test 4: TEST OK [0.011 secs, 3336 KB] Test 5: TEST OK [0.076 secs, 3336 KB] Test 6: TEST OK [0.054 secs, 3336 KB] Test 7: TEST OK [0.108 secs, 3336 KB] All tests OK.Your program ('runround') produced all correct answers! This is your submission #2 for this problem. Congratulations!
Here are the test data inputs:
------- test 1 ---- 99 ------- test 2 ---- 111110 ------- test 3 ---- 134259 ------- test 4 ---- 348761 ------- test 5 ---- 1000000 ------- test 6 ---- 5000000 ------- test 7 ---- 9000000Keep up the good work!
Thanks for your submission!
Runaround NumbersRuss CoxThe trick to this problem is noticing that since runaround numbers must have unique digits, they must be at most 9 digits long. There are only 9! = 362880 nine-digit numbers with unique digits, so there are fewer than 9*362880 numbers with up to 9 unique digits. Further, they are easy to generate, so we generate all of them in increasing order, test to see if they are runaround, and then take the first one bigger than our input.
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <assert.h> int m; FILE *fout; /* check if s is a runaround number; mark where we've been by writing 'X' */ int isrunaround(char *s) { int oi, i, j, len; char t[10]; strcpy(t, s); len = strlen(t); i=0; while(t[i] != 'X') { oi = i; i = (i + t[i]-'0') % len; t[oi] = 'X'; } /* if the string is all X's and we ended at 0, it's a runaround */ if(i != 0) return 0; for(j=0; j<len; j++) if(t[j] != 'X') return 0; return 1; } /* * create an md-digit number in the string s. * the used array keeps track of which digits are already taken. * s already has nd digits. */ void permutation(char *s, int *used, int nd, int md) { int i; if(nd == md) { s[nd] = '\0'; if(atoi(s) > m && isrunaround(s)) { fprintf(fout, "%s\n", s); exit(0); } return; } for(i=1; i<=9; i++) { if(!used[i]) { s[nd] = i+'0'; used[i] = 1; permutation(s, used, nd+1, md); used[i] = 0; } } } void main(void) { FILE *fin; char s[10]; int i, used[10]; fin = fopen("runround.in", "r"); fout = fopen("runround.out", "w"); assert(fin != NULL && fout != NULL); fscanf(fin, "%d", &m); for(i=0; i<10; i++) used[i] = 0; for(i=1; i<=9; i++) permutation(s, used, 0, i); assert(0); /* not reached */ }
Diego Exactas from Argentina has a better solution that runs extremely quickly.
This is my solution, with doesn't generate all solutions, but just looks for the next one.
#include <fstream> #include <string> #include <vector> #include <algorithm> #define INPUT_FILE "runround.in" #define OUTPUT_FILE "runround.out" using namespace std; void NextNumber(std::vector<int>& number, int Digits) { number[Digits - 1]++; for (int i = Digits - 1; i >= 0; i--) { if (number[i] == 10) { number[i] = 1; if (i == 0) { number.insert (number.begin(),1); return; } else number[i - 1]++; } } return; } bool CheckElement(std::vector<int>::iterator first, std::vector<int>::iterator last, int val) { while (first < last) { if (*first == val) return true; ++first; } return false; } void NextUniqueNumber(std::vector<int>& number) { std::vector<int> old = number; for (int i = 1; i < number.size(); ++i) { if (number[i] == 0) number[i]++; while (CheckElement (number.begin(),number.begin() + i,number[i])) { number[i]++; if (number[i] == 10) { number[i] = 1; NextNumber (number,i); i = 1; continue; } } } return; } bool IsRoundNumber(std::vector<int>& number) { std::vector<bool> used(10,false); for (int i = 0, pos = 0, val = number[0]; i < number.size(); i++) { pos = (pos + val) % number.size(); val = number[pos]; if (used[pos] == true) return false; used[pos] = true; } return true; } unsigned int NextRoundNumber(unsigned int numb