一只猴子第一天摘下若干个桃子,当即吃了一半,不过瘾,又多吃了一个。第二天又将剩下的桃子吃了一半,又多吃了一个。以后每天都吃了前一天剩下的一半零一个。第10天再想吃时,发现只剩下一个桃子。请问第一天猴子共摘了多少桃子?
解决方案 »
- 大伙来看看这两个方法是重载方法么?
- java代码结果输出,大家来探讨一下
- OutputStream 转成 InputStream 流
- 实现键盘输入姓名,年龄,性别等信息并写入文件
- 求助 类似ICQ的聊天工具加密 悉尼大学engineering学院某project
- Java传输Word文件
- 极度郁闷中,各位高手救命啊!关于数据库查询的古怪问题
- 小弟拜求一个Java编的停车场管理系统,在线等!!
- Eclipse 的快捷键的问题!
- 作为一个初学者,想问在国内网站能否下载Java开发工具?
- Date today=new Date() 获取的不是现在的日期么??
- jax-ws发布WS,用axis的wsdl2java生成的客户端调用有问题
写错了,应该是1534个,
package com.potevio.telecom.simple;public class MonkeyEatPeach { /**
* @param args
*/
public static void main(String[] args) {
int totalpeach = 0;
int day = 10;
int lastdaypeach = 1;
totalpeach = totalPeach(10,1);
System.out.println(totalpeach);
} private static int totalPeach(int day,int lastdaypeach){
int total = 0;
int loopnum = day - 1;
int temp = lastdaypeach + 1 ;
for(int i = 0 ; i < loopnum ; i++){
total = temp * 2;
temp = total+1;
System.out.println(total);
}
return total;
}
}
public class Monkey {
public static void main(String args[]){
int sum = 1;
for(int i = 1; i < 10; i++)
sum = (sum + 1) * 2;
System.out.println(sum);
}
}
public static void main(String args[]) {
int peach=totalPeach(1);
System.out.println(peach);
}
public static int totalPeach(int day){
if(day==10){
return 1;
}
return 2*(totalPeach(day+1)+1);
}
}
public class Test{
public static void main(String args[]) {
int peach=totalPeach();
System.out.println(peach);
}
public static int totalPeach(){
int sum=1; //第10天的桃子数;
for(int day=9;day>=1;day--){
sum=(sum+1)*2;
}
return sum;
}
}循环和递归都可以,循环效率高一些。
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
useLoop();
int count = 1;
count = useRecurrent(10);
System.out.println("cou" + count);
}
public static void useLoop()
{
int tomato = 1;
int days = 10;
for(int i = 1; i < days; ++i)
{
tomato = (tomato + 1) * 2;
}
System.out.println(tomato);
}
public static int useRecurrent(int days)
{
if(days == 1)
{
return 1;
}
else
{
return (useRecurrent(--days) + 1) * 2;
}
}}