JAVA Blue Bridge Cup training algorithm lonely number

JAVA Blue Bridge Cup training algorithm lonely number

Resource constraints
Time limit: 1.0s Memory Limit: 256.0MB

Problem Description
  morality by saying: one two, two three, three things.
  For any positive integer n, we define d (n) plus the value of n for the composition of the various numbers and n. For example, d (23) = 23 + 2 + 3 = 28, d (1481) = 1481 + 1 + 4 + 8 + 1 = 1495.
  Thus, given any n as a starting point, you may be configured as an increasing sequence: n, d (n), d (d (n)), d (d (d (n))) ... For example, from 33 starts incremental sequence:
  33, 39, 51, 57, 69, 84, 96, 111, 114, 120, 123, 129, 141, ...
  we n is called d (n) of the generator, the number of columns in the above, 33 is a generator 39, 39 is a generator 51, and the like. Some figures may even have two generators, such as 101, 91 and 100 may be generated by. But there is no generator some figures, such as 42. We call such numbers called lonely figure.

Input format
  line, a positive integer n.

Output format
  in ascending order of the digital output of all lonely less than n, one per line.

Sample input
40

Sample output
. 1
. 3
. 5
. 7
. 9
20 is
31 is

Data size and conventions
  n <= 10000

(Do not ask, ask that violence)

import java.util.Scanner;

public class Main {
		public static void main(String[] args) {
			Scanner sca=new Scanner(System.in);
			int[] a=new int[10001];
			for(int i=1;i<10001;i++) {
				int sum=i;
				String s=String.valueOf(i);
				while(sum<=10000) {
					for(int j=0;j<s.length();j++) {
						int temp=s.charAt(j)-'0';
						sum+=temp;
					}
					if(sum>10000) {
						break;
					}
					a[sum]=1;
					s=String.valueOf(sum);
				}
			}
			int n=sca.nextInt();
			for(int i=1;i<=n;i++) {
				if(a[i]==0) {
					System.out.println(i);
				}
			}
		}
	}
	

发布了17 篇原创文章 · 获赞 0 · 访问量 367

Guess you like

Origin blog.csdn.net/qq_36551453/article/details/104488688