1、一个小球,从高为H的地方下落,下落弹地之后弹起高度为下落时的一半,比如第一次弹起高度为H/2,如此反复,计算从小球H高度下落到n次弹地往返的总路程。
要求:(1)用递归的方法实现。(2)输入H和n,输出结果。(3)注意程序的健壮性。(4)可以用C/C++实现。
#include<iostream>
#include<cmath>
using namespace std;
double high(int n,double h) { //从下落到第n次弹起的总路程
if (n == 1)
return h+h/2;
else
return high(n - 1, h) + h / pow(2, n) + h / pow(2, n - 1);
}
int main()
{
int h, n;
cin >> h >> n;
cout<<high(n, h);
system("pause");
return 0;
}
2、(易)创建一个CPoint类,代表平面直角坐标系中的点,创建构造函数和运算符重载函数,运算符重载为类重载(非友元重载),可以实现计算两个点之间的距离。可以根据需要加入自己的成员变量或成员函数。
要求:(1)输入两个点的坐标,输出两个点之间的距离。(2)重载运算符为“-”。
#include<iostream>
#include<cmath>
using namespace std;
class cpiont {
private:
double x, y;
public:
cpiont() {}
cpiont(double i, double j) :x(i), y(j) {}
double operator -(cpiont a) {
return sqrt(pow(a.x-x,2)+pow(a.y-y,2));
}
};
int main()
{
int a, b, c, d;
cin >> a >> b >> c >> d;
cpiont x(a, b), y(c, d);
cout << x - y;
system("pause");
return 0;
}
3、创建一个CTriangle类,需要用到第二题中创建的类,即用3点来代表一个三角形,输入三个点的坐标,实现判断此三角形是不是直角三角形,并输出此三角形的周长。可以根据需要加入自己的成员变量或成员函数。
要求:(1)输入三个点的坐标,输出周长并给出是否是直角三角形的信息。(2)注意程序的健壮性。
#include<iostream>
#include<cmath>
#include<algorithm>
using namespace std;
class cpiont {
private:
double x, y;
public:
cpiont() {}
cpiont(double i, double j) :x(i), y(j) {}
double operator -(cpiont a) {
return sqrt(pow(a.x-x,2)+pow(a.y-y,2));
}
};
int main()
{
double a, b;
cin >> a >> b;
cpiont c(a, b);
cin >> a >> b;
cpiont d(a, b);
cin >> a >> b;
cpiont e(a, b);
double s[3];
s[0] = c - d;
s[1] = c - e;
s[2] = d - e;
sort(s, s + 3);
if (pow(s[0], 2) + pow(s[1], 2) == pow(s[2], 2))
cout << "是直角三角形";
else
cout << "不是直角三角形";
system("pause");
return 0;
}
4、请自定义一个Student类,属性包括,Char name[10],int num。编程实现学生信息的输入、查询、浏览,其中浏览分为:升序和降序两种
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
class stu {
private:
string name;
int num;
public:
stu(string i, int j) :name(i), num(j) {}
int shownum() {
return num;
}
string showname() {
return name;
}
};
bool keya(stu a, stu b) {
return (a.shownum() < b.shownum());
}
bool keyb(stu a, stu b) {
return (a.shownum() > b.shownum());
}
int main() {
vector<stu> sd;
string a;
int i,key,n;
while (cin >> a >> i) {
if (a == "0")
break;
stu s(a, i);
sd.push_back(s);
}
cout << "请输入要查询学生的学号,以0 0结束" << endl;
while (cin >> n) {
if (n == 0)
break;
for (int i = 0; i < sd.size(); i++) {
if (sd[i].shownum() == n)
cout << sd[i].showname() << endl;
}
}
cout << "请输入所需要的功能:1.按学号升序浏览;2.按学号降序浏览。" << endl;
while (cin >> key) {
if (key == 1) {
sort(sd.begin(), sd.end(), keya);
for (int i = 0; i < sd.size(); i++)
cout << sd[i].shownum() << " " << sd[i].showname() << endl;
}
else if (key == 2) {
sort(sd.begin(), sd.end(), keyb);
for (int i = 0; i < sd.size(); i++)
cout << sd[i].shownum() << " " << sd[i].showname() << endl;
}
else
break;
}
system("pause");
return 0;
}
网友评论