28人买可乐喝,3个可乐瓶盖可以换一瓶可乐,那么要买多少瓶可乐,够28人喝?假如是50人,又需要买多少瓶可乐?
- public class SaleNum
- {
- public static void main(String agrs[])
- {
- int s = SaleNum.NeedBuy(28);
- System.out.print(s);
- }
- public static int NeedBuy(int num)
- {
- int need = 0;
- while (true)
- {
- if (num<=SaleNum.Sale(need))
- {
- break;
- }
- else
- {
- need++;
- }
- }
- return need;
- }
- public static int Sale(int num)
- {
- if(num<3)
- return num;
- else
- return num+duihuan(num);
- }
- public static int duihuan(int num)
- {
- int temp = num/3;
- int sheyu = num%3;
- if(temp>0){
- temp+=duihuan(temp+sheyu);
- }
- return temp;
- }
- }
复制代码 |
|