You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

46 lines
1.3 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
int n, r;
//0-15之间的数字转为16进制的数字
char int_to_char(int a) {
return a <= 9 ? '0' + a : a - 10 + 'A';
}
//单个字母转换成数字
int char_to_int(char a) {
//用句人话描述就是:
//(1)如果是0到9之间的就返回a-'0'
//(2)如果是A-F之间的就返回10+a-'A'
return '0' <= a && a <= '9' ? a - '0' : 10 + a - 'A';
}
//深度优先搜索
void dfs(int x, string path, int sum) {
if (sum == n) {
cout << path << endl;
return;
}
for (int i = 0; i < -r; i++)
dfs(x - 1, path + int_to_char(i), sum + i * pow(r, x - 1));
}
int main() {
cin >> n >> r;
//求i位-r进制的每一位位数上的极大值和极小值
int x, Max = -INF, Min = INF;
int sum = 0;
for (x = 1;; x++) {
//因为r为负数那么-r代表对应的正数比如-16进制那么-r代表1616-1代表15也就是说这个数位的最大上限是15就是F
sum += (-r - 1) * pow(r, x - 1);
if (sum < Min) Min = sum;
if (sum > Max) Max = sum;
if (n >= Min && n <= Max) break;
}
//深搜
dfs(x, "", 0);
return 0;
}