题目传送门

题意:

将1~n个数按字典序排序后,求第k个数

 

思路:

hdu6468 zyb的面试 (思维)

 

代码:

 

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string>
#include<string.h>
#include<math.h>
#include<map>
using namespace std;
typedef long long ll;
int ans;
int n,k;
bool flag;
int cnt;


void dfs(int x,int &cnt)
{//cout<<x<<" "<<cnt<<endl;
    if(flag) return ;
    if(x>n||cnt>k) return ;
    if(cnt==k) {
        ans=x;
        flag=1;
        return ;
    }
    for(int i=10*x;i<10*(x+1);i++)
        {
          if(i>n) return ;//注意这里在hdu写成if(i<n)会超时
          dfs(i,++cnt);
        }

}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        ans=0;
        scanf("%d %d",&n,&k);
         cnt=0;
        flag=0;
        for(int i=1;i<=9;i++)
         dfs(i,++cnt);
        printf("%d\n",ans);
    }
    return 0;
}
View Code

相关文章:

  • 2021-11-09
  • 2021-12-25
  • 2021-10-23
  • 2021-09-09
  • 2021-07-27
  • 2021-11-20
  • 2022-01-31
猜你喜欢
  • 2021-09-16
  • 2022-12-23
  • 2021-08-04
  • 2021-09-24
  • 2022-12-23
  • 2021-08-03
相关资源
相似解决方案