The problem is as follows:
Dark Horse Programmer Training camp Entrance Exam Questions
10, 28 people buy cola drink, 3 Coke bottle cap can change a bottle of Coke, then how many bottles of Coke to buy, enough 28 people to drink? If it is 50 people, how many bottles of Coke do you need to buy? (Need to write analysis ideas)
My train of thought 1:
The use of reverse thinking and global thinking, if 28 have been drinking coke, then there will be 28 caps, you can redeem 28/3 = 9 (truncated, can buy less can not buy more) bottles of coke, then altogether have to buy 28-9 = 19 bottles of Coke is enough for 28 people to drink.
Similarly: 50 people drink cola, need 50-50/3 =50-16= 34 bottles of Coke
Of course, this mathematical calculation is too easy to write code, I will not demonstrate.
My train of thought 2:
Using the multi-threaded wait-wake mechanism, the code is as follows:
/* Dark Horse Programmer training Camp Entrance Examination Questions 10, 28 people buy cola drink, 3 Coke bottle cap can change a bottle of Coke, then how many bottles of Coke to buy, enough to drink 28 people? If it is 50 people, how many bottles of Coke do you need to buy? (Need to write analysis ideas) Use reverse thinking to think: 28 personal if all drink Coke, then there will be 28 caps, you can exchange 28/3 = 9 bottles of Coke. Then altogether need to buy 28-28/3 = 19 bottle Coke to drink. Similarly, 50 people need to buy a 50-50/3 = 34 bottle of Coke. Problem Solving ideas: look at the model of this problem a bit like the example of producer and consumer define two threads a thread to buy a Coke: a thread to collect bottles to redeem Coke *///in order to highlight the focus, The following member variables do not have to be privately privatized. import java.util.concurrent.locks.*;class kele{int need = 0;//the number of people who need to drink Coke int buycount = 0;//already bought the number of cola int returncount = 0;//through the lid to redeem the number of Coke int drokedcount = 0; //drink the number of Coke, that is, the number of lids. boolean flag = false; //record Whether everyone is drinking a coke. boolean flagbuyed = false; //switch to buy coke and change the lid of the thread//define a lock private lock lock = new reentrantlock ();p rivate condition con_buy = lock.newcondition ();p rivate cOndition con_col = lock.newcondition ();p Ublic kele (int need) {this.need = Need;} Public void buy ()//Buy Cola {lock.lock (); Try{if (flagbuyed) //if you buy a bottle of Coke, You have to recalculate whether the remaining caps can be exchanged 3 for a bottle of Coke. {try{con_buy.await ();} catch (exception e) {throw new runtimeexception ("Wait for Exception");}} buycount++; //Buy 1 bottles of Coke for 1 people to drink drokedcount++; //at the same time buy 1 bottles of Coke will be more than 1 lid flagbuyed = True;con_col.signal ();} catch (exception e) {throw new runtimeexception (e.tostring ());} Finally{lock.unlock ();}} Public void col ()//Redeem lid {lock.lock (); Try{if (!flagbuyed) {try{con_col.await ();} catch (exception e) {throw new runtimeexception ("Wait for Exception");}} Mode 1-reasoning in the usual way (I do not know why this way for 28 results is 20, one more, and for 50, the result is 34, just right.) if (drokedcount == 3) //if the number of caps collected reaches 3, change 1 bottles of coke and the lid is left. {//returncount++;//drokedcount = 1; //Each time it is zeroed out, but it also has a more lid. }//Mode 2-Convert the conditions: 3 lid for a bottle of Coke, equivalent to 2 lids for a bottle of Coke if without lid (DROKEDCOUNT&NBSP;==&NBSP;2) //If the number of caps collected reaches 3, change 1 bottles of coke and the lid is left. {returncount++;d rokedcount = 0; //just a bottle of Coke, but the lid is not counted. }flagbuyed = false;con_buy.signal ();} catch (exception e) {throw new runtimeexception (e.tostring ());} Finally{lock.unlock ();}}} class buyer implements runnable//to buy Coke thread {kele k;public buyer (Kele k) {this.k = k;} Public void run () //buy cola {while (k.buycount + k.returncount < K.need) {k.buy ();} K.flag = true;}} Class collector implements runnable//collects the lid of the thread and returns the Coke. {Kele k;public collector (kele k) {this.k = k;} Public void run () {while (k.buycount + k.returncount < k.need) {K.col ();} K.flag = true;}} Class rxtest10{public static void main (String[] args) throws Exception{kele res = new keleNew thread (New buyer (res)). Start (); New thread (New collector (res)). Start (); while (!res.flag) {}// Two sub-threads do not end, resolutely do not let the main thread output. is equivalent to blocking the main thread. Quite a two sub-threads join, and they can be executed alternately. Prevents a child thread from starting output without marking the label's main thread. System.out.println (res.need + "Individual needs to buy" + res.buyCount + "Bottle Cola");}}
About this problem, Baidu a bit, there are a lot of solutions for reference, its a drink coke problem
This article is from the "line of the World" blog, please be sure to keep this source http://4259297.blog.51cto.com/4249297/1663206
The problem of Java data-drinking coke