想的太简单导致写错啦,改正发上来,真是服了服了..
public static void main (String[] args){
int peopleNumber=50;
int count=howMany(peopleNumber,0);
System.out.print("需要买"+count+"瓶即可");
}
public static int howMany (int peopleNum,int bottleCount){
if ((bottleCount+exChange(bottleCount,0)) ==peopleNum){ //买的瓶数+瓶盖可换的瓶数等于人数时结束
// if ((bottleCount+bottleCount/3) ==peopleNum){ //原来是这样写的,真是怀疑自己智商,忘了换回来的瓶子还有瓶盖.....
return bottleCount;
}
bottleCount++;
//递归调用
return howMany(peopleNum,bottleCount);
}
public static int exChange(int capCount,int bottleCount){ //拿n个瓶盖能换多少瓶
if (capCount/3>0){
capCount-=3;
bottleCount++;
capCount++;
}else
{
return bottleCount;
}
return exChange(capCount,bottleCount);
} |
|