1.1(显示3条消息)编写程序显示Welcome to C++、Welcome to Computer Science和Programming is fun
#include <iostream>
using namespace std;
int main()
{
cout << "Welcome to C++" << endl;
cout << "Welcome to Computer Science" << endl;
cout << "Programming is fun" << endl;
return 0;
}
1.2(显示5条消息)编写程序显示5次Welcome to C++。
#include <iostream>
using namespace std;
int main()
{
cout << "Welcome to C++" << endl;
cout << "Welcome to C++" << endl;
cout << "Welcome to C++" << endl;
cout << "Welcome to C++" << endl;
cout << "Welcome to C++" << endl;
return 0;
}
1.3(显示一个图案)编写程序显示下面的图案。
#include <iostream>
using namespace std;
int main()
{
cout << " CCCC + +" << endl;
cout << " C + +" << endl;
cout << "C +++++++ +++++++" << endl;
cout << " C + + " << endl;
cout << " CCCC + +" << endl;
return 0;
}
1.4(打印一个表格)编写程序显示下面的表格。
#include <iostream>
using namespace std;
int main()
{
cout << "a a^2 a^3" << endl;
cout << "1 1 1" << endl;
cout << "2 4 8" << endl;
cout << "3 9 27" << endl;
cout << "4 16 64" << endl;
return 0;
}
1.5(计算表达式)编写程序输出表达式(9.54.5-2.53)/(45.5-3.5)的结果。
#include <iostream>
using namespace std;
int main()
{
cout << (9.5*4.5 - 2.5 * 3) / (45.5 - 3.5) << endl;
return 0;
}
1.6(系列的总和)编写程序输出1+2+3+4+5+6+7+8+9的结果
#include <iostream>
using namespace std;
int main()
{
cout << 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 << endl;
return 0;
}
1.7π=4*(1-1/3+1/5-1/7+1/9-1/11+…)(π的近似值)π可以根据下面的公式计算出来:编写一个程序,分别显示4*(1-1/3+1/5-1/7+1/9-1/11)和4*(1-1/3+1/5-1/7+1/9-1/11+1/13)的结果,在程序中使用1.0代替1.
#include <iostream>
using namespace std;
int main()
{
cout << 4 * (1.0 - 1.0 / 3 + 1.0 / 5 - 1.0 / 7 + 1.0 / 9 - 1.0 / 11) << endl;
cout << 4 * (1.0 - 1.0 / 3 + 1.0 / 5 - 1.0 / 7 + 1.0 / 9 - 1.0 / 11 + 1.0 / 13) << endl;
return 0;
}
1.8(圆的面积和周长)编写一个程序,通过计算下面的公式,输出半径为5.5的圆的面积和周长。
周长=2 * 半径 * π
面积=半径 * 半径 * π
#include <iostream>
using namespace std;
int main()
{
cout << "圆的周长为:" << 2 * 5.5*3.14 << endl;
cout << "圆的面积为:" << 5.5*5.5*3.14 << endl;
return 0;
}
1.9(矩形的面积和周长)编写一个程序,通过下面的公式计算宽度为4.5高度为7.9的矩形的面积和周长,并运行输出。
面积 = 宽度 * 高度
#include <iostream>
using namespace std;
int main()
{
cout << "矩形的周长为:" << 2 * 4.5+7.9*2 << endl;
cout << "矩形的面积为:" << 4.5*7.9 << endl;
return 0;
}
1.10(以英里为单位的平均速度)假设一个赛跑运动员在45分30秒内跑了14千米,编写一个程序,输出该运动员以英里为单位的每小时平均速度。(1英里为1.6千米)
#include <iostream>
using namespace std;
int main()
{
cout << "该运动员以英里为单位的每小时平均速度:" << 14/((45*60+30)/(60*60.0)) << "英里/小时" << endl;
return 0;
}
1.11(人口推算)美国人口普查局项目根据以下假设来进行人口推算:
·每七秒有一人出生·每13秒有一人死亡·每45秒有一个新移民
编写程序,输出5年的人口推算结果。假设目前的人口为312032486,每年按365天计算。
#include <iostream>
using namespace std;
int main()
{
cout << "5年的人口推算结果:" << 312032486 + 5 * 365 * 24 * 60 * 60 / 7 + 5 * 365 * 24 * 60 * 60 / 45 - 5 * 365 * 24 * 60 * 60 / 13 << endl;
return 0;
}
1.12(以千米为单位的平均速度)假设一个赛跑运动员在1小时40分35秒内跑了24英里。编写程序,输出该运动员以千米为单位的每小时平均速度。(1英里为1.6千米)
#include <iostream>
using namespace std;
int main()
{
cout << "该运动员以千米为单位的每小时平均速度:" << (24 * 1.6) / ((60.0 * 60 + 40 * 60 + 35) / (60 * 60)) << endl;
return 0;
}