422 字
2 分钟
Friendly-Numbers

题面#

For an integer xx, we call another integer yy friendly if the following condition holds:

  • yd(y)=xy - d(y) = x, where d(y)d(y) is the sum of the digits of yy.

For a given integer xx, determine how many friendly numbers it has.

Input

Each test contains multiple test cases. The first line contains the number of test cases tt (1t5001 \le t \le 500). The description of the test cases follows.

Each test case consists of a single line containing one integer xx (1x1091 \le x \le 10^{9}).

Output

For each test case, output one integer — the answer to the problem.

思路#

对题目所给的式子变形可以得到:y=x+d(y)y = x + d(y)

也就是求符合条件的 yy 的个数

xx 是输入,已经给出,我们只需要确定 d(y)d(y) 即可

进行如下推理:

yykk 位,则 d(y)9kd(y) \leq 9k

NOTE

因为 d(y)d(y) 表示 yy 各位上的和,而每个数字位最大值为9

而注意到,当 k11k \geq 11 时,y1011y \geq 10^11

此时有:x=yd(y)101099109x = y - d(y) \geq 10^10 - 99 \geq 10^9

明显不符合题目条件:1x1091 \le x \le 10^{9}

因此,yy 最多只有10位

根据上面的推论,不难得到:d(y)9×10=90d(y) \leq 9 \times 10 = 90

因此可以得到 yy 的上界:y=x+d(y)x+90y = x + d(y) \leq x + 90

因此,我们只需要暴力枚举 [x,x+90][x, x + 90] 内范围的数,看他们是否满足条件即可,满足给对应计数器加一,否则不进行操作

代码#

#include <bits/stdc++.h>
using namespace std;
int calc(int num) {
string str = to_string(num);
int result = 0;
for (auto ch: str) {
result += ch - '0';
}
return result;
}
int main() {
int t;
cin >> t;
while (t --) {
int x;
cin >> x;
if (x == 1)
cout << 0 << endl;
else {
int count = 0;
for (int y = x + 1; y < x + 91; y ++) {
int d = calc(y);
if (y - d == x) {
count ++;
}
}
cout << count << endl;
}
}
}
Friendly-Numbers
https://github.com/posts/friendly-numbers/
作者
FZSGBall
发布于
2026-03-24
许可协议
CC BY-NC-SA 4.0