这是固定规律
n位首位相接组成n+10^n-1个数
则,
- 以n-1个0开始
- 以n-1个0结束
直接用dfs搜索,扩展,若扩展失败,一定是因为0,直接以1扩展即可。
// ShellDawn
// POJ1780
// No.20
#include<cstdio>
#include<iostream>
#include<cstring>
#include<string>
#include<map>
#define MM(x) memset(x,0,sizeof(x))
using namespace std;
#define maxn 100010
const int mod[] = {0,1,10,100,1000,10000,100000};
int n;
// V[i]表示n-1位的数字i在后面使用过了0~?个数
int V[maxn];
char ans[maxn*10]; // 最后输出序列
int a;
int main(){
while(~scanf("%d",&n)&&n){
if(n==1){
puts("0123456789");
continue;
}
MM(V);
// 此排列一定以0开始
MM(ans);
a = n-1;
int now = 0;
int f = 1;
while(f){
f = 0;
while(V[now] < 10){
f = 1;
int next = now*10 + V[now];
ans[a++] = V[now]++;
now = next%mod[n];
}
now = (now + ans[a-n]*mod[n])/10;
a--; // 搜索失败,回退一位
}
// 此排列一定以0结束
for(int i=0;i<=a+n;i++) putchar(ans[i] + '0');
puts("");
}
return 0;
}