C++循环语句作业,急```做对,而且快手的```马上给分````

题目如下:
有一种生物叫jackalope,它们每一代都以3%的成长速度增长,又以1%速度死亡.例如,原本有132个jackalope的,1代之后,3个增长了(132 * 0.03 = 3.96, 约成3个,因为是生物,不能多,只能少),然后总数1%死亡,就是剩下134个,以后每一代如此类推.

问user开始有多少jackalope, 要看多少代, 目标总数.

规格如下:
How many jackalopes do you have? 200
How many generations do you want to wait? 1
What is your target population? 205
If you start with 200 jackalopes and wait 1 generations,
you'll end up with a total of 204 of them.
If you start with 200 jackalopes, it will take 2 generations
to reach a population of 205

#include <iostream>
using namespace std;
int main()
{
cout << "How many jackalopes do you have? ";
int start,tempstart;
cin >> start;
tempstart = start;
cout << "How many generations do you want to wait? ";
int gen,tempgen;
cin >> gen;
tempgen = gen;
cout << "What is your target population? ";
int end,tempend;
cin >> end;
tempend = end;
int i = 0;
while(gen--)
{
float tempplus = start * 0.03;
int plus = (int)tempplus;
start += plus;
start -= int(start * 0.01);
}
cout << "If you start with " << tempstart << " jackalopes and wait " << tempgen << " generations, you'll end up with a total of " << start << " of them." << endl;
start = tempstart;
end = tempend;
while(1)
{
float tempplus = start * 0.03;
int plus = (int)tempplus;
start += plus;
start -= int(start * 0.01);
if(start >= end)
{
cout << "If you start with " << tempstart << " jackalopes, it will take " << ++i << " generations to reach a population of " << end << endl;
break;
}
++i;
}
return 0;
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2009-03-26
void main()
{
float total,target,temp;
int gen;
cout<<"How many jackalopes do you have?";
cin>>total;
temp=total;
cout<<"How many generations do you want to wait?";
cin>>gen;
cout<<"What is your target population? ";
cin>>target;
for(int i=1;i<=gen;i++)
{
total=(int)(total*(1+0.03));
total=(float)(total)*(1-0.01);
if(total<target)
gen++;
}
cout<<"If you start with "<<temp<<" jackalopes and wait "<<gen<<" generations, "<<endl;
cout<<"you'll end up with a total of "<<total<<" of them."<<endl;
cout<<"If you start with "<<temp<<" jackalopes,it will take "<<gen<<" generations
to reach a population of "<<target<<endl;
}
第2个回答  2009-03-26
楼上两位没考虑期待值在预定的代数内就达到的结果吧,

比如上面的代数是3的情况,

#include <iostream>
#include <stdarg.h>
using namespace std;

const double BEARRATE = 0.03;
const double DEADRATE = 0.01;

int main()
{
int startno;
cout<<"How many jackalopes do you have?";
cin>>startno;

int generations;
cout<<"How many generations do you want to wait?";
cin>>generations;

int target;
cout<<"What is your target population?";
cin>>target;

int total = startno;
int reachyears = 0;
bool reach = (total >= target)?true:false;

int i = 0;
for(; i<generations; ++i)
{
total += (int)(total*BEARRATE);
total -= (int)(total*DEADRATE);
if(!reach && total>= target)
{
reachyears = i+1;
reach = true;
}
}

cout<<"If you start with "<<startno<<" jackalopes and wait "<<generations<<" generations, " \
<<"you'll end up with a total of "<<total<<" of them. "<<endl;

for(; total< target; ++i)
{
total += (int)(total*BEARRATE);
total -= (int)(total*DEADRATE);
if(total>= target)
{
reachyears = i+1;
}
}
cout<<"If you start with "<<startno<<" jackalopes, it will take "<<reachyears<<" generations to reach a population of " \
<<target<<endl;
return 0;
}
相似回答