选择题










判断题



编程题
1. 幸运数

C++代码实现
cpp
#include <iostream>
using namespace std;
typedef long long LL;
// 奇数位的变换函数
int transform(int d) {
int x = d * 7;
// 只要结果大于 9,就继续各位相加
while (x > 9) {
int sum = 0;
int temp = x;
while (temp > 0) {
sum += temp % 10;
temp /= 10;
}
x = sum;
}
return x;
}
void solve() {
LL num;
cin >> num;
int total_sum = 0;
int pos = 1; // 记录当前是第几位(从个位开始)
while (num > 0) {
int digit = num % 10; // 提取当前位
if (pos % 2 == 1) {
// 奇数位:进行变换
total_sum += transform(digit);
} else {
// 偶数位:保持不变
total_sum += digit;
}
num /= 10; // 处理下一位
pos++;
}
// 判断总和是否为 8 的倍数
if (total_sum % 8 == 0) {
cout << "T" << endl;
} else {
cout << "F" << endl;
}
}
int main() {
int n;
if (!(cin >> n)) return 0;
while (n--) {
solve();
}
return 0;
}

2. 图像压缩

C++代码实现
cpp
#include <iostream>
#include <string>
#include <algorithm>
#include <cmath>
using namespace std;
const int N = 30;
int n, m;
int a[N][N], cnt[300], target[20];
struct Node {
int v, f;
} b[300];
// 排序规则:频次降序,频次相同时灰阶值升序
bool cmp(Node x, Node y) {
if (x.f != y.f) return x.f > y.f;
return x.v < y.v;
}
// 十六进制字符转十进制数值
int get_v(char c) {
if (c >= '0' && c <= '9') return c - '0';
return c - 'A' + 10;
}
// 数值转十六进制字符(大写)
char to_h(int x) {
return x < 10 ? x + '0' : x - 10 + 'A';
}
int main() {
// 1. 读取行数并解析十六进制像素数据
if (!(cin >> n)) return 0;
for (int i = 1; i <= n; i++) {
string s; cin >> s;
m = s.size() / 2; // 每两个字符代表一个像素
for (int j = 1; j <= m; j++) {
a[i][j] = get_v(s[2 * j - 2]) * 16 + get_v(s[2 * j - 1]);
cnt[a[i][j]]++;
}
}
// 2. 统计并筛选出现频次最高的 16 种灰阶
for (int i = 0; i < 256; i++) b[i] = {i, cnt[i]};
sort(b, b + 256, cmp);
// 输出选定的 16 种灰阶编码
for (int i = 0; i < 16; i++) {
target[i] = b[i].v;
cout << to_h(target[i] >> 4) << to_h(target[i] & 15);
}
cout << endl;
// 3. 映射原图像像素到目标灰阶索引
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
int res = 0, min_d = 300;
// 遍历 16 种目标灰阶,寻找最近邻
for (int k = 0; k < 16; k++) {
int cur_d = abs(a[i][j] - target[k]);
// 寻找绝对值最小的距离,若相等则保留编号较小的 k
if (cur_d < min_d) {
min_d = cur_d;
res = k;
}
}
cout << to_h(res);
}
cout << endl;
}
return 0;
}
