一つの数は二進法で表してもいいし、十進法で表してもいいです.この数の二進法のすべてのビット数の和は十進法のすべての桁数の和に等しい場合、この数を不思議数と呼びます.

983 ワード

import java.util.LinkedList;
import java.util.Scanner;
import java.util.Stack;

public class suanFa01 {
	public static void main(String[] args) {
		Stack stack = new Stack();
		LinkedList list = new LinkedList();
		Scanner console = new Scanner(System.in);
		int M = console.nextInt();
		int sum1 = 0;
		int sum2 = 0;
		Find(M,stack,sum1,list,sum2);
	}
	public static void Find(int M,Stack stack,int sum1,LinkedList list,int sum2){
		for(int i = 1; i <= M; i ++){
			int temp = i;
		while(i != 0){
			int x = i % 2;
			i /= 2;
			stack.push(x);
		}
		while(stack.size() > 0){
			int y = stack.pop();
			sum1 += y;
		}		
			i = temp;
		while(i != 0){
			int z = i % 10;
			i /= 10;
			sum2 += z;
		}
		if(sum1 == sum2){
			list.add(temp);
		}
			i = temp;
			sum1 = 0;
			sum2 = 0;
		}
		System.out.println(list.size());
		System.out.println(list);
	}
}	
100
3
[1, 20, 21]