#include #include using namespace std;

// 判断一个数是否为"123"好数 bool is123Number(string num) { for (int i = 2; i < num.length(); i++) { if (num[i] == '3' && num[i - 1] == '2' && num[i - 2] == '1') { return false; } } return true; }

// 获取由"123"构成的n位数中第k个"123"好数 string getKth123Number(int n, int k) { string num = ""; int count = 0; while (count < k) { num += '1'; count++; if (count == k) { break; } num += '2'; count++; if (count == k) { break; } num += '3'; count++; if (count == k) { break; } } while (num.length() < n) { if (num[num.length() - 1] == '1') { num += '2'; } else if (num[num.length() - 1] == '2') { num += '3'; } else if (num[num.length() - 1] == '3') { num += '1'; } } while (!is123Number(num)) { num = getKth123Number(n, k + 1); } return num; }

int main() { int n, k; cin >> n >> k; cout << getKth123Number(n, k) << endl; return 0;

题目描述一个数中如果如果不含连续的12连续的23连续的31 就称作123好数那么 在由123构成的n位数中 第k个123好数是什么输入一行 两个正整数nk 用空格分隔输出一行 输出由123构成的n位数中 第k个123好数样例输入 复制6 14样例输出 复制113322提示【说明提示】1≤n≤20k不超过满足条件的123好数个数c++代码

原文地址: https://www.cveoy.top/t/topic/iDRX 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录