Guess the age question (Lanqiao Cup real question)
Problem Description
Xiao Ming took two sisters to the Lantern Festival Lantern Festival, and others asked them how
old they were . They naughty said: "The product of our age is 6 times the sum of our age."
Xiao Ming added: "They are not twins, and the difference in age is no more than 8 years old".
Please write down the age of your younger sister.
Thinking analysis
1. The age product of the younger sister is 6 times the sum of the ages
2. The age difference between the sisters is <= 8
3. The age of the sisters is not equal.
Here, my problem-solving method is enumeration, and the younger sister’s age is i, and the older sister is the
one-level cyclic sister j The age i is 0 to 30, then the age j of the second-level loop sister should be i+1 to i+8 and
finally add the if judgment, and output the result
In summary, the code is as follows:
public static void main(String[] args) {
for(int i=0;i<=30;i++) {
//i为较小的妹妹的年龄
for(int j=i+1;j<=i+8;j++) {
//j为较大的姐姐的年龄
if((i+j)*6==i*j) {
System.out.println(i+" "+j);
}
}
}
}
Output result
Sister is 10 years old, sister is 15 years old
(More on next week)