禁止转载,该文章内容并非教学,仅为个人笔记。
问题描述
有些数字的立方的末尾正好是该数字本身。
比如:1,4,5,6,9,24,25,…
请你计算一下,在10000以内的数字中(指该数字,并非它立方后的数值),符合这个特征的正整数一共有多少个。
代码实现
#include <iostream>
#include <cmath>
using namespace std;
int ToModel(int i)
{
int model = 1;
while (i)
{
model *= 10;
i /= 10;
}
return model;
}
int main()
{
int count = 0;
for(int i = 1; i <= 10000 ; ++i)
{
long long result = pow(i,3);
if(result % ToModel(i) == i)
cout << ++count << ":" << i << " => " << result << endl;
}
return 0;
}
运行结果
1:1 => 1
2:4 => 64
3:5 => 125
4:6 => 216
5:9 => 729
6:24 => 13824
7:25 => 15625
8:49 => 117649
9:51 => 132651
10:75 => 421875
11:76 => 438976
12:99 => 970299
13:125 => 1953125
14:249 => 15438249
15:251 => 15813251
16:375 => 52734375
17:376 => 53157376
18:499 => 124251499
19:501 => 125751501
20:624 => 242970624
21:625 => 244140625
22:749 => 420189749
23:751 => 423564751
24:875 => 669921875
25:999 => 997002999
26:1249 => 1948441249
27:3751 => 52776573751
28:4375 => 83740234375
29:4999 => 124925014999
30:5001 => 125075015001
31:5625 => 177978515625
32:6249 => 244023456249
33:8751 => 670151588751
34:9375 => 823974609375
35:9376 => 824238309376
36:9999 => 999700029999