#include #include #include using namespace std; int getMin(int y); int incr(int x); int ans[1000000]; //vector inUse(10); int main() { int x=1; int n; for(int i=0; i<1000000; i++) { ans[i]=x; //cout << "Blah" << endl; x=incr(x); //cout << x << endl; } //cout << "Done precalcing" << endl; cin >> n; while(n!=0) { cout << ans[n-1] << endl; cin >> n; } } int getMin(int x, int y) { vector inUse(10); while(x>0) { inUse[x%10]=true; x/=10; } for(int i=0; i<10; i++) if(!inUse[i] && i>y) return i; for(int i=0; i<10; i++) if(!inUse[i]) return i; } int incr(int x) { //cout << "Need to incr prefix: " << x << endl; if(x==0) return 1; //inUse[x%10]=0; int cache=getMin(x, x%10); if(cache > x%10) { x=(x/10)*10+cache; //inUse[cache]=1; } else { int nX = incr(x/10); int tmp=getMin(nX, -1); // inUse[tmp]; x = nX*10 + tmp; } return x; }