1、编程的写法:
using System; using System.Collections.Generic; using System.Text; namespace SlowX.CalcEgg {class Program {protectedstaticstring ShowStr(int theResult,int KP,int PG) {return theResult +"瓶啤酒、" + KP +"空瓶、" + PG +"瓶盖"; }staticvoid Main(string[] args) {// 啤酒2块一瓶,4个瓶盖可换1瓶,2个空瓶可换1瓶,10块钱可以喝多少瓶?int theResult =5;int KP =5;// 空瓶int PG =5;// 瓶盖string str =null;int dV =0;int mV =0;bool isBreak =true; str= ShowStr(theResult, KP, PG); Console.WriteLine(str);while (true) { isBreak=true; dV= KP /2;// 兑换多少啤酒// mV = KP %2;// 剩余多少空瓶// if (dV !=0) { theResult+= dV; KP= mV + dV; PG+= dV; str= dV *2 +"空瓶兑换" + dV +"瓶啤酒," + ShowStr(theResult, KP, PG); Console.WriteLine(str); isBreak=false; } dV= PG /4;// 兑换多少啤酒// mV = PG %4;// 剩余多少瓶盖// if (dV !=0) { theResult+= dV; KP+= dV; PG= mV + dV; str= dV *4 +"瓶盖兑换" + dV +"瓶啤酒," + ShowStr(theResult, KP, PG); Console.WriteLine(str); isBreak=false; }if (!isBreak)continue;// 借瓶模式// if (KP ==1) {// 借一空瓶,还一空瓶,得一瓶啤酒+一瓶盖 theResult +=1; KP=0; PG+=1; str="借一空瓶,还一空瓶,得一瓶啤酒," + ShowStr(theResult, KP, PG); Console.WriteLine(str); isBreak=false; }if (!isBreak)continue;if (PG ==3) {// 借一空瓶,还一空瓶,得一瓶啤酒+一瓶盖// theResult +=1; PG=0; KP+=1; str="借一瓶盖,还一瓶盖,得一瓶啤酒," + ShowStr(theResult, KP, PG); Console.WriteLine(str); isBreak=false; }if (!isBreak)continue;break; }// 最后逻辑// if (KP ==0 && PG ==2) {// 剩下2瓶盖的模式// theResult +=1; PG= -1; KP=1; str="借两瓶盖,还一瓶盖,得一瓶啤酒," + ShowStr(theResult, KP, PG); Console.WriteLine(str); theResult+=1; PG=0; KP=0; str="借一空瓶,还一空瓶一瓶盖,得一瓶啤酒," + ShowStr(theResult, KP, PG); Console.WriteLine(str); } str= ShowStr(theResult, KP, PG); Console.WriteLine(str); } } }