2
猴子吃桃问题(25分)
题目内容:
猴子吃桃问题。猴子第1天摘了若干个桃子,当即吃了一半,还不解馋,又多吃了一个;第2天,吃剩下的桃子的一半,还不过瘾,又多吃了一个;以后每天都吃前一天剩下的一半多一个,到第n天想再吃时,只剩下一个桃子了。问第一天共摘了多少个桃子?
输入格式:
标准输入,剩下一个桃子的天数n。
输出格式:
标准输出,第一天共摘的桃子个数。
输入样例:
10
输出样例:
1534
my answer:
int main ()
{
int n;
int pre_day_total_pitch=1;
cin>>n;
for(int day=1;day<n;day++)//explor: why have we put "i<n" rather than "i<=n" ?
{
pre_day_total_pitch=(pre_day_total_pitch+1)*2;
}
cout<<pre_day_total_pitch;
return 0;
}
/*for "pre_day_total_pitch" is the amount of pitch the day before today,
"day=1,2,3..." is the inversed sequence of date;
when "day"==1 , "pre_day_total_pitch" mean's the amount of pitch of "day==2"
*/
if i want “day<=n”:
int main ()
{
int n;
cin>>n;
int pre_day_amount=1;//init
for(int day=2;day<=n;day++)
{
pre_day_amount=(pre_day_amount+1)*2;
}
cout<<pre_day_amount;
return 0;
}