#include<cstdio> #include<cstring> using namespace std; char s[10]; int used[10]; char shuru[10]; int check(char s[]) { int len=strlen(s); int vis[10]={0}; vis[0]=1; int temp=0; for(int i=1;i<len;i++) { temp=(temp+s[temp]-‘0‘)%len; if(vis[temp]) return 0; vis[temp]=1; } if((temp+s[temp]-‘0‘)%len==0) return 1; return 0; } int ju(char s[],char shuru[]) { int len1=strlen(s); int len2=strlen(shuru); if(len1>len2) return 1; if(len1<len2) return 0; for(int i=0;i<len1;i++) { if(s[i]>shuru[i]) return 1; if(s[i]<shuru[i]) return 0; } return 0; } int work(char s[],int cur,int fin) { if(cur==fin) { s[fin]=0; if(ju(s,shuru)&&check(s)) { printf("%s\n",s); return 1; } return 0; } for(int i=1;i<=9;i++) { if(!used[i]) { s[cur]=i+‘0‘; used[i]=1; if(work(s,cur+1,fin)) return 1; used[i]=0; } } return 0; } int main() { freopen("runround.in","r",stdin); freopen("runround.out","w",stdout); memset(used,0,sizeof(used)); scanf("%s",shuru); for(int i=1;i<=9;i++) { if(work(s,0,i)) break; } return 0; }
迭代生成排列
时间: 2024-12-23 18:42:01