|
发表于 2023-3-18 13:51:38
|
显示全部楼层
C++#include <iostream>
using namespace std;
// 用来判断 N 的各个位加起来结果是否是 X
bool f(int num, int X) {
int sum = 0;
while (num) {
sum += num % 10;
num /= 10;
}
return sum == X;
}
int main(void) {
int L, D, X;
cin >> L >> D >> X;
// 打印 N
for (int num = L; num <= D; ++num) {
if (f(num, X)) {
cout << num << endl;
break;
}
// 打印 M
for (int num = D; num >= L; --num) {
if (f(num, X)) {
cout << num << endl;
break;
}
}
return 0;
}
PythonL, D, X = map(int, (input(), input(), input()))
f = lambda num, x: sum(map(int, iter(str(num)))) == x
N = min([num for num in range(L, D + 1) if f(num, X)])
M = max([num for num in range(L, D + 1) if f(num, X)])
print(N, M, sep = '\n')
|
|