zyb的面试

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 1000000007

struct 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;
            else
                k--;
            if(k==0)
                break;
        }
        cout<<a[i].w<<endl;
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值