Interesting programming questions

1. There are 5 people sitting together and asking how old is the fifth person? He said that he is 2 years older than the fourth person. Asked the fourth person, he said that he is 2 years older than the third person. Asked the third person and said that he was two years older than the second person. Ask the second person, said that he is two years older than the first person. Finally asked the first person, he said that he is 10 years old. How old is the fifth person? (Use programming to achieve and say ideas)

思维: I don't know how old the fifth person is, but there is a connection between the fifth person and the first person. Knowing the age of one person, I know everyone. The age, according to the law, each person is 2 years old, you can use the for loop to simply program the fifth person's age

program implementation:

public static void main(String[] args) {
Int first = 10;//the age of the first person
Int age=first;//the age of the fifth person (temporary)
For (int i = 1; i < 5; i++) {//1 to 5 people, cycle four times
Age+=2;//every age +2
}
System.out.println("The age of the fifth person is: "+age+" years old)); / / final output
}

2.明明 A very simple question, the answer is varied

A: 10 yuan can buy 5 bottles, then there are 5 caps, 5 empty bottles, a total of 5 bottles,

4 caps for 1 bottle, at this time you can drink 6 bottles, 2 caps left, 6 empty bottles,

6 empty bottles for 3 bottles, then you can drink a total of 9 bottles 5 caps left, 3 empty bottles,

4 caps for 1 bottle, then you can drink 10 bottles, 2 caps left, 4 empty bottles,

4空Change the bottle for 2 bottles. At this time, you can drink 12 bottles, 4 caps left, 2 empty bottles,

4 caps for 1 bottle. At this time, you can drink 13 bottles and leave 1 bottle cap. 3 empty bottles,

2 empty bottles for 1 bottle, then you can drink a total of 14 bottles, 2 caps left, 2 empty bottles,

2 empty bottles for 1 bottle, then A total of 15 bottles can be drunk, 3 caps left, 1 empty bottle,

If you can borrow, borrow a bottle cap,

4 caps for 1 bottle, then you can drink a total of 16 bottles. 1 bottle cap left, 2 empty bottles,

2 empty bottles for 1 bottle, then you can drink a total of 17 bottles, 2 caps left, 1 empty bottle,

再借一空Bottle,

2 empty bottles for 1 bottle, then you can drink a total of 18 bottles, 3 caps, 1 empty bottle,

, then borrow a cap,

4 caps for 1 bottle, then you can drink 19 bottles, 1 cap, 2 empty bottles ,

2 empty bottles for 1 bottle, then you can drink a total of 20 bottles, 2 bottles left, 1 empty bottle,

Maybe the boss is angry at this time, and two caps, An empty bottle

, so if the boss does not borrow, you can drink 15 bottles, if you can borrow, you can drink 20 bottles