A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© lanzy1989 中级黑马   /  2014-9-20 21:20  /  1418 人查看  /  8 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

28人买可乐喝,3个可乐瓶盖可以换一瓶可乐,那么要买多少瓶可乐,够28人喝?假如是50人,又需要买多少瓶可乐?(需写出分析思路
  1. import java.io.*;

  2. public class TestString extends Thread {
  3.         public static void main(String[] args) throws Exception {

  4.                 method_1(28);
  5.         }

  6.         private static void method_1(int p) throws Exception {

  7.                 // 当是3的倍数时,需要多拿一瓶,
  8.                 if (p % 3 == 0) {
  9.                         int p1 = mehod_2(p + 1);
  10.                         System.out.println(p1);
  11.                 }
  12.                 // 当不是3的倍数是不需要多拿,
  13.                 else {
  14.                         int p1 = mehod_2(p);
  15.                         System.out.println(p1);
  16.                 }
  17.         }

  18.         // 定义方法返回需要的瓶数
  19.         private static int mehod_2(int p) {
  20.                 int i = 0, j = 0, p1 = 0, p2 = 0, s = 0;
  21.                 while (s != p) {
  22.                         p1++;
  23.                         p2 = p1;
  24.                         s = p2;
  25.                         while (p2 >= 3) {
  26.                                 i = p2 % 3;
  27.                                 j = p2 / 3;
  28.                                 p2 = i + j;
  29.                                 s += j;
  30.                         }
  31.                 }
  32.                 return p1;
  33.         }
  34. }
复制代码


8 个回复

倒序浏览
本帖最后由 java_dream 于 2014-9-20 23:52 编辑

就这么简单:
  1. public class BuyKeLe{
  2.        
  3.         public static void main(String[] args){
  4.                 int people = 28;
  5.                 System.out.println(people+"个人,需要购买"+buy(people,1,0)+"瓶可乐");
  6.         }
  7.        
  8.         /**
  9.          *
  10.          * @param people 喝饮料的人数
  11.          * @param count  购买的可乐数量
  12.          * @param give   赠送的可乐数量
  13.          * @return       返回购买的可乐数量
  14.          */
  15.         public static int buy(int people,int count,int give){
  16.                 //每次手里有3瓶(包括购买的和赠送的)就获赠1瓶
  17.                 give = (count+give)%3==0 ? give+1 : give;
  18.                 //如果购买的可乐数量+赠送的可乐数量还不够,继续购买
  19.                 if((count+give)<people){
  20.                         return buy(people,++count,give);
  21.                 }else{//已经够喝了,返回需购买的数量
  22.                         return count;
  23.                 }
  24.         }
  25. }
复制代码




回复 使用道具 举报
看起来二楼的更清晰啊。
回复 使用道具 举报
本帖最后由 new999 于 2014-9-21 09:46 编辑
  1. package com.itheima.test;

  2. public class EmptyBottles{
  3.         public static void main(String[] args){
  4.                 final int pNum=3;
  5.                 //3空瓶=1瓶可乐+1可乐,也就是1空瓶=0.5瓶可乐,所以初始1瓶可乐=1.5瓶可乐
  6.                 //因此,最初只要买pNum*/1.5瓶可乐,就够pNum人喝
  7.                 int num=pNum*2/3;
  8.                 System.out.println("方法1:买来"+(pNum%3==0?num:num+1)+"瓶可乐,够"+pNum+"人喝");
  9.                 System.out.println("方法2:买来"+getBottles(pNum)+"瓶可乐,够"+pNum+"人喝");
  10.         }

  11.         private static int getBottles(int pNum) {
  12.                 //empty用于记录空瓶数,sum是实际喝到的瓶数,bottles2是换来的可乐。
  13.                 int sum, empty,bottles2;
  14.                 for (int i = pNum>>>1; i < pNum; i++) {
  15.                         sum = i;//初始瓶数i,喝掉i瓶,
  16.                         empty = i;//产生i瓶空瓶
  17.                         while ((bottles2=empty/3)!=0) {//空瓶换可乐
  18.                                 sum += bottles2;//喝掉换来的可乐,
  19.                                 empty = bottles2 + empty%3;//产生的空瓶再加上上次剩下的空瓶
  20.                         }
  21.                         if(empty==2){//向老板赊来1瓶,最后用3个空瓶偿还
  22.                                 sum++;
  23.                         }
  24.                         //System.out.println("买来"+i+"瓶可乐,够"+sum+"人喝,最后剩下个"+empty+"空瓶");
  25.                         if(sum>=pNum){
  26.                                 return i;
  27.                         }
  28.                 }
  29.                 return pNum;
  30.         }
  31. }
复制代码
回复 使用道具 举报
买来34瓶可乐,够50人喝
回复 使用道具 举报
买来19瓶可乐,够28人喝
回复 使用道具 举报
new999 中级黑马 2014-9-20 23:19:45
7#

换来的可乐,喝完产生的空瓶,也是可以拿来换可乐的
回复 使用道具 举报
我的术测试题里就有这个
回复 使用道具 举报
new999 发表于 2014-9-20 23:19
换来的可乐,喝完产生的空瓶,也是可以拿来换可乐的

谢谢提醒,考虑不全,稍微修改就好了
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马