您的位置:首页 > 编程语言 > C语言/C++

第二周【2015级C++实践项目汇总】

2016-03-10 08:39 267 查看
【项目 - 胖子不想说体重】

  成年男性的标准体重公式为:

  

标准体重(kg)=身高(cm)−100

  超标准体重20%为超重,比标准体重轻20%为超轻。请编写C++程序,输入身高和体重,完成下面的任务:

  (1)计算并输出标准体重。

  (2)计算出标准体重,当超重时,请给出提示。

  (3)计算出标准体重,当超重时给出提示,不超重时也给提示。

  (4)计算出标准体重,输出体重状态(正常/超重/超轻)

(1)
#include <iostream>

using namespace std;

int main()

{

int height,stweight;

cout<<"请输入身高(cm):";

cin>>height;

stweight=height-100;

cout<<"标准体重为:"<<stweight<<endl;

return 0;

}

(2)
#include <iostream>

using namespace std;

int main()

{

int height,weight,stweight;

cout<<"请输入身高(cm)和体重(kg):";

cin>>height>>weight;

stweight=height-100;

if(weight>stweight*1.2)

{

cout<<"你超重啦!"<<endl;

}

return 0;

}

(3)
#include <iostream>

using namespace std;

int main()

{

int height,weight,stweight;

cout<<"请输入身高(cm)和体重(kg):";

cin>>height>>weight;

stweight=height-100;

if(weight>stweight*1.2)

{

cout<<"你超重啦!"<<endl;

}

else

cout<<"你没超重"<<endl;

return 0;

}

(4)
#include <iostream>

using namespace std;

int main()

{

int height,weight,stweight;

cout<<"请输入身高(cm)和体重(kg):";

cin>>height>>weight;

stweight=height-100;

if(weight>stweight*1.2)

{

cout<<"你超重啦!"<<endl;

}

else if(weight<stweight*0.8)

{

cout<<"你超轻啦" <<endl;

}

else

cout<<"你的体重是标准的"<<endl;

return 0;

}

【项目3-小试循环】

  写出实现下面求解任务的程序【提示:m是一个变量,在程序中输入】

  (1)求1到m的平方和

  (2)求1到m间所有奇数的和

  (3)求1到m的倒数和,即

1+12+13+14+...+1m

  (4)求值:

1−12+13−14+...+(−1)(m+1)×1m

  (5)求m!,即

1×2×3×...×m

(1)
#include <iostream>

using namespace std;

int main()

{

int n,m,sum;

cin>>m;

n=1;

sum=0;

while(n<=m)

{

sum+=(n*n);

n++;

}

cout<<"1到"<<m<<"的平方和="<<sum<<endl;

return 0;

}


(2)
#include <iostream>

using namespace std;

int main()

{

int n,m,sum;

cin>>m;

n=1;

sum=0;

while(n<=m)

{

sum+=(n);

n+=2;

}

cout<<"1到"<<m<<"间所有奇数的和="<<sum<<endl;

return 0;

}


(3)
#include <iostream>

using namespace std;

int main()

{

int n,m;

double sum;

cin>>m;

n=1;

sum=0;

while(n<=m)

{

sum+=(1.0/n);

n++;

}

cout<<"1到"<<m<<"的倒数和="<<sum<<endl;

return 0;

}


(4)
#include <iostream>

using namespace std;

int main()

{

int n,m,symbol;

double sum;

cin>>m;

n=1;

sum=0;

symbol=1;

while(n<=m)

{

sum+=(symbol*1.0/n);

n++;

symbol*=-1;

}

cout<<"结果为"<<sum<<endl;

return 0;

}

(5)
#include <iostream>

using namespace std;

int main()

{

int n,m,product;

cin>>m;

n=1;

product=1;

while(n<=m)

{

product*=n;

n++;

}

cout<<m<<"!="<<product<<endl;

return 0;

}


【项目4-用循环求解】

  写出实现下面求解任务的程序:

  (1)用如下公式求π的近似值(计算直到最后一项的绝对值小于10−5)

π4=1−13+15−17+...

  (2)Fibonacci数列在计算科学、经济学等领域中广泛使用,其特点是:第一、二个数是1,从第3个数开始,每个数是其前两个数之和。据此,这个数列为:1 1 2 3 5 8 13 21 34 55 89 ……,请设计程序,输出这个数列,直到这个数字超过10000。

【提示】数列可以表示为:

{f1=f2=1fn=fn−1+fn−2,n>2
(1)

#include<iostream>

using namespace std;

int main()

{

int n,symbol;

double sum,a;

n=1;

sum=0;

symbol=1;

a=1;

while(a>1e-5)

{

sum+=(symbol*a);

n+=2;

a=1.0/n;

symbol*=-1; //变号

}

cout<<"π的近似值为:"<<4*sum<<endl;

return 0;

}



(2)

#include <iostream>

using namespace std;

int main( )

{

int f1,f2,fn,n;

f1=f2=1;

n=2;

cout<<f1<<'\t'<<f2<<'\t';

fn=f1+f2;

while(fn<10000)

{

cout<<fn<<'\t';

n++;

if(n%5==0)

cout<<endl;

f1=f2;

f2=fn;

fn=f1+f2;

}

return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: