首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

问个题目解决方案

2012-01-10 
问个题目有304瓶啤酒,每3个空瓶子能换一瓶新啤酒,问能喝到多少瓶啤酒?这个解题目的思路是什么? [解决办法]

问个题目
有304瓶啤酒,每3个空瓶子能换一瓶新啤酒,问能喝到多少瓶啤酒?这个解题目的思路是什么?


[解决办法]
3个空瓶喝一个 剩一个空瓶

相当于2个空瓶 喝一个

a=304/2

余0 a-1
余1 a


public class wer
{
public static int res(int m,int n)
{
int re=0;
n=n-1;

re=(m%n==0)?(m/n-1):(m/n);

return re;

}


public static void main(String[] args)
{
int result;

result=res(305,3);


System.out.println(result);

}
}
[解决办法]
public class Peer{
private static int num=0;//记录总共喝的数目
private static int i=0;//记录下一次换酒后的数目
public static void totalPeer(int n){
num+=n-n%3;
i=n%3+(n-(n%3))/3;
if(i <3)num+=i;
else{
//System.out.println(num);
totalPeer(i);
}
}
public static void main(String[] args){
totalPeer(304);
System.out.println( "The total peer you can drink is: "+num);
}
}
输出:
The total peer you can drink is: 455
没有考虑有关怎样换啤酒的方法。
[解决办法]
呵呵,等一下。

热点排行