题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4151
题目解析:
先打表求出所有特殊数存到数组res[1000000]中,然后对于每一个数在表中进行二分查找,找到的位置即小于等于该数的特殊数个数。
#include <iostream>
using namespace std;
int res[1000000];
int main() {
bool flag[10]; // flag[i] 表示是否出现过数字i
int i, j, k, n, a, b, c;
k = 1;
for (i = 1; i < 10000000; i++) {
j = i;
memset(flag, 0, sizeof(flag));
while (j != 0) {
if (flag[j % 10])
break;
else {
flag[j % 10] = true;
j /= 10;
}
}
if (j == 0) res[k++] = i;
}
while (cin >> n) {
if (n <= 0) {
cout << "0" << endl;
continue;
}
//二分搜索
a = 1;
b = k - 1;
while (a <= b) {
c = (a + b) / 2;
if (n <= res[c])
b = c - 1;
else
a = c + 1;
}
cout << b << endl;
}
return 0;
}