题解:P12154 [蓝桥杯 2024 省 Java B] 逃离高塔

思路

一个数的立方数个位仅由该数的个位数决定。因此,我们只需确定哪些个位数的立方结果以 $3$ 结尾并统计以这些个位数结尾的数的出现次数。

因为在 $0$ 至 $9$ 这个范围内,只有 $7$ 的立方数个位是 $3$($7^3=343$)。

然后从 $1$ 到 $2025$ 中每 $10$ 个就会有一个数的个位为 $7$,最小为 $7$,最大为 $2017$。

所以,我们用这些数构成等差数列,首项为 $7$,末项为 $2017$,公差为 $10$。总项数公式为:

$$
\text{项数} = \frac{\text{末项} - \text{首项}}{\text{公差}} + 1 = \frac{2017 - 7}{10} + 1 = 202
$$

因此答案为 $202$。

代码

C++ 代码:

1
2
3
4
5
6
7
8
9
10
11
12
#include <bits/stdc++.h>
#define endl '\n'
using namespace std;

int main()
{
// ios::sync_with_stdio(false);
// cin.tie(nullptr);
// cout.tie(nullptr);
cout << 202 << endl;
return 0;
}

Java 代码:

1
2
3
4
5
public class Main {
public static void main(String[] args) {
System.out.println(202);
}
}