问题:10块钱,2块钱一瓶酒可以买5瓶酒,2个瓶子可以换一瓶酒,4个盖子也可以换一瓶酒,一共可以换几瓶酒 。
想了一下,就个问题可以用算法来实现嘛 ,下在是我的代码哈哈.
package pp1;public class t1 { private int pg = 0; private int pz = 0; private int count = 0 ; public t1(int pg,int pz){ this.pg = pg; this.pz = pz ; } //处理瓶盖 ,4瓶盖换一瓶 public void addpg(){ int i = 0 ; //兑现得到的 if(pg>=4){ i = pg/4 ; pg= pg%4 ; count +=i; pg += i ; pz += i ; } } //处理瓶子 , 2 瓶子换一瓶 public void addpz(){ int i = 0 ; if(pz>=2){ i = pz/2 ; pz= pz%2 ; count+=i; pz += i ; pg += i ; } } public void eat(){ addpz(); addpg(); if(pg<4&&pz<2){ System.out.println("count = "+count); System.out.println("剩下pz = "+pz+",剩下pg = "+pg); }else{ eat(); } } public int getCount(){ return count; } public static void main(String[] args) { int c1 = 10/2 ; //10块钱,2块一瓶 t1 t = new t1(c1,c1); t.eat(); System.out.println("结果共能喝:"+(c1+t.getCount())); }}
运行结果是: