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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 余旋 中级黑马   /  2015-11-9 13:32  /  308 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

28人买可乐喝,3个可乐瓶盖可以换一瓶可乐,那么要买多少瓶可乐,够28人喝?假如是50人,又需要买多少瓶可乐?
  1. public class SaleNum
  2. {
  3.         public static void main(String agrs[])
  4.         {
  5.                 int s = SaleNum.NeedBuy(28);
  6.                 System.out.print(s);
  7.         }
  8.         public static int NeedBuy(int num)
  9.         {
  10.                 int need = 0;
  11.                 while (true)
  12.                 {
  13.                         if (num<=SaleNum.Sale(need))
  14.                         {
  15.                                 break;
  16.                         }
  17.                         else
  18.                         {
  19.                                 need++;
  20.                         }
  21.                 }
  22.                 return need;
  23.         }
  24.         public static int Sale(int num)
  25.         {
  26.                 if(num<3)
  27.                         return num;
  28.                 else
  29.                         return num+duihuan(num);
  30.         }
  31.         public static int duihuan(int num)
  32.         {
  33.                 int temp = num/3;
  34.                 int sheyu = num%3;
  35.                 if(temp>0){
  36.                         temp+=duihuan(temp+sheyu);
  37.                 }                       
  38.                 return temp;
  39.         }
  40. }
复制代码

1 个回复

倒序浏览
兄弟注意了,我以前发过这样的帖子,被无情的删了
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马