蓝桥杯 寂寞的数

问题描述
  道德经曰:一生二,二生三,三生万物。
  对于任意正整数n,我们定义d(n)的值为为n加上组成n的各个数字的和。例如,d(23)=23+2+3=28, d(1481)=1481+1+4+8+1=1495。
  因此,给定了任意一个n作为起点,你可以构造如下一个递增序列:n,d(n),d(d(n)),d(d(d(n)))....例如,从33开始的递增序列为:
  33, 39, 51, 57, 69, 84, 96, 111, 114, 120, 123, 129, 141, ...
  我们把n叫做d(n)的生成元,在上面的数列中,33是39的生成元,39是51的生成元,等等。有一些数字甚至可以有两个生成元,比如101,可以由91和100生成。但也有一些数字没有任何生成元,如42。我们把这样的数字称为寂寞的数字。
输入格式
  一行,一个正整数n。
输出格式
  按照升序输出小于n的所有寂寞的数字,每行一个。
样例输入
40
样例输出
1
3
5
7
9
20
31
数据规模和约定
  n<=10000
不会做,参考自https://blog.csdn.net/coding__girl/article/details/76302157
 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 int flag[10010]; //flag[i]=0表示i为寂寞的数 
 4 int main() {
 5     int n;
 6     cin >> n;
 7     for (int i = 1; i < n; i++) {
 8         int sum = i; //sum表示d(i)的值 
 9         int m = i; //拆出i的每一位,用中间变量,避免直接对i操作,会影响for循环的 
10         while (m) {
11             sum += m % 10;
12             m /= 10;
13         }
14         flag[sum] = 1; //表示sum不为寂寞的数,标记为1
15     }
16     for (int i = 1; i < n; i++) {
17         if (flag[i] == 0) {
18             cout << i << endl;
19         }
20     }
21     return 0;
22 }
原文地址:https://www.cnblogs.com/fx1998/p/12656797.html