[Daily Blue Bridge] 25. One-five-year provincial Java group real question "The cube becomes itself"

Hello, I am the little gray ape, a programmer who can write bugs!

Welcome everyone to pay attention to my column " Daily Blue Bridge ". The main function of this column is to share with you the real questions of the Blue Bridge Cup provincial competitions and finals in recent years, analyze the algorithm ideas, data structures and other content that exist in it, and help you learn To more knowledge and technology!

Title: The cube becomes itself

Observe the following phenomenon, the number obtained by adding the cubes of each number by bit is still its own.

1^3=1

8^3=512     5+1+2=8

17^3=4913     4+9+1+3=17

......

Could you please calculate the total number of all positive integers that meet the requirements, including 1, 8, 17?

Please fill in the number

Do not fill in any redundant or descriptive text

Problem-solving ideas:

As a fill-in-the-blank question, the relative difficulty is relatively simple. According to the normal thinking, we only need to enumerate and judge the number, but there should be a range for the enumerated object. We know that the larger the number , Then the cube of this number will of course be larger. When the cube of a number is large enough, obviously the sum of each digit of his number cannot be equal to the number, so we only need to set the enumeration range It can be between 1 and 99. After finding the cube of each number, add each digit of the cube to determine whether it is equal to the number.

Answer source code:

public class Year2015_Bt2 {

	public static void main(String[] args) {
		int ans = 0;
		for (int i = 1; i <= 99; i++) {
			int cube = i*i*i;
			int n=0;    //定义n来记录位数的和
			while (cube>0) {
				n += cube%10;    //对立方数求余得到个位上的数字,并将其相加
				cube /= 10;	//将立方数cube除以10,以便获取下一个个位上的数字		
			}		
			if (n==i) {
				ans++;
//				System.out.println(n);
			}
			
		}
		System.out.println(ans);
	}

}

 

 

Sample output:

There are deficiencies or improvements, and I hope that my friends will leave a message and learn together!

Interested friends can follow the column!

Little Gray Ape will accompany you to make progress together!

Guess you like

Origin blog.csdn.net/weixin_44985880/article/details/114486074
Recommended