先将所有数求出来后,再用二分找出第一个大于等于x的值
#include<stdio.h> #include<string.h> int a[800000]; int bs(int j,int x) { int mid,i=1; while(i<j) { mid = i+(j-i)/2; if(a[mid]<x) i = mid+1; else j = mid; } return i; } int main() { int n,m,i,b[10],ok,num,j=1; memset(a,0,sizeof(a)); for(i=1;i<=10000000;i++) { m = i; ok = 1; memset(b,0,sizeof(b)); while(m) { b[m]++; if(b[m]>=2) {ok = 0;break;} m/=10; } if(ok)a[j++] = i; } while(scanf("%d",&n)!=EOF) { num = bs(j,n); //这里不能是j-1,由于二分函数是while(i<j)i从1开始的,当j为1时就错了 printf("%d\n",num-1); } return 0; }