zyb的面试

zyb的面试

http://acm.hdu.edu.cn/showproblem.php?pid=6468

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 470    Accepted Submission(s): 179


Problem Description
今天zyb参加一场面试,面试官听说zyb是ACMer之后立马抛出了一道算法题给zyb:
有一个序列,是1到n的一种排列,排列的顺序是字典序小的在前,那么第k个数字是什么?
例如n=15,k=7, 排列顺序为1, 10, 11, 12, 13, 14, 15, 2, 3, 4, 5, 6, 7, 8, 9;那么第7个数字就是15.
那么,如果你处在zyb的场景下,你能解决这个问题吗?
 
Input
T组样例(T<=100)
两个整数n和k(1<=n<=1e6,1<=k<=n),n和k代表的含义如上文
 
Output
输出1-n之中字典序第k小的数字
 
Sample Input
1
15 7
 
Sample Output
15

暴力模拟

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 #define lson l,mid,rt<<1
 4 #define rson mid+1,r,rt<<1|1
 5 #define sqr(x) ((x)*(x))
 6 #define pb push_back
 7 #define eb emplace_back
 8 #define maxn 100005
 9 #define eps 1e-8
10 #define pi acos(-1.0)
11 #define rep(k,i,j) for(int k=i;k<j;k++)
12 typedef long long ll;
13 typedef pair<int,int> pii;
14 typedef pair<double,double>pdd;
15 typedef pair<int,char> pic;
16 typedef pair<pair<int,string>,pii> ppp;
17 typedef unsigned long long ull;
18 const long long mod=1e9+7;
19 const double oula=0.57721566490153286060651209;
20 using namespace std;
21 
22 int n,k;
23 
24 int main(){
25 
26     int t;
27     cin>>t;
28     while(t--){
29         cin>>n>>k;
30         int ans=1;
31         k--;
32         while(k){
33             if(ans*10<=n){
34                 ans*=10;
35                 k--;
36             }
37             else{
38                 if(ans+1<=n){
39                     while(ans%10==9) ans/=10;
40                     ans++;
41                     k--;
42                 }
43                 else{
44                     ans/=10;
45                     while(ans%10==9) ans/=10;
46                     ans++;
47                     k--;
48                 }
49             }
50         }
51         cout<<ans<<endl;
52     }
53 
54 }
View Code
原文地址:https://www.cnblogs.com/Fighting-sh/p/10566060.html