Two kinds of questions and ingenious solutions to the problem of "three changes in soft drinks"

Source: Internet
Author: User
this year I met in the written interview of three companies including 360, summed up the question of two methods and ingenious solutions, to facilitate the recruitment or when an intellectual problem to start thinking. Ask one: Three empty bottles for a bottle of soda, a class bought 30 bottles of soda, to ensure that each person drink a bottle of soda, up to how many students? Ask two: Three empty bottles for a bottle of soda, a class has 30 students, to ensure that each person drink a bottle of soda, at least the number of bottles of water to buy.

I read this question in the third grade of my primary school in an "intellectual" book, and the reason I was so impressed was because it had a clever thought: For example, there are two empty bottles left, you can buy a bottle of soda like the boss, drink three empty bottles to the boss (equivalent to a bottle of soda), so there are two empty bottles can also drink a bottle of soda.
Now we look at the relatively simple question Fayi, because it is asking, logical reasoning is very smooth, such as we bought 30 bottles of soda, the first can provide 30 students, the remaining 30 empty bottles can be replaced 10 bottles of soda can supply the total number of students to 30+10 name, the next 10 empty bottles can be replaced 3 bottles of soda supply total 30 + 10+3 name, finally left 4 empty bottles can supply 1 students, finally left two empty bottles, according to the beginning of the "thought" can also provide 1 students can supply 30+10+3+1+1 = 45.
The implementation of this algorithm is also very simple, for everyone to refer to:
public class Sanhuanyi {

	/**
	 * @param args
	 *
	/public static void main (string[] args) {
		
		int init_bottl E_num = 30;//initial soda quantity
		int person_num = 0;//can supply the number of classmates
		
		Person_num = Getallbottlenum (init_bottle_num);
		System.out.println (Person_num);
		
	}
	static int getallbottlenum (int bottle_num) {
		if (bottle_num <= 0)
			return 0;
		int all_num = Bottle_num;//all_num Final quantity
		while (Bottle_num >= 3) {
			int temp = BOTTLE_NUM/3;
			All_num + = temp;
			Bottle_num = temp + bottle_num% 3;
		}
		All_num + = Bottle_num = = 2? 1:0;//The total number of two bottles to add 1
		return all_num;
	}

}
Ask a more ingenious solution, first sell a xiaoguanzi, and finally, perhaps you read the law two can think of themselves.
Ask law one is relatively simple, my written interview is to ask the law two, the question seems to be reversed, my initial idea is to supply 30 classmates, it seems to buy 20 bottles, and then according to the "three for one" principle feel on the total and 30 almost, and then in the vicinity of 20 to take a number such as 19,21 ... Finally find a number that needs the least amount of soda. This is, of course, a correct solution. But in the written test it is not possible to spend a lot of time to calculate a small problem, it is impossible to use the "bad" method in the interview to attract the interviewer. So introduce a "packet" algorithm to solve this problem. For example, the need to supply 30 students, according to "N for a" problem of the students into the "30/n" group, than so n=3, we put people into 30/3 = 10 groups, we assume that in order to supply them, the first group we need to supply 3 bottles of soda, and so the first group drank we have 3 empty bottles in the hands of 1 bottles , so we'll just have to buy two more bottles of soda when we supply the second group. When the second group finished, we had 3 empty bottles, and so on ... Until the last group, we need to buy 2 * (10-1) = 21 bottles of soda, but the last 3 empty bottles can be, so we can buy less one bottle is 20 bottles is enough (that is, the 30th classmate drink soda when the hand has two empty bottles, according to the first "thought" can be changed another bottle). But if a total of 31 students, the end of the group is still one person, then we just put the last group to drink the remaining 3 empty bottles to him, the result is 21 bottles; if altogether 32 students, had to buy one more bottle, the total is 22 bottles. Summary: First of all the total number of SUM/3 groups, and then you can use the formula 3+2* (SUM/3-1) to obtain the total number of soda bottles, if there is no remainder after grouping the total number of bottles minus 1, if the remaining 1 people, the total number of bottles is unchanged, if the remaining 2 people, the total number of bottles plus 1。 We can finally write a simple program to simulate this process:
public class Sanhuanyi {

	/**
	 * @param args
	 *
	/public static void main (string[] args) {

		int person_num = number of 30;//to be supplied
		int all_bottle_num = Getleastbottle (person_num);
		System.out.println (All_bottle_num);
		
	}

	static int getleastbottle (int person_num) {
		
		if (person_num <= 0)
			return 0;
		if (Person_num > 0 && person_num < 3)
			return person_num;
		int sum = 0;		
		int group_num = person_num/3;//can divide the number of groups
		int yushu = person_num% 3;//is grouped, the remainder
		sum = 3 + (group_num-1) * 2;
		if (Yushu = = 0) {
			sum--;
		} else if (Yushu = = 1) {
			
		}else{
			sum++;
		}
		
		return sum;
	}
}

Well, we now look back to ask Fayi ingenious solution: bought 30 bottles of soda, first can supply 30 students, left 30 empty bottles, we still use that "thought", find 15 students each separated two empty bottles, let them go to find the boss, then they can drink to soda, so directly with the 15 = 45 is done.

==================================================================================================

Author: nash_ welcome reprint, share with people is the source of progress.

Reprint Please keep the original address: http://blog.csdn.net/nash_/article/details/8235632

===================================================================================================

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.