28人买可乐喝,3个可乐瓶盖可以换一瓶可乐,那么要买多少瓶可乐,够28人喝?假如是50人,又需要买多少瓶可乐?

王朝学院·作者佚名  2016-08-27  
宽屏版  字体: 小 | 中 | 大 | 超大  

packagecom.study.day07;importjava.util.Scanner;/*** 28人买可乐喝,3个可乐瓶盖可以换一瓶可乐,

* 那么要买多少瓶可乐,够28人喝?假如是50人,又需要买多少瓶可乐?(需写出分析思路)

* 答:28人需要买19瓶,50人需要买34瓶。

*@authordenny

*思路:1.先买一瓶

* 2.用瓶盖数/3计算可以换多个瓶

* 3.循环条件28瓶*/publicclassTest {publicstaticvoidmain(String[] args) {

Scanner input=newScanner(System.in);//多买多少个人喝的System.out.PRintln("请输入要买多少瓶");intcount=input.nextInt();

getCount(count);

}publicstaticvoidgetCount(intcount){inttemp=0;//瓶盖数intbuyPing=0;//当前瓶数intsendPing=0;while(sendPing+buyPing!=count){

buyPing++;//多买一瓶temp++;//瓶盖数+1if(temp/3==1){

sendPing++;//赠送的瓶+1temp=temp-2;//瓶盖重新计算 换了一瓶又多了一个瓶盖temp-3+1,所以是一2 也可以直接temp=1赋值}

}

System.out.println("买了: "+buyPing+" 送了: "+sendPing);

}

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
© 2005- 王朝网络 版权所有