http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2609
A-Number and B-Number
Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^
题目描述
Tom is very interested in number problem. Nowadays he is thinking of a problem about A-number and B-number.
A-number is a positive integer whose decimal form contains 7 or it can be divided by 7. We can write down the first 10 A-number ( a[i] is the ith A-number)
{a[1]=7,a[2]=14,a[3]=17,a[4]=21,a[5]=27,a[6]=28,a[7]=35,a[8]=37,a[9]=42,a[10]=47};
B-number is Sub-sequence of A-number which contains all A-number but a[k] ( that k is a A-number.) Like 35, is the 7th A-number and 7 is also an A-number so the 35 ( a[7] ) is not a B-number. We also can write down the first 10 B-number.
Now Given an integer N, please output the Nth B-number
输入
For each test case, there will be a positive integer N as the description.
输出
For each test case, output an integer indicating the Nth B-number.
You can assume the result will be no more then 2^63-1.
示例输入
1 7 100
示例输出
7 37 470
提示
来源
示例程序
分析:
一开始想到打表,结果超时咯,后来看了标程用了搜索写的,,,orz
超时代码:
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 using namespace std; 5 bool fun(int n){ 6 if(n%7==0) return true; 7 while(n) { 8 if(n%10==7) return true; 9 n/=10; 10 } 11 return false; 12 } 13 long long a[100000010]; 14 long long b[100010000]; 15 int main(){ 16 int i,c=0,cc=0; 17 //freopen("in.txt","r",stdin); 18 for(i=1;i<=100000000;i++) 19 if(fun(i)) a[++c]=i; 20 for(i=1;i<c;i++) 21 if(!fun(i)) 22 { b[++cc]=a[i];} 23 long long n; 24 while(cin>>n){ 25 if(n>cc-1) cout<<"??"; 26 else 27 cout<<b[n]<<endl;} 28 return 0; 29 }