题意:
将一个从 1 到 n 的数组进行字典序排序,输出第 k 个数字。
题解:
直接模拟字典序增加,注意范围,不会超时(范围较小)。
AC代码:
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <string>
#include <vector>
#include <bitset>
#include <stack>
#include <cmath>
#include <deque>
#include <queue>
#include <list>
#include <set>
#include <map>
#pragma comment(linker, "/STACK:1024000000,1024000000")
#define line printf("---------------------------\n")
#define mem(a, b) memset(a, b, sizeof(a))
#define pi acos(-1)
using namespace std;
typedef long long ll;
const double eps = 1e-9;
const int inf = 0x3f3f3f3f;
const int mod = 1e9+7;
const int maxn = 2000+10;
int main() {
int t;
cin >> t;
while(t--) {
int n, k;
cin >> n >> k;
int temp = 1;
while(k > 1) {
if(temp * 10 <= n) {
temp *= 10;
k--;
} else {
if(temp + 1 <= n) {
while(temp % 10 == 9) {
temp /= 10;
}
temp++;
k--;
} else {
temp /= 10;
while(temp % 10 == 9) {
temp /= 10;
}
temp++;
k--;
}
}
}
cout << temp << endl;
}
}