[Daily Blue Bridge] 1. "Guess the Age" in the Java Group for the 13th Provincial Games

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!

Topic: Guess the age

The American mathematician Wiener (N. Wiener) has a premature intelligence and went to university at the age of 11. He was invited to give lectures at Tsinghua University in China in 1935-1936. Once, he attended an important conference and his young face was eye-catching. , So someone asked his age, he replied:

"The cube of my age is a 4 digit number, and the 4th power of my age is a 6 digit number. These 10 digits happen to contain 10 digits from 0 to 9, each of which appears exactly once. Please calculate it. How young was he then,

Directly submit his age number at that time through the browser,

Note: Do not submit the answering process or other explanatory text"

Answer source code:

package 一三年省赛真题;

import java.util.HashSet;
import java.util.Set;
public class One {

	public static void main(String[] args) {
		//在10岁到100岁之间寻找
		for (int i = 10; i < 100; i++) {
			int i3 = i*i*i;	//获取到3次方
			int i4 = i3*i;	//获取到4次方
			String s3 = i3 + "";	
			String s4 = i4 + "";
			
			//判断立方的结果是否是4位数,4次方的结果是否是6位数,字符是否是由10个不重复的数字组成
			//同时成立则输出年龄
			if (s3.length()==4&&s4.length()==6&&check(s3+s4)) {
				System.out.println(i);
			}
		}
	}

	/**
	 * 判断字符是否是由10个不重复的数字组成
	 * @param s 判断的字符
	 * @return true
	 * */
	private static boolean check(String s) {
		Set<Character> set = new HashSet<Character>();	//建立不存放重复字符的集合
//		HashSet<String> set = new HashSet<String>();
		for (int i = 0; i < s.length(); i++) {
			//将字符中的每一个元素单独拿出存放到集合中,若元素重复则不被存放
			set.add(s.charAt(i));	
		}
		return set.size()==10;	//字符长度等于10,返回True
	}
	
}

 

Sample output:

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

Interested friends can follow the column!

Little Grey Ape will accompany you to make progress together!

 

Guess you like

Origin blog.csdn.net/weixin_44985880/article/details/112393479