題意:
將一個從 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;
}
}