Self Numbers

╰+攻爆jí腚メ 2023-07-07 04:59 70阅读 0赞

2.Self Numbers

  1. Description
  2. In 1949 the Indian mathematician D.R. Kaprekar discovered a class of numbers
  3. called self-numbers. For any positive integer n, define d(n) to be n plus
  4. the sum of the digits of n. (The d stands for digitadition, a term coined
  5. by Kaprekar.) For example, d(75) = 75 + 7 + 5 = 87. Given any positive
  6. integer n as a starting point, you can construct the infinite increasing
  7. sequence of integers n,d(n), d(d(n)), d(d(d(n))), .... For example, if you
  8. start with 33, the next number is 33 + 3 + 3 = 39, the next is 39 + 3 + 9 = 51, the next is 51 + 5 + 1 = 57,
  9. and so you generate the sequence 33, 39, 51, 57, 69, 84, 96, 111, 114, 120,
  10. 123, 129, 141, ... The number n is called a generator of d(n). In the sequence above, 33 is
  11. a generator of 39, 39 is a generator of 51, 51 is a generator of 57, and
  12. so on. Some numbers have more than one generator: for example, 101 has two
  13. generators, 91 and 100. A number with no generators is a self-number.
  14. There are thirteen self-numbers less than 100: 1, 3, 5, 7, 9, 20, 31, 42,
  15. 53, 64, 75, 86, and 97.
  16. Input
  17. No input for this problem.
  18. Output
  19. Write a program to output all positive self-numbers less than 10000 in
  20. increasing order, one per line.
  21. Sample Input
  22. Sample Output
  23. 1
  24. 3
  25. 5
  26. 7
  27. 9
  28. 20
  29. 31
  30. 42
  31. 53
  32. 64
  33. |
  34. | <-- a lot more numbers
  35. |
  36. 9903
  37. 9914
  38. 9925
  39. 9927
  40. 9938
  41. 9949
  42. 9960
  43. 9971
  44. 9982
  45. 9993

挺简单的

  1. public class Main {
  2. public static void main(String[] args) {
  3. int a=1,b=1,c=3;
  4. int []arr=new int[10009];
  5. while(a<10000) {
  6. if(a<10) {
  7. arr[a+a%10]=1;
  8. }
  9. if(a<100) {
  10. arr[a+a%10+a/10]=1;
  11. }
  12. if(a<1000) {
  13. arr[a+a/100+a/10%10+a%10]=1;
  14. }
  15. if(a<10000) {
  16. if (a+a/100%10+a/1000+a/10%10+a%10>10000) {
  17. break;
  18. }
  19. arr[a+a/100%10+a/1000+a/10%10+a%10]=1;
  20. }
  21. a++;
  22. }
  23. for(int i=1;i<9995;i++) {
  24. if (arr[i]==0) {
  25. System.out.println(i);
  26. }
  27. }
  28. }
  29. }

发表评论

表情:
评论列表 (有 0 条评论,70人围观)

还没有评论,来说两句吧...

相关阅读