zyb的面试
Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u
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
思路: 看了代码就懂了
//struct A
//{
// ll x,y;
// bool operator < (const A & a) const
// {
// return x>a.x;
// }
//};
//priority_queue <A> q;
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <string>
#include <cstdio>
#include <vector>
#include <cmath>
#include <queue>
#include <deque>
#include <stack>
#include <map>
#include <set>
#define eps 0.0000000001
#define mem(a) memset(a,0,sizeof(a))
#define maxx 1e10
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
//priority_queue<int,vector<int>,greater<int> > q;
#define mod 1000000007struct A
{int w;char s[8];
}a[1000100];stack<int> st;
bool cmp(A a,A b)
{return strcmp(a.s,b.s)<0;
}
int main()
{for(int i=1;i<=1000000;i++){a[i].w=i;int t=i;while(t>0){st.push(t%10);t/=10;}for(int j=0;!st.empty();j++){a[i].s[j]=st.top()+'0';st.pop();}}sort(a+1,a+1+1000000,cmp);int T;cin>>T;while(T--){int n,k;scanf("%d %d",&n,&k);int i;for(i=1;k>0;i++){if(a[i].w>n)continue;elsek--;if(k==0)break;}cout<<a[i].w<<endl;}return 0;
}