Record the Euler's path.
The question is hard to understand. English dregs, translation is not necessary for half a day. Only when you read the Chinese characters in PDF can you understand the meaning of the question.
Question: give n (1 <= n <= 6) a sequence of all numbers in the shortest Lexicographic Order.
An example of N = 2 is given. Let's take this description. If it's too long, let's talk about the previous one.
00102030405060708091121314151617181922324252627282933435363738394454647484955657585966768697787988990
00 appears, 01 appears, and then 10, 02, 20, 03, 30, 04, 40, 05, 50, 06, 60, 07, 70, 08, 80, 09.
90 cannot appear here. Because all the values starting with 0 already appear. If 0 is used, the shortest cannot be ensured. 91, (10 Miss) 11,12,
The end of the previous number must be the beginning of the next number.
If multiple digits n = 6. Except the first digit, the remaining five digits must be the next header. This way.
The last length is 10 ^ N + N-1.
Taken from PDF:
There are 10 N encoding schemes for N digits (I .e., 10 N groups). To make a digital sequence contain the 10 N groups of N digits, and the sequence length
The degree is the shortest. The only possibility is that each group appears once and only once, and the last n-1 bits of the previous group are the first n-1 bits of the last group,
In this way, the numbers of 10 N groups are respectively set to 1 bit, with a total of 10 N digits, plus the last n-1 digits in the last group, and the total number of digits is 10 N + n-1.
#include<cstdio>#include<cstring>#include<string>#include<queue>#include<algorithm>#include<map>#include<stack>#include<iostream>#include<list>#include<set>#include<cmath>#define INF 0x7fffffff#define eps 1e-6#define LL long longusing namespace std;#define M 100000char str[7][M*10];int l[M];stack<int>s;char ans[M * 10];int a;void dfs( int v, int m ){ int w; while ( l[v] < 10 ) { w = v * 10 + l[v]; l[v]++; s.push(w); v = w % m; }}int main( ){ int n, m, i, v; strcpy(str[1],"0123456789"); for(int nn=2; nn<=6; nn++) { n=nn; while(!s.empty())s.pop(); a = 0, v=0; m = pow( 10, double( n - 1 ) ); for ( i = 0; i < m; i++ ) l[i] = 0; dfs( v, m ); while(!s.empty()) { v=s.top(); s.pop(); ans[a++] = v % 10 + '0'; v /= 10; dfs( v, m ); } int cot=0; for ( i = 1; i < n; i++ ) str[nn][cot++]='0'; while (a) str[nn][cot++]=ans[--a]; str[nn][cot]='\0'; } while(scanf("%d",&n),n) puts(str[n]);}