hello world

【描述】
一只大象口渴了,要喝20升水才能解渴,但现在只有一个深h厘米,底面半径为r厘米的小圆桶(h和r都是整数)。问大象至少要喝多少桶水才会解渴。假设PI为3.14159。
【输入】
输入小圆桶的深h和底面半径r,单位厘米。
【输出】
输出大象至少要喝多少桶水(整数)。
【输入示例】
23 11
【输出示例】
3
【提示】
1升等于1000立方厘米。取整可以使用数学库中的ceil(x)函数。例如,ceil(2.1),向上取整结果为3.0;ceil(-2.1),向上取整结果为-2.0。

(10分)
我的答案:
#include
#include<math.h>
using namespace std;
#define PI 3.14159
#define z 20000
int main(){
int h,r;
double t,n;
cin>>h>>r;
t=PIrr*h;
n=z/t;
cout<<ceil(n);
return 0;
}
题目得分 10
参考答案:
#include
#include
using namespace std;
int main() {
const double PI = 3.14159;
int h, r;
cin >> h >> r; // 输入深和底面半径,单位厘米
double volume = PI * r * r * h / 1000; // 计算体积,单位升,1升等于1000立方厘米
cout << ceil(20 / volume) << endl; // 向上取整
return 0;
}
【描述】
将摄氏温度转换为华氏温度。
转换公式为:

f表示华氏温度,c表示摄氏温度。
【输入】
输入一个摄氏温度。
【输出】
输出对应的华氏温度。
【输入示例】
100
【输出示例】
212
【来源】
《程序设计基础——以C++为例》第1章实验3。(10分)
我的答案:
#include
#include<math.h>
using namespace std;
int main(){
double c,f;
cin>>c;
f=(9.0/5)c+32;
cout<<f;
return 0;
}
题目得分 10
参考答案:
#include
using namespace std;
int main() {
double c;
cin >> c;
double f = (9.0 / 5.0) * c + 32;
cout << f << endl;
return 0;
}
【描述】
在屏幕上显示“Welcome to C++!”并换行。
【输入】
没有输入。
【输出】
Welcome to C++!
【来源】
《程序设计基础——以C++为例》第1章实验1。(10分)
我的答案:
#include
using namespace std;
int main(){
cout<<“Welcome to C++!”<<endl;
return 0;
}
题目得分 10
参考答案:
#include
using namespace std;
int main() {
cout << “Welcome to C++!” << endl;
return 0;
}
【描述】
编写程序,计算并输出两个正整数的和、差、积、商。题目保证输入和输出全部在整型范围内。
【输入】
输入在一行中给出2个正整数a和b。
【输出】
按照格式“A运算符B=结果”顺序输出分行输出两个正整数的和、差、积、商。
【输入示例】
5 3
【输出示例】
5+3=8
5-3=2
5
3=15
5/3=1
【来源】
《程序设计基础——以C++为例》第1章实验4。(10分)
我的答案:
#include
using namespace std;
int main(){
int a,b;
cin>>a>>b;
cout<<a<<"+"<<b<<"="<<a+b<<endl;
cout<<a<<"-"<<b<<"="<<a-b<<endl;
cout<<a<<""<<b<<"="<<ab<<endl;
cout<<a<<"/"<<b<<"="<<a/b<<endl;
return 0;
}
题目得分 10
参考答案:
#include
using namespace std;
int main() {
int a, b;
cin >> a >> b;
cout << a << “+” << b << “=” << (a + b) << endl;
cout << a << “-” << b << “=” << (a - b) << endl;
cout << a << “*” << b << “=” << (a * b) << endl;
cout << a << “/” << b << “=” << (a / b) << endl;
return 0;
}
【描述】
输入一个圆环的内外半径,定义和调用函数:double computeArea(double outside, double inside),计算圆环的面积,inside和outside分别为圆环的内外半径,题目保证外半径大于内半径,函数返回圆环的面积。
假设PI为3.14159。
【输入】
输入圆环的外半径和内半径。
【输出】
输出对应的圆环面积
【输入示例】
3.5 2.5
【输出示例】
18.8495
【来源】
《程序设计基础——以C++为例》第1章实验6强化练习。

(10分)
我的答案:
#include
#include<math.h>
using namespace std;
#define PI 3.14159
double computeArea(double out, double in){
double A;
A=PIoutout-PIinin;
return A;
}
int main(){
double a,in,out;
cin>>out>>in;
a=computeArea(out,in);
cout<<a;
}
题目得分 10
参考答案:
#include
using namespace std;
double computeArea(double outside, double inside) {
const double PI = 3.14159;
double area;
area = PI * (outside * outside - inside * inside); // 求圆环的面积
return area;
}
int main() {
double outsideRadius, insideRadius;
cin >> outsideRadius >> insideRadius;
cout << computeArea(outsideRadius, insideRadius) << endl;
return 0;
}
【描述】
编写程序,根据火车的出发时间和达到时间计算整个旅途所用的时间。
【输入】
在一行中给出两个正整数,其间以空格分隔,分别表示火车的出发时间和到达时间。每个时间的格式为两位小时数(00~23)和两位分钟数(00~59),假设出发和到达在同一天内。
【输出】
在一行中输出该旅途所用的时间,格式为“hh:mm”,其中hh为两位小时数、mm为两位分钟数。
【输入示例】
1201 1530
【输出示例】
03:29
【提示】
#include
设置小时hour的输出宽度和填充字符:
cout << setw(2) << setfill(‘0’) << hour << endl;(10分)
我的答案:
#include
#include
using namespace std;
int main(){
int a,b,c,d;
cin>>a>>b;
if(b%100<a%100){
b=b-100;
c=b/100-a/100;
d=60-(a%100-b%100);
}
else{
c=b/100-a/100;
d=b%100-a%100;
}
cout<<setw(2)<<setfill(‘0’)<<c<<":";
cout<<setw(2)<<setfill(‘0’)<<d;
return 0;
}
题目得分 10
参考答案:
#include
#include
using namespace std;
int main() {
int start, end;
cin >> start >> end;
start = start / 100 * 60 + start % 100;
end = end / 100 * 60 + end % 100;
int hour = (end - start) / 60;
int minute = (end - start) % 60;
cout << setfill(‘0’) << setw(2) << hour << “:” << setw(2) << minute << endl;
return 0;
}
【描述】
计算圆柱体的体积。
假设PI为3.14159。

【输入】
输入圆柱体的半径和高。
【输出】
输出对应的圆柱体体积
【输入示例】
2.5 3.5
【输出示例】
68.7223
【来源】
《程序设计基础——以C++为例》第1章实验6。(10分)
我的答案:
#include
using namespace std;
#define PI 3.14159
int main(){
double r,h;
double v;
cin>>r>>h;
v=PIrr*h;
cout<<v;
}
题目得分 10
参考答案:
#include
using namespace std;
int main() {
const double PI = 3.14159;
double radius, height;
cin >> radius >> height;
double area = PI * radius * radius;
double volume = area * height;
cout << volume << endl;
return 0;
}
【描述】
输入六边形的边长side,求六边形的面积area。
利用下面的公式计算六边形的面积:

【输入】
输入六边形的边长side。
【输出】
输出对应的六边形面积。
【输入示例】
5.5
【输出示例】
78.5918
【提示】
求平方根可以使用数序库中的sqrt(x)函数。
【来源】
《程序设计基础——以C++为例》第1章实验5。

(10分)
我的答案:
#include
#include<math.h>
using namespace std;
int main(){
double a,s;
cin>>s;
a=(3sqrt(3))/2.0s*s;
cout<<a;
}
题目得分 10
参考答案:
#include
#include
using namespace std;
int main() {
double side, area;
cin >> side;
area = 3 * sqrt(3) * side * side / 2;
cout << area << endl;
return 0;
}
【描述】
编写程序,读取投资总额、年利率和年数,然后使用如下公式计算未来投资金额。

【输入】
一行中给出投资总额、年利率和年数,其间以空格分隔。
【输出】
一行中输出未来投资金额,结果保留2位小数。
【输入示例】
1000 3.25 1
【输出示例】
1032.99
【提示】
可以使用数学库中的pow函数来计算a的b次幂。
年利率转换为月利率,年数转换为月数。
结果x保留2位小数:
#include
cout << fixed << setprecision(2) << x << endl;

(10分)
我的答案:
#include
#include
#include<math.h>
using namespace std;
int main(){
double z,rate,t,f;
cin>>z>>rate>>t;
rate=rate/12/100;
t=t12.0;
f=z
pow((1+rate),t);
cout<<fixed<<setprecision(2)<<f<<endl;
return 0;
}
题目得分 10
参考答案:
#include
#include
#include
using namespace std;
int main() {
double investmentAmount, annuallyInterestRate;
int numberOfYears;
cin >> investmentAmount >> annuallyInterestRate >> numberOfYears;
double futureInvestmentValue = investmentAmount * pow((1 + annuallyInterestRate / 1200), numberOfYears * 12);
cout << fixed << setprecision(2) << futureInvestmentValue << endl;
return 0;
}
【描述】
在屏幕上显示下列图案。



**
*
【输入】
没有输入。
【输出】



**
*
【来源】
《程序设计基础——以C++为例》第1章实验2。(10分)
我的答案:
#include
using namespace std;
int main(){
cout<<“"<<endl;
cout<<"
"<<endl;
cout<<"**"<<endl;
cout<<"
”;
}
题目得分 10
参考答案:
#include
using namespace std;
int main() {
cout << “\n";
cout << "
\n";
cout << “**\n”;
cout << "
\n”;
return 0;
}

【描述】
输入一个整数a(a不为-1),求如下表达式的值,结果保留2位小数。

【输入】
输入一个整数a。
【输出】
输出表达式的值,结果保留2位小数。
【输入示例】
2
【输出示例】
2.26
【提示】
求三角函数cos的值可以使用数学库中的cos(x)函数,x为弧度值。求平方根可以使用数学库中的sqrt(x)函数。
【来源】
《程序设计基础——以C++为例》第2章实验15。

(10分)
我的答案:
#include
#include<math.h>
#include
using namespace std;

int main(){
int a;
double b;
cin>>a;
b=(cos(50/180.0*3.14)+sqrt(37.5))/(a+1);
cout<<fixed<<setprecision(2)<<b<<endl;
return 0;
}
题目得分 10
参考答案:
#include
#include
#include
using namespace std;
const double PI = 3.14159;
int main() {
int a;
cin >> a;
cout << fixed << setprecision(2) << (cos(50 * PI / 180) + sqrt(37.5))/(a+1) << endl;
return 0;
}
【描述】
输入一个四位正整数,将该整数每一位上的数字加9,然后除以10取余,作为该位上的新数字,最后将千位和十位上的数字互换,百位和个位上的数字互换,组成变换后的新四位正整数并输出。题目保证转换后的数的千位不会为0。
【输入】
输入一个四位正整数。
【输出】
输出变换后的新四位正整数。
【输入示例】
1257
【输出示例】
4601
【来源】
《程序设计基础——以C++为例》第2章实验4强化练习。

(10分)
我的答案:
#include
using namespace std;

int main(){
int n,a,b,c,d;
cin>>n;
a=n%10;
a=(a+9)%10;
n=n/10;
b=n%10;
b=(b+9)%10;
n=n/10;
c=n%10;
c=(c+9)%10;
n=n/10;
d=n%10;
d=(d+9)%10;
cout<<b<<a<<d<<c<<endl;

return 0;

}
题目得分 10
参考答案:
#include
using namespace std;
int main() {
int n;
cin >> n;
int a = n % 10; // 个位
int b = (n / 10) % 10; // 十位
int c = (n / 10 / 10) % 10; // 百位
int d = (n / 10 / 10 / 10) % 10; // 千位
a = (a + 9) % 10; // 加9并除以10取余
b = (b + 9) % 10;
c = (c + 9) % 10;
d = (d + 9) % 10;
n = b * 1000 + a * 100 + d * 10 + c;
cout << n << endl;
return 0;
}
【描述】
比较两个整数之间的大于、小于、等于、不等于关系。
【输入】
输入在一行中给出2个整数a和b。
【输出】
分行输出整数a和b之间的大于、小于、等于、不等于关系。
【输入示例】
5 3
【输出示例】
true
false
false
true
【来源】
《程序设计基础——以C++为例》第2章实验1。

(10分)
我的答案:
#include
using namespace std;

int main(){
int a,b;
bool c;
cin>>a>>b;
c=a>b;
cout<<boolalpha<<c<<endl;
c=a<b;
cout<<boolalpha<<c<<endl;
c=(a==b);
cout<<boolalpha<<c<<endl;
c=(a!=b);
cout<<boolalpha<<c<<endl;
return 0;
}
题目得分 10
参考答案:
#include
using namespace std;
int main() {
int a, b;
cin >> a >> b;
cout << boolalpha << (a > b) << endl;
cout << boolalpha << (a < b) << endl;
cout << boolalpha << (a == b) << endl;
cout << boolalpha << (a != b) << endl;
return 0;
}
【描述】
求一元二次方程

的根,系数a、b、c为浮点数。
【输入】
输入a、b和c。
【输出】
若无穷解,则输出:Infinitely solution。
若无解,则输出:No solution。
若是一个实根,则输出格式为:x=…,数字、符号之间没有空格,结果保留2位小数。
若两个实根相等,则输出格式为:x1=x2=…,数字、符号之间没有空格,结果保留2位小数。
若是两个实根,则输出格式为:x1=…;x2=…,数字、符号之间没有空格,结果保留2位小数。
若是虚根,则输出:Imaginary root。
【输入示例】
2.1 8.93.5
【输出示例】
x1=-0.44;x2=-3.80
【提示】
可以使用数学库中的sqrt函数和fabs函数。方程的根以及其它中间变量用double类型变量表示。
【来源】
《程序设计基础——以C++为例》第2章实验8强化练习。

(10分)
我的答案:
#include
#include<math.h>
#include
using namespace std;

int main(){
double a,b,c,x,x1,x2;
cin>>a>>b>>c;
if(a0&&b0&&c0){
cout<<“Infinitely solution”;
}
else{
if(b
0&&a0&&c!=0){
cout<<“No solution”;
}
else{
if((bb-4a*c)<0){
cout<<“Imaginary root”;
}
else{
if(a
0){
x=(-c)/b;
cout<<“x=”<<fixed<<setprecision(2)<<x<<endl;
}
else{
if((bb-4ac)==0){
x=(-b+sqrt(b
b-4ac))/(2a);
cout<<“x1=x2=”<<fixed<<setprecision(2)<<x<<endl;
}
else{
x1=(-b+sqrt(b
b-4ac))/(2a);
x2=(-b-sqrt(b
b-4ac))/(2*a);
cout<<“x1=”<<fixed<<setprecision(2)<<x1<<";x2="<<x2<<endl;
}
}

	}
	}
}

return 0;

}
题目得分 10
参考答案:
#include
#include
#include
using namespace std;
const double EPSILON = 1e-6;
int main() {
double a, b, c;
double delta, real_part, imaginary_part;
cin >> a >> b >> c;
if(fabs(a) <= EPSILON && fabs(b) <= EPSILON && fabs© <= EPSILON)
cout << “Infinitely solution” << endl;
else if(fabs(a) <= EPSILON && fabs(b) <= EPSILON)
cout << “No solution” << endl;
else if(fabs(a) <= EPSILON)
cout << “x=” << fixed << setprecision(2) << (-c / b) << endl;
else {
real_part = -b / (2 * a);
delta = b * b - 4 * a * c;
imaginary_part = sqrt(fabs(delta)) / (2 * a);
if(fabs(delta) <= EPSILON)
cout << “x1=x2=” << fixed << setprecision(2) << real_part << endl;
else if(delta > EPSILON) {
cout << fixed << setprecision(2) << “x1=” << (real_part+imaginary_part)
<< “;x2=” << (real_part-imaginary_part);
}
else {
cout << “Imaginary root” << endl;
}
}
return 0;
}
【描述】
输入一个整数,输出分段函数的值。分段函数的数学定义如下:

【输入】
输入一个整数。
【输出】
输出分段函数的值。
【输入示例】
100
【输出示例】
10005
【来源】
《程序设计基础——以C++为例》第2章实验7。

(10分)
我的答案:
#include
#include<math.h>
using namespace std;

int main(){
int x;
cin>>x;
if(x<0){
cout<<x+1<<endl;
}
else if(x>=0&&x<100){
cout<<pow(x,3)<<endl;
}
else{
cout<<pow(x,2)+5<<endl;
}
return 0;
}
题目得分 10
参考答案:
#include
using namespace std;
int main() {
int x, fx;
cin >> x;
if(x < 0)
fx = x + 1;
else if(x < 100)
fx = x * x * x;
else
fx = x * x + 5;
cout << fx << endl;
return 0;
}
【描述】
计算三个整数的和、平均值、最小值和最大值。
要求用条件运算符求最小值和最大值。
【输入】
输入在一行中给出整数a、b、c。
【输出】
分行输出整数a、b、c的和、平均值、最小值和最大值。
【输入示例】
5 3 -1
【输出示例】
7
2.33333
-1
5
【来源】
《程序设计基础——以C++为例》第2章实验2强化练习。

(10分)
我的答案:
#include
using namespace std;

int main(){
int a,b,c;
cin>>a>>b>>c;
cout<<a+b+c<<endl;
cout<<(a+b+c)/3.0<<endl;
cout<<((a<b?a:b)<c?(a<b?a:b):c)<<endl;
cout<<((a>b?a:b)>c?(a>b?a:b):c)<<endl;
return 0;
}
题目得分 10
参考答案:
#include
using namespace std;
int main() {
int a, b, c;
cin >> a >> b >> c;
cout << (a + b + c) << endl;
cout << (a + b + c) / 3.0 << endl;
int min = (a > b) ? b : a;
min = (min > c) ? c : min;
cout << min << endl;
int max = (a > b) ? a : b;
max = (max < c) ? c : max;
cout << max << endl;
return 0;
}
【描述】
输入学生的考试成绩(0~100),将学生的成绩划分等级并输出。学生的成绩可分为5个等级:90~100为A级,80~89为B级,70~79为C级,60~69为D级,0~59为E级。要求用switch语句。
【输入】
输入学生的考试成绩(0~100)。
【输出】
输出等级。
【输入示例】
91
【输出示例】
A
【提示】
假设学生的成绩为score,则score/10的一个值就代表了10种情况,例如score/10为7时,就代表成绩70~79的情况。
【来源】
《程序设计基础——以C++为例》第2章实验9。

(10分)
我的答案:
#include
using namespace std;

int main(){
int n;
cin>>n;
switch(n/10){
case 10:
cout<<“A”<<endl;
break;
case 9:
cout<<“A”<<endl;
break;
case 8:
cout<<“B”<<endl;
break;
case 7:
cout<<“C”<<endl;
break;
case 6:
cout<<“D”<<endl;
break;
default:
cout<<“E”<<endl;
break;
}
return 0;
}
题目得分 10
参考答案:
#include
using namespace std;
int main() {
int score;
char grade;
cin >> score;
score = score / 10;
switch (score) {
case 0:
case 1:
case 2:
case 3:
case 4:
case 5: grade = ‘E’; break;
case 6: grade = ‘D’; break;
case 7: grade = ‘C’; break;
case 8: grade = ‘B’; break;
case 9:
case 10: grade = ‘A’; break;
}
cout << grade << endl;
return 0;
}
【描述】
输入a、b和c,若它们能构成三角形,则输出三角形周长,否则输出“Invalid”。
【输入】
输入a、b和c。
【输出】
输出三角形周长或“Invalid”。
【输入示例1】
1 2 3
【输出示例1】
Invalid
【输入示例2】
3 3 3
【输出示例2】
9

(10分)
我的答案:
#include
using namespace std;

int main(){
double a,b,c;
cin>>a>>b>>c;
if((a+b>c)&&(a+c>b)&&(b+c>a)&&a>0&&b>0&&c>0){
cout<<a+b+c<<endl;
}
else{
cout<<“Invalid”<<endl;
}
return 0;
}
题目得分 10
参考答案:
#include
using namespace std;
int main() {
double a, b, c;
cin >> a >> b >> c;
if((a + b > c) && (a + c > b) && (b + c > a))
cout << (a + b + c) << endl;
else
cout << “Invalid” << endl;
return 0;
}
【描述】
某校学生成绩的绩点计算方法如下(百分制折合为绩点):
(A)90~100分,折合4.0~5.0绩点。
(B)80~89分,折合3.0~3.9绩点。
(C)70~79分,折合2.0~2.9绩点。
(D)60~69分,折合1.0~1.9绩点。
(E)59分及其以下为不及格。
要求用switch语句。
【输入】
输入一个整数表示学生的成绩(0~100)。
【输出】
如果输入的学生成绩不合法,则输出“Invalid”。
如果输入的学生成绩为不及格,则输出“Failed”。
否则,输出成绩对应的绩点,结果保留一位小数。
【输入示例】
74
【输出示例】
2.4

(10分)
我的答案:
#include
#include
using namespace std;

int main(){
int n;
cin>>n;
if(n<0||n>100){
cout<<“Invalid”;
}
else{
switch(n/10){
case 10:
cout<<“5.0”<<endl;
break;
case 9:
cout<<fixed<<setprecision(1)<<4.0+(n%10)/10.0<<endl;
break;
case 8:
cout<<fixed<<setprecision(1)<<3.0+(n%10)/10.0<<endl;
break;
case 7:
cout<<fixed<<setprecision(1)<<2.0+(n%10)/10.0<<endl;
break;
case 6:
cout<<fixed<<setprecision(1)<<1.0+(n%10)/10.0<<endl;
break;
default:
cout<<“Failed”<<endl;
break;
}
}
return 0;
}
题目得分 10
参考答案:
#include
#include
using namespace std;
int main() {
int score;
cin >> score;
if(score < 0 || score > 100)
cout << “Invalid\n”;
else if(score >= 0 && score < 60)
cout << “Failed\n”;
else
cout << fixed << setprecision(1) << (1.0 + (score - 60) * 0.1) << endl;
return 0;
}
【描述】
输入一个整数,检查它是否能同时被2和3整除,是否被2或3整除,是否被2或3整除且只被其一整除。
【输入】
输入一个整数。
【输出】
分行输出该整数是否能同时被2和3整除,是否被2或3整除,是否被2或3整除且只被其一整除。见输出示例。
【输入示例】
18
【输出示例】
18 divisible by 2 and 3? true
18 divisible by 2 or 3? true
18 divisible by 2 or 3, but not both? false
【来源】
《程序设计基础——以C++为例》第2章实验6。

(10分)
我的答案:
#include
using namespace std;

int main(){
int n;
cin>>n;
cout<<“18 divisible by 2 and 3?”<<boolalpha<<bool(n%20&&n%30)<<endl;
cout<<“18 divisible by 2 or 3?”<<boolalpha<<bool(n%20||n%30)<<endl;
cout<<“18 divisible by 2 or 3, but not both?”<<boolalpha<<bool((n%20&&n%3!=0)||(n%2!=0&&n%30))<<endl;
return 0;
}
题目得分 10
参考答案:
#include
using namespace std;
int main() {
int n;
cin >> n;
cout << n << " divisible by 2 and 3? " << boolalpha
<< (n % 2 == 0 && n % 3 == 0) << endl;
cout << n << " divisible by 2 or 3? " << boolalpha
<< (n % 2 == 0 || n % 3 == 0) << endl;
cout << n << " divisible by 2 or 3, but not both? " << boolalpha
<< ((n % 2 == 0 || n % 3 == 0) && !(n % 2 == 0 && n % 3 == 0)) << endl;
return 0;
}

【描述】
计算如下式子:

的值,计算到最后一项的值小于给定的阈值时为止。
【输入】
输入在一行中给出小于1的阈值。
【输出】
在一行中输出满足阈值条件的式子值,结果保留6位小数。
【输入示例】
0.000001
【输出示例】
2.718282
【来源】
《程序设计基础——以C++为例》第2章实验11强化练习。

(10分)
我的答案:
#include
#include
using namespace std;

int main(){
int n=2,t=2;
float x,e=2;
cin>>x;
while(x<=(1.0/t)){
e=e+(1.0/t);
n++;
t=1;
for(int i=1;i<=n;i++){
t=t*i;
}
}
cout<<fixed<<setprecision(6)<<e<<endl;
return 0;
}
题目得分 10
参考答案:
#include
#include
using namespace std;
int main() {
double epsilon;
cin >> epsilon;
int i = 1;
double fact = 1, item = 1, e = 0;
while(item >= epsilon) {
e += item;
fact *= i;
item = 1 / fact;
++i;
}
cout << fixed << setprecision(6) << e << endl;
return 0;
}
【描述】
输入若干个整数,如果输入0,输入即终止。判定读入的整数中有多少个正整数、多少个负整数,并计算这些整数的总和和平均值(0不计算在内)。平均值结果保留2位小数。
【输入】
输入若干个整数,如果输入0,输入即终止。
【输出】
分行输出这些整数中的正整数个数、负整数个数、总和、平均值(0不计算在内)。
若只输入0,则输出:No input。
【输入示例】
-1 -2 -3 -4 -5 6 7 8 9 0
【输出示例】
4
5
15
1.67
【来源】
《程序设计基础——以C++为例》第2章实验12。

(10分)
我的答案:
#include
#include
using namespace std;

int main(){
int z=0,f=0,n;
float r,sum;
while(cin>>n){
if(n0)
break;
if(n<0)
f++;
if(n>0)
z++;
sum=sum+n;
}
r=sum/(f+z);
if(z
0&&f==0)
cout<<“No input”<<endl;
else{
cout<<z<<endl;
cout<<f<<endl;
cout<<fixed<<setprecision(0)<<sum<<endl;
cout<<fixed<<setprecision(2)<<r<<endl;

}
	return 0; 

}
题目得分 10
参考答案:
#include
#include
using namespace std;
int main() {
int countPositive = 0;// 正整数个数
int countNegative = 0;// 负整数个数
int count = 0;// 总的个数
int sum = 0;// 累加变量
int number;
do {
cin >> number;
if (number > 0)
++countPositive;
else if (number < 0)
++countNegative;
sum += number;
++count;
} while (number != 0);
–count;
if (count == 0)
cout << “No input\n”;
else {
cout << countPositive << endl;
cout << countNegative << endl;
cout << sum << endl;
cout << fixed << setprecision(2) << (sum * 1.0 / count) << endl;
}
return 0;
}
【描述】
编写程序,用迭代法求立方根

求立方根的迭代公式为:

当满足如下条件时:

迭代停止。
【输入】
输入一个数。
【输出】
输出该数的立方根。
【输入示例】
27
【输出示例】
3
【来源】
《程序设计基础——以C++为例》第2章实验18。

(10分)
我的答案:
#include
#include
#include
using namespace std;

float dd(float a){
float x1=1;
float x2=(2x1+(a/(x1x1)))/3.0;
if(a==0)
return 0;
else{
while(fabs(x2-x1)>=0.000001){
x1=x2;
x2=(2x1+(a/(x1x1)))/3.0;
}
return x2;
}

}

int main(){
float a;
cin>>a;
cout<<dd(a);

return 0;

}
题目得分 10
参考答案:
#include
#include
using namespace std;
const double EPSILON = 1e-6;
int main() {
double a, x, g;
cin >> a;
if(a == 0)
cout << a << endl;
else {
x = a;
g = (2.0 * x + a / (x * x)) / 3.0;
while(fabs(x - g) >= EPSILON) {
x = g;
g = (2.0 * x + a / (x * x)) / 3.0;
}
cout << g << endl;
}
return 0;
}
【描述】
输入一个正整数,从小到大输出该数所有的质因子。
质因数(质因数)是指能整除给定正整数的质数(素数)。
【输入】
输入一个正整数。
【输出】
分行从小到大输出该数所有的质因子。
【输入示例】
120
【输出示例】
2
2
2
3
5
【来源】
《程序设计基础——以C++为例》第2章实验17。

(10分)
我的答案:
#include
#include
using namespace std;

bool p(int i){
if(i<2)
return false;
else{
for(int j=2;j<i-1;j++){
if(i%j0)
return false;
}
return true;
}
}
int main(){
int n;
cin>>n;
for(int i=2;i<=n;i++){
if(n%i
0&&p(i)){
cout<<i<<endl;
n=n/i;
i=1;
}

	}
	return 0; 

}
题目得分 10
参考答案:
#include
using namespace std;
int main() {
int number, factor = 2;
cin >> number;
while(factor <= number) {
if (number % factor == 0) {
cout << factor << endl;
number = number / factor;
}
else
++factor;
}
return 0;
}
【描述】
编写程序,根据输入的字符以及棱形的边长,输出以该字符为填充字符的棱形。
【输入】
输入在一行中给出字符和边长。
【输出】
输出以该字符为填充字符,相应边长的棱形。
【输入示例】
A 5
【输出示例】
A
AAA
AAAAA
AAAAAAA
AAAAAAAAA
AAAAAAA
AAAAA
AAA
A

【来源】
《程序设计基础——以C++为例》第2章实验19。(10分)
我的答案:
#include
#include
using namespace std;
int main(){
char c;
int n;
cin>>c>>n;
for(int i=1;i<=n;i++){
for(int j=1;j<=n-i;j++){
cout<<" ";
}
for(int l=1;l<=2i-1;l++){
cout<<c;
}
cout<<endl;
}
for(int i=1;i<=n-1;i++){
for(int j=0;j<i;j++){
cout<<" ";
}
for(int l=1;l<=2
(n-1)-1-(i-1)2;l++){
cout<<c;
}
cout<<endl;
}
return 0;
}
题目得分 10
参考答案:
#include
using namespace std;
int main() {
char ch;
int n;
cin >> ch >> n;
for(int i = 1; i <= n; ++i) { /
上半三角形 /
for(int j = 1; j <= n - i; ++j)
cout << " ";
for(int j = 1; j <= 2 * i - 1; ++j)
cout << ch;
cout << endl;
}
for(int i = 1; i <= n - 1; ++i) { /
下半三角形 */
for(int j = 1; j <= i; ++j)
cout << " ";
for(int j = 1; j <= 2 * n - 1 - 2 * i; ++j)
cout << ch;
cout << endl;
}
return 0;
}
【描述】
计算数列1+1/3+1/5+…的前n项之和。
【输入】
输入一个正整数n。
【输出】
输出数列前n项的和。
【输入示例】
10
【输出示例】
2.13326
【来源】
《程序设计基础——以C++为例》第2章实验10。

(10分)
我的答案:
#include
#include
using namespace std;
int main(){
int n,i=1;
double z=0;
cin>>n;

while(n--){
	z=z+(1.0/i);
	i+=2;
} 
cout<<z<<endl;
return 0;

}
题目得分 10
参考答案:
#include
using namespace std;
int main() {
int n;
cin >> n;
double sum = 0.0;
for(int i = 1; i <= n; ++i)
sum += 1.0 / (2 * i - 1);
cout << sum << endl;
return 0;
}
【描述】
计算数列1+(1+2)+(1+2+3)+(1+2+3+4)+…的前n项之和。
【输入】
输入一个正整数n。
【输出】
输出数列前n项的和。
【输入示例】
3
【输出示例】
10
【C++代码】
《程序设计基础——以C++》第2章实验13。

(10分)
我的答案:
#include
#include
using namespace std;
int main(){
int n,sum=0,t;
cin>>n;
for(int i=1;i<=n;i++){
t=0;
for(int j=1;j<=i;j++){
t=t+j;
}
sum+=t;
}
cout<<sum;
return 0;
}
题目得分 10
参考答案:
#include
using namespace std;
int main() {
int n;
cin >> n;
int sum = 0;
int subItem;
for(int i = 1; i <= n; ++i) {
subItem = 0;
for(int j = 1; j <= i; ++j)
subItem += j;
sum += subItem;
}
cout << sum << endl;
return 0;
}
【描述】
在校园里,没有自行车,上课办事会很不方便。但实际上,并非去办任何事情都是骑车快,因为骑车总要找车、开锁、停车、锁车等,这要耽误一些时间。假设找到自行车,开锁并骑上自行车的时间为27秒;停车锁车的时间为23秒;步行每秒行走1.2米,骑车每秒行走3.0米。请判断走不同的距离去办事,骑车快还是走路快。
【输入】
第一个正整数表示有n(n > 0)个测试数据,其后n行是对应的测试数据,每行为一次办事要行走的距离,单位为米。
【输出】
对应每个办事要行走的距离,如果骑车快,输出一行“Bike”;如果走路快,输出一行“Walk”;如果一样快,输出一行“All”。
【输入示例】
4
50
90
120
180
【输出示例】
Walk
Walk
Bike
Bike

(10分)
我的答案:
#include
#include
using namespace std;
int main(){
int n,r,w,b;
cin>>n;
while(n–){
cin>>r;
w=r/1.2;
b=50+r/3.0;
if(w<b)
cout<<“Walk”<<endl;
else if(b<w)
cout<<“Bike”<<endl;
else
cout<<“All”<<endl;
}
return 0;
}
题目得分 10
参考答案:
#include
#include
using namespace std;
const double EPSILON = 1e-6;
int main() {
int n;
cin >> n;
for(int i = 0; i < n; ++i) {
double distance;
cin >> distance;
double walk = distance / 1.2;
double bike = 27 + distance / 3 + 23;
if(fabs(walk - bike) < EPSILON)
cout << “All” << endl;
else if(walk < bike)
cout << “Walk” << endl;
else
cout << “Bike” << endl;
}
return 0;
}
【描述】
某工地需要搬运砖块,已知男人一人搬3块,女人一人搬2块,小孩两人搬1块。用45人正好搬45块砖,问有多少种搬法?
【输入】
没有输入。
【输出】
输出搬砖的男人、女人和小孩数。
【输出示例】
A,B,C
A、B、C分别表示男人、女人、小孩数。
【来源】
《程序设计基础——以C++为例》第2章实验14。

(10分)
我的答案:
#include
#include
using namespace std;
int main(){
int a=3,b=2;
float c=0.5;
for(int i=0;i<=15;i++){
for(int j=0;j<=22;j++){
for(int l=0;l<=90;l++){
if((i+l+j)==45&&(ia+jb+l*c)==45){
cout<<i<<","<<j<<","<<l<<endl;
}
}
}
}
return 0;
}
题目得分 10
参考答案:
#include
using namespace std;
int main() {
int men, women, child;
for(men = 0; men <= 15; ++men) {
for(women = 0; women <= 22; ++women) {
child = 45 - men - women;
if(men * 3 + women * 2 + child * 0.5 == 45)
cout << men << “,” << women << “,” << child << endl;
}
}
return 0;
}
【描述】
给定一个十进制正整数,求其对应的二进制数中1的个数。
【输入】
第一个正整数表示有n(n > 0)个测试数据,其后n行是对应的测试数据,每行为一个正整数。
【输出】
分行输出n个正整数对应的二进制数中1的个数。
【输入示例】
4
2
100
1000
66
【输出示例】
1
3
6
2

(10分)
我的答案:
#include
#include
using namespace std;
int main(){
int t,x,n;
cin>>n;
while(n–){
t=1;
cin>>x;
while(x>1){
if(x%2==1)
t++;
x=x/2;
}
cout<<t<<endl;
}
return 0;
}
题目得分 10
参考答案:
#include
using namespace std;
int main() {
int n;
cin >> n;
for(int i = 0; i < n; ++i) {
int value;
cin >> value;
int count_1 = 0;
int remainder;
while(value) {
remainder = value % 2; // 余数
if(remainder == 1)
++count_1;
value = value / 2; // 商
}
cout << count_1 << endl;
}
return 0;
}

【描述】
输入一个正整数,判断它是否是回文数。要求定义和调用函数:bool isPalindrome(int n),如果n是回文数,该函数返回true,否则返回false。回文数是指正读和反读都相同的数。
【输入】
输入一个正整数。
【输出】
如果该正整数是回文数,输出true,否则输出false。
【输入示例】
616
【输出示例】
true
【来源】
《程序设计基础——以C++为例》第3章实验3。(10分)
我的答案:
bool isPalindrome(int n){
int N=n,m=0;
while(N>0){
m=m10+N%10;
N=N/10;
}
if(m==n){
return true;
}
else{
return false;
}
}
题目得分 10
参考答案:
#include
using namespace std;
bool isPalindrome(int n);
int main() {
int n;
cin >> n;
cout << boolalpha << isPalindrome(n) << endl;
return 0;
}
bool isPalindrome(int n) {
int temp, remainder, result = 0;
temp = n;
while (temp != 0) {
remainder = temp % 10;
result = result * 10 + remainder;
temp = temp / 10;
}
return (n == result);
}
【描述】
定义和调用三个swapValue函数,实现两个整数、两个浮点数和两个字符的交换。
要求用函数重载实现。函数参数可以是引用。
在main中输入交换前和输出交换后的值。
【输入】
输入的第一行是一个整数n(1≤n≤100),表示测试数据的总数。
接下来的n行,每行为测试数据,包含交换前的两个整数,两个浮点数和两个字符。
【输出】
输出包括n行,每行为测试结果,包含交换后的两个整数、两个浮点数和两个字符。
【输入示例】
2
12345 67890 1.23 8.79 A F
213879 7892 12379.2 8081.23 a u
【输出示例】
67890 12345 8.79 1.23 F A
7892 213879 8081.23 12379.2 u a(10分)
我的答案:
/
请在此处编写swapValue函数 /
void swapValue(int &i1,int &i2){
int i;
i=i1;
i1=i2;
i2=i;
}
void swapValue(double &d1,double &d2){
double d;
d=d1;
d1=d2;
d2=d;
}
void swapValue(char &c1,char &c2){
char c;
c=c1;
c1=c2;
c2=c;
}
题目得分 10
参考答案:
#include
using namespace std;
void swapValue(int &i1, int &i2);
void swapValue(double &d1, double &d2);
void swapValue(char &ch1, char &ch2);
int main() {
int n;
cin >> n;
for(int i = 1; i <= n; ++i) {
int i1, i2;
cin >> i1 >> i2;
double d1, d2;
cin >> d1 >> d2;
char ch1, ch2;
cin >> ch1 >> ch2;
swap(i1, i2);
swap(d1, d2);
swap(ch1, ch2);
cout << i1 << " " << i2 << " " << d1 << " " << d2 << " " << ch1 << " " << ch2 << endl;
}
return 0;
}
void swapValue(int &i1, int &i2) {
int temp = i1;
i1 = i2;
i2 = temp;
}
void swapValue(double &d1, double &d2) {
double temp = d1;
d1 = d2;
d2 = temp;
}
void swapValue(char &ch1, char &ch2) {
char temp = ch1;
ch1 = ch2;
ch2 = temp;
}
【描述】
定义和调用如下计算面积的函数:
double computeArea(double radius = 1);
double computeArea(double width, doubleheight);
double computeArea(double side1, doubleside2, double side3);
分别计算圆、矩形和三角形的面积。如果函数返回的面积为0,表示输入数据不合法。π值为3.14159。
要求用函数重载实现。
内联函数isValid用于判断三角形合法性:
inline bool isValid(double side1, doubleside2, double side3);
【输入】
输入有三行:第一行圆半径;第二行矩形宽度和高度,两个实数以空格间隔;第三行三角形边长,三个实数以空格间隔。
【输出】
分行输出对应的圆、矩形和三角形面积。
【输入示例】
5
10 10
1 2 3
【输出示例】
78.5397
100
0
【来源】
《程序设计基础——以C++为例》第2章实验7。(10分)
我的答案:
double computeArea(double radius = 1){
return 3.14159
pow(radius,2);
}
double computeArea(double width, double height){
return widthheight;
}
double computeArea(double side1, double side2, double side3){
double p=(side1+side2+side3)/2;
return sqrt(p
(p-side1)(p-side2)(p-side3));
}
题目得分 10
参考答案:
#include
#include
using namespace std;
double computeArea(double radius = 1);
double computeArea(double width, double height);
double computeArea(double side1, double side2, double side3);
inline bool isValid(double side1, double side2, double side3);
int main() {
double radius;
cin >> radius;
double width, height;
cin >> width >> height;
double side1, side2, side3;
cin >> side1 >> side2 >> side3;
cout << computeArea(radius) << endl;
cout << computeArea(width, height) << endl;
cout << computeArea(side1, side2, side3) << endl;
return 0;
}
double computeArea(double radius) {
const double PI = 3.14159;
double area = 0;
if(radius > 0)
area = PI * radius * radius;
return area;
}
double computeArea(double width, double height) {
double area = 0;
if((width > 0) && (height > 0))
area = width * height;
return area;
}
double computeArea(double side1, double side2, double side3) {
double area = 0;
if(isValid(side1, side2, side3)) {
double p = (side1 + side2 + side3) * 0.5;
area = sqrt(p * (p - side1) * (p - side2) * (p - side3));
}
return area;
}
bool isValid(double side1, double side2, double side3) {
return (side1 + side2 > side3) && (side1 + side3 > side2) && (side2 + side3 > side1);
}
【描述】
输入一个数,定义和调用函数:double squareRoot(double x),求x的平方根,如果x是负数,函数抛出一个异常,否则返回x的平方根。要求使用异常机制来处理错误。
【输入】
输入一个数。
【输出】
如果该数是负数,则抛出异常,输出“Invalid”;否则输出该数的平方根。
【输入示例】
-6
【输出示例】
Invalid
【提示】
输出错误信息使用cout,不要使用cerr。【来源】
《程序设计基础——以C++为例》第3章实验12。(10分)
我的答案:
/* 请在此处编写squareRoot函数 */
double squareRoot(double x){
if(x<0){
throw (int)x;
}
else{
return sqrt(x);
}
}
题目得分 10
参考答案:
#include
#include
using namespace std;
double squareRoot(double x) {
if (x < 0)
throw -1;
return sqrt(x);
}
int main() {
double x;
cin >> x;
try {
cout << squareRoot(x) << endl;
}
catch(int) {
cout << “Invalid” << endl;
}
return 0;
}
【描述】如果一个素数可以写成的形式,其中p是一个正整数,那么该素数就称为梅森素数。
例如,p为2时,为3,3就是梅森素数。
注意:1不是素数。
要求定义和调用函数:int isPrime(int n),如果n是素数,该函数返回1,否则返回0。
【输入】
输入一个正整数n(0≤n≤31)。
【输出】
输出p≤n的梅森素数的个数。
【输入示例】
3
【输出示例】
2
(10分)
我的答案:
bool isPrime(int n){
if(n1||n0)
return 0;
for(int i=2;i<n/2+1;i++){
if(n%i==0)
return 0;
}
return 1;
}
题目得分 10
参考答案:
#include
#include
using namespace std;
bool isPrime(int n);
int main() {
int n;
cin >> n;
int count = 0;
for(int p = 1; p <= n; ++p) {
if(isPrime((int)(pow(2.0, p) - 1)))
++count;
}
cout << count << endl;
return 0;
}
bool isPrime(int n) {
bool flag = true;
if(n <= 1)
flag = false;
else if(n == 2)
flag = true;
else if(n % 2 == 0)
flag = false;
else {
int limit = (int)(sqrt(1.0 * n) + 1);
for(int i = 3; i <= limit; i += 2) {
if(n % i == 0) {
flag = false;
break;
}
}
}
return flag;
}
【描述】
正多边形是一个有n条边的多边形,每条边的长度side相同,每个角的度数也相同。求正多边形面积的公式如下:

要求定义和调用函数:double computeArea(int n,double side),该函数返回正多边形面积。π值为3.14159。
【输入】
输入有两行:第一行一个正整数,表示正多边形的边数;第二行一个实数,表示正多边形的边长。
【输出】
输入对应的正多边形面积。
【输入示例】
5
3
【输出示例】
15.4843
【来源】
《程序设计基础——以C++为例》第2章实验1强化练习。

(10分)
我的答案:
double computeArea(int n, double side){
double a;
a=(npow(side,2))/(4tan(3.14159/n));
return a;
}
题目得分 10
参考答案:
#include
#include
using namespace std;
double computeArea(int n, double side);
int main() {
int n;
cin >> n;
double side;
cin >> side;
cout << computeArea(n, side) << endl;
return 0;
}
double computeArea(int n, double side) {
const double PI = 3.14159;
double area;
area = n * side * side / (4 * tan(PI / n));
return area;
}
【描述】
定义和调用sum函数,返回若干个整数的和,体会函数默认参数的使用。
【输入】
没有输入。
【输出】
100
106
16
36
【提示】
根据sum函数调用时实参的使用和结果的输出,推测sum函数的声明和实现。(10分)
我的答案:
int sum(int a=0,int b=100,int c=0,int d=0){
return a+b+c+d;
}
题目得分 10
参考答案:
#include
using namespace std;
int sum(int num1 = 0, int num2 = 100, int num3 = 0);
int main() {
cout << sum() << endl;
cout << sum(6) << endl;
cout << sum(6, 10) << endl;
cout << sum(6, 10, 20) << endl;
return 0;
}
int sum(int num1, int num2, int num3) {
return num1 + num2 + num3;
}
【描述】
定义和调用函数模板:int compare(Tvalue1, T value2),比较两个值的大小。如果是第一个值小于第二个值,函数返回-1;如果是第一个值等于第二个值,函数返回0;如果是第一个值大于第二个值,函数返回1。
【输入】
输入有三行:第一行两个整数,两个整数以空间间隔;第二行两个实数,两个实数以空格间隔;第三行两个字符,两个字符以空格间隔。
【输出】
分行输出对应的判断结果。
【输入示例】
5 2
3.8 5.4
a a
【输出示例】
1
-1
0
【来源】
《程序设计基础——以C++为例》第3章实验8。(10分)
我的答案:
/* 请在此处编写compare函数 */
template
int compare(T a,T b){
if(a<b)
return -1;
else if(a==b)
return 0;
else if(a>b)
return 1;
}
题目得分 10
参考答案:
#include
using namespace std;
template
int compare(T value1, T value2);
int main() {
int n1, n2;
cin >> n1 >> n2;
double d1, d2;
cin >> d1 >> d2;
char ch1, ch2;
cin >> ch1 >> ch2;
cout << compare(n1, n2) << endl;
cout << compare(d1, d2) << endl;
cout << compare(ch1, ch2) << endl;
return 0;
}
template
int compare(T value1, T value2) {
if(value1 < value2 )
return -1;
if(value1 > value2)
return 1;
return 0;
}
【描述】
定义和调用函数:int sum(int (fp)(int), int start, int end)和int f(int x),f函数的功能是求x的平方;sum函数的功能是求f(start)+…+f(end)的值。
输入两个整数num1和num2,要求num1 <= num2;调用函数sum(f, num1, num2),求出f(num1)+…+f(num2)的值。例如,假设num1为1,num2为5,则f(1)为1、f(2)为4、f(3)为9、f(4)为16、f(5)为25、f(1)+…+f(5)的值为55。
【输入】
输入两个整数num1和num2,两个正整数以空格间隔。
【输出】
输出f(num1)+…+f(num2)的值
【输入示例】
1 5
【输出示例】
55
【来源】
《程序设计基础——以C++为例》第3章实验10。(10分)
我的答案:
int f(int x){
return x
x;
}
int sum(int (*fp)(int f), int num1, int num2){
int z=0;
for(int i=num1;i<=num2;i++){
z=z+f(i);
}
return z;
}
题目得分 10
参考答案:
#include
using namespace std;
int f(int);
int sum(int (*fp)(int), int, int);
int main() {
int num1, num2;
cin >> num1 >> num2;
int result = sum(f, num1, num2);
cout << result << endl;
return 0;
}
int f(int x) {
return x * x;
}
int sum(int (*fp)(int), int start, int end) {
int result = 0;
for(int i = start; i <= end; ++i)
result += fp(i);
return result;
}
【描述】
按如下公式:

求出数列的前n项(n≤20)并输出,要求每行输出5项。定义和调用函数:long sequence(int n),计算数列第n项的值。
【输入】
输入一个正整数n。
【输出】
输出数列的前n项。每行输出5项。每项宽度为6。
【输入示例】
20
【输出示例】

 0     1     2     3     6
11    20    37    68   125

230 423 778 1431 2632
4841 8904 16377 30122 55403

【来源】
《程序设计基础——以C++为例》第3章实验9。

(10分)
我的答案:
long sequence(int n){
if(n0||n1||n==2)
return n;
else
return sequence(n-1)+sequence(n-2)+sequence(n-3);
}
题目得分 10
参考答案:
#include
#include
using namespace std;
long sequence(int n);
int main() {
int count = 0;
int n;
cin >> n;
for(int i = 0; i < n; ++i) {
cout << setw(6) << sequence(i);
++count;
if(count % 5 == 0)
cout << endl;
}
return 0;
}
long sequence(int n) {
if(n == 0)
return 0;
else if(n == 1)
return 1;
else if(n == 2)
return 2;
else
return sequence(n - 1) + sequence(n - 2) + sequence( n - 3);
}

【描述】
输入一个正整数n(2≤n≤10)和n×n矩阵a中的元素,如果a是上三角矩阵,输出“Yes”,否则输出“No”。
【输入】
第一行为正整数n,表示矩阵大小。
接着n行,每一行n个整数,整数以空格间隔。
【输出】
输出“Yes”或“No”。
【输入示例】
3
3 4 5
1 2 3
1 3 4
【输出示例】
No
【提示】
用二维数组表示n×n矩阵时(i表示行下标,j表示列下标),则:
主对角线i==j,副对角线i + j == n – 1。
上三角矩阵i<=j。
下三角矩阵i>=j。
【来源】
《程序设计基础——以C++为例》第4章实验5。

(10分)
我的答案:
#include
using namespace std;

int main(){
int n;
int flag=1;
cin>>n;
int jz[n][n];
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
cin>>jz[i][j];
if(i>j){
if(jz[i][j]!=0){
flag=0;
}
}
}
}
if(flag==1)
cout<<“Yes”<<endl;
else
cout<<“No”<<endl;

return 0;

}
题目得分 10
参考答案:
#include
using namespace std;
int main() {
const int ARRAY_SIZE = 10;
int a[ARRAY_SIZE][ARRAY_SIZE];
int n;
cin >> n;
for(int i = 0; i < n; ++i)
for(int j = 0; j < n; ++j)
cin >> a[i][j];
bool flag = true;
for(int i = 0; i < n; ++i) {
for(int j = 0; j < i; ++j) {
if(a[i][j] != 0) {
flag = false;
break;
}
}
}
if(flag)
cout << “Yes” << endl;
else
cout << “No” << endl;
return 0;
}
【描述】
输入n(1≤n≤100)个正整数(无序的),找出第k(k≤n)大的数。注意,第k大的数意味着从大到小排在第k位置的数。
【输入】
n
k
a1 a2 a3 a4…an
【输出】
b
【输入示例】
5
2
32 3 12 5 89
【输出示例】
32

(10分)
我的答案:
#include
#include
using namespace std;

int comp(const void *p,const void *q){
return (reinterpret_cast<const int>§-reinterpret_cast<const int>(q));
}

int main(){
int n,k;
cin>>n;
int a[n];
cin>>k;
for(int i=0;i<n;i++){
cin>>a[i];
}
qsort(a,n,sizeof(int),comp);
cout<<a[n-k]<<endl;

return 0;

}
题目得分 10
参考答案:
#include
#include
using namespace std;
int comp(const void *p, const void *q) {
return *((int *)q) - *((int *)p);
}
int main() {
int n;
cin >> n;
int k;
cin >> k;
int intArray[100];
for(int i = 0; i < n; ++i) {
cin >> intArray[i];
}
qsort(intArray, n, sizeof(int), comp);
cout << intArray[k - 1] << endl;
return 0;
}
【描述】
给定一组整数,要求利用数组把这组数保存起来,实现对数组中的数循环移动。假定共有n个整数,则要使前面各数顺序向后移m个位置,并使最后m个数变为最前面的m个数。
要求只用一个数组的方式实现,一定要保证在输出结果时,输出的顺序和数组中数的顺序是一致的。
【输入】
第一行包含一个正整数n和一个正整数m,n和m以空格间隔。
第二行包含n个正整数,整数以空格间隔。
【输出】
依次输出经过循环移动后数组中元素值,元素值以空格间隔。
【输入示例】
11 4
15 3 76 67 84 87 13 67 45 34 45
【输出示例】
67 45 34 45 15 3 76 67 84 87 13

(10分)
我的答案:
#include
using namespace std;

int main(){
int n,m;
cin>>n;
cin>>m;
int a[n+m];
for(int i=0;i<n;i++){
cin>>a[i];
}
for(int i=n-1;i>=0;i–){
a[i+m]=a[i];
}
for(int i=m-1;i>=0;i–){
a[i]=a[n+i];
}
for(int i=0;i<n;i++){
if(i==n-1)
cout<<a[i]<<endl;
else
cout<<a[i]<<" ";
}
return 0;
}
题目得分 10
参考答案:
#include
using namespace std;
int main() {
int n, m;
cin >> n >> m;
int array[n];
for(int i = 0; i < n; ++i)
cin >> array[i];
for(int i = 1; i <= m; ++i) {
int temp = array[n - 1];
for(int j = 1; j < n; ++j) {
array[n - j] = array[n - j - 1];
}
array[0] = temp;
}
for(int i = 0; i < n; ++i)
cout << array[i] << " ";
cout << endl;
return 0;
}
【描述】
编写程序,输入10个数,计算这10个数的均值和标准偏差。用下面的公式计算均值mean和标准偏差deviation:

【输入】
在第一行中给出10个数,数间以空格间隔。
【输出】
第一行为均值。
第二行为标准偏差。
【输入示例】
583 566 58 632 244 485 600 432 88 562
【输出示例】
425
216.476
【来源】
《程序设计基础——以C++为例》第4章实验4。

(10分)
我的答案:
double mean(const double x[], int arraySize){
double t=0;
for(int i=0;i<arraySize;i++){
t+=x[i];
}
return t/arraySize;
}
double deviation(const double x[], int arraySize){
double t=0;
double m=mean(x, arraySize);
for(int i=0;i<arraySize;i++){
t+=pow((x[i]-m),2);
}
return sqrt(t/(arraySize-1));
}
题目得分 10
参考答案:
#include
#include
using namespace std;
double mean(const double x[], int arraySize);
double deviation(const double x[], int arraySize);
int main() {
const int ARRAY_SIZE = 10;
double array[ARRAY_SIZE];
for(int i = 0; i < ARRAY_SIZE; ++i)
cin >> array[i];
cout << mean(array, ARRAY_SIZE) << endl;
cout << deviation(array, ARRAY_SIZE) << endl;
return 0;
}
double mean(const double x[], int arraySize) {
double sum = 0;
for(int i = 0; i < arraySize; ++i)
sum += x[i];
return sum / arraySize;
}
double deviation(const double x[], int arraySize) {
double mean1 = mean(x, arraySize);
double squareSum = 0;
for(int i = 0; i < arraySize; ++i)
squareSum += pow(x[i] - mean1, 2);
return sqrt(squareSum / (arraySize - 1));
}
【描述】
输入10个整数,存放在一维数组中,找出值最大和最小的元素,输出最大值、最小值及它们所在的元素下标。
【输入】
在一行中输入10个整数,整数以空格间隔。
【输出】
第一行输出最大值及其所在的元素下标,最大值和下标以空格间隔。
第二行输出最小值及其所在的元素下标,最小值和下标以空格间隔。
【输入示例】
1 3 57 9 6 0 8 2 4
【输出示例】
9 4
0 6
【来源】
《程序设计基础——以C++为例》第4章实验1。

(10分)
我的答案:
#include
using namespace std;

int main(){
int min,max,minI=0,maxI=0;
int a[10];
for(int i=0;i<10;i++){
cin>>a[i];
if(i==0){
min=a[i];
max=a[i];
}
else{
if(a[i]<min){
min=a[i];
minI=i;
}
if(a[i]>max){
max=a[i];
maxI=i;
}
}
}
cout<<max<<" “<<maxI<<endl;
cout<<min<<” "<<minI<<endl;
return 0;
}
题目得分 10
参考答案:
#include
using namespace std;
int main() {
const int ARRAY_SIZE = 10;
int a[ARRAY_SIZE];
for(int i = 0; i < ARRAY_SIZE; ++i)
cin >> a[i];
int max = a[0];
int min = a[0];
int maxIndex = 0;
int minIndex = 0;
for(int i = 1; i < ARRAY_SIZE; ++i) {
if(a[i] > max) {
max = a[i];
maxIndex = i;
}
if(a[i] < min) {
min = a[i];
minIndex = i;
}
}
cout << max << " " << maxIndex << endl;
cout << min << " " << minIndex << endl;
return 0;
}
【描述】
编写程序,以左下三角的形式输出前n行杨辉三角形。
【输入】
输入在一行中给出n(1≤n≤10)。
【输出】
以左下三角的格式输出前n行杨辉三角形。每个数字占固定4位。
【输入示例】
5
【输出示例】
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
【来源】
《程序设计基础——以C++为例》第4章实验7。

(10分)
我的答案:
#include
#include
using namespace std;
int main(){
int n,t=0;
cin>>n;
int a[n][n];
for(int i=0;i<n;i++){
for(int j=0;j<i+1;j++){
if(i0||j0||i==j)
a[i][j]=1;
else{
a[i][j]=a[i-1][j-1]+a[i-1][j];
}
cout<<setw(4)<<a[i][j];
}
cout<<endl;
}
return 0;
}
题目得分 10
参考答案:
#include
#include
using namespace std;
int main() {
const int ARRAY_SIZE = 10;
int a[ARRAY_SIZE][ARRAY_SIZE];
int n;
cin >> n;
for(int i = 0; i < n; ++i) {
for(int j = 0; j < n; ++j) {
if(j == 0 || i == j)
a[i][j] = 1;
if(j > 0 && i > j)
a[i][j] = a[i - 1][j - 1] + a[i - 1][j];
if(i < j)
a[i][j] = 0;
}
}
for(int i = 0; i < n; ++i) {
for(int j = 0; j < n; ++j) {
if(i >= j) {
cout << setw(4) << a[i][j];
}
}
cout << endl;
}
return 0;
}
【描述】
编写程序,定义和调用函数:void mulMatrix(int **a, int **b, int **c, int cRow, int cCol, intaCol),实现矩阵a和矩阵b相乘,结果存放在矩阵c中。
【输入】
第一行输入矩阵a的行数和列数,以空格间隔。
第二行输入矩阵b的列数(矩阵b的函数等于矩阵a的列数)。
接着按矩阵a的行列数输入矩阵a的值。
然后按矩阵b的行列数输入矩阵b的值。
【输出】
按矩阵c的行列数输出矩阵a和矩阵b相乘的结果矩阵c。元素间以空格分隔,但行末不得有多余空格。
【输入示例】
3 2
3
5 7
8 3
7 4
12 3 6
4 2 7
【输出示例】
88 29 79
108 30 69
100 29 70
【来源】
《程序设计基础——以C++为例》第4章实验13。

(10分)
我的答案:
void mulMatrix(int **a, int **b, int **c, int cRow, int cCol, int aCol){
for(int i=0;i<cRow;i++){
for(int j=0;j<cCol;j++){
c[i][j]=0;
}
}
for(int i=0;i<cRow;i++){
for(int j=0;j<cCol;j++){
for(int l=0;l<aCol;l++){
c[i][j]+=(a[i][l]*b[l][j]);
}
}
}
}
void printMatrix(int **array, int arrayRow, int arrayCol){
for(int i=0;i<arrayRow;i++){
for(int j=0;j<arrayCol;j++){
if(j==arrayCol-1)
cout<<array[i][j]<<endl;
else
cout<<array[i][j]<<" ";
}
}
}
题目得分 10
参考答案:
#include
#include
using namespace std;
void mulMatrix(int **a, int **b, int **c, int cRow, int cCol, int aCol);
void printMatrix(int **array, int arrayRow, int arrayCol);
int main() {
int aRow, aCol;
int bRow, bCol;
int cRow, cCol;
cin >> aRow >> aCol;
bRow = aCol; // 矩阵b的行数等于矩阵a的列数
cin >> bCol;
cRow = aRow; // 矩阵c的行数等于矩阵a的行数
cCol = bCol; // 矩阵c的列数等于矩阵b的列数
int **a = new(nothrow) int *[aRow];
for(int i = 0; i < aRow; ++i)
a[i] = new(nothrow) int[aCol];
int **b = new(nothrow) int *[bRow];
for(int i = 0; i < bRow; ++i)
b[i] = new(nothrow) int[bCol];
int **c = new(nothrow) int *[cRow];
for(int i = 0; i < cRow; ++i)
c[i] = new(nothrow) int[cCol];
for(int i = 0; i < aRow; ++i)
for(int j = 0; j < aCol; ++j)
cin >> a[i][j];
for(int i = 0; i < bRow; ++i)
for(int j = 0; j < bCol; ++j)
cin >> b[i][j];
mulMatrix(a, b, c, cRow, cCol, aCol);
printMatrix(c, cRow, cCol);
return 0;
}
void mulMatrix(int **a, int **b, int **c, int cRow, int cCol, int aCol) {
for(int i = 0; i < cRow; ++i) {
for(int j = 0; j < cCol; ++j) {
c[i][j] = 0;
for(int k = 0; k < aCol; ++k)
c[i][j] += a[i][k] * b[k][j];
}
}
}
void printMatrix(int **array, int arrayRow, int arrayCol) {
for(int i = 0; i < arrayRow; ++i) {
for(int j = 0; j < arrayCol - 1; ++j) {
cout << array[i][j] << " ";
}
cout << array[i][arrayCol - 1] << endl;
}
}
【描述】
输入一指定金额(以元为单位),然后输出支付该金额的各种面额的人民币数量,显示100元,50元,20元,10元,5元,1元各多少张,要求尽量使用大面额的。
【输入】
输入一个小于1000的正整数。
【输出】
分行输出,每行显示一个整数,从上到下分别表示100元,50元,20元,10元,5元,1元人民币的张数。
【输入示例】
735
【输出示例】
7
0
1
1
1
0
【来源】
《程序设计基础——以C++为例》第4章实验2。

(10分)
我的答案:
#include
using namespace std;
int main(){
int n,a[6];
cin>>n;
a[0]=n/100;
if(a[0]!=0)
n=n%(a[0]*100);
a[1]=n/50;
if(a[1]!=0)
n=n%(a[1]*50);
a[2]=n/20;
if(a[2]!=0)
n=n%(a[2]*20);
a[3]=n/10;
if(a[3]!=0)
n=n%(a[3]*10);
a[4]=n/5;
if(a[4]!=0)
n=n%(a[4]*5);
a[5]=n;
for(int i=0;i<6;i++){
cout<<a[i]<<endl;
}

return 0;

}
题目得分 10
参考答案:
#include
using namespace std;
int main() {
const int ARRAY_SIZE = 6; // 货币面额数量
int a[ARRAY_SIZE] = {100, 50, 20, 10, 5, 1};// 货币面额(以元为单位)
int counts[ARRAY_SIZE] = {0}; // 计数器,各货币面额张数
double money; // 输入的金额
cin >> money;
for(int i = 0; i < ARRAY_SIZE; ++i) {
while(money >= a[i]) {
money -= a[i];
++counts[i];
}
}
for(int i = 0; i < ARRAY_SIZE; ++i)
cout << counts[i] << endl;
return 0;
}
【描述】
编写程序,创建一个m×n(2≤m、n≤10)的矩阵,输入矩阵的值,找出该矩阵的鞍点,鞍点是指本行最大、本列最小的元素,可能没有鞍点,也可能有多个鞍点。简单起见,只考虑一个鞍点和没有鞍点的情况。测试数据保证矩阵中任意两个数互不相等。
【输入】
第一行输入矩阵的行列数m和n,以空格间隔。
接着按矩阵的行列数输入矩阵的值。
【输出】
按“行 列 鞍点值”的格式输出鞍点,若不存在鞍点,则输出“No saddle point”。
【输入示例】
3 4
41 89 31 39
96 94 15 20
40 96 86 11
【输出示例】
0 1 89
【来源】
《程序设计基础——以C++为例》第4章实验6。

(10分)
我的答案:
#include
using namespace std;
int main(){
int m,n,flag=0;
cin>>m>>n;
int a[m][n];
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
cin>>a[i][j];
}
}
int q=0,p=0,rmax,cmin,x,y;
for(q=0;q<m;q++){
rmax=a[q][0];
x=0,y=q;
for(p=0;p<n;p++){
if(a[q][p]>rmax){
rmax=a[q][p];
x=p;
y=q;
}
}
cmin=rmax;
for(int i=0;i<m;i++){
if(a[i][x]<cmin){
cmin=a[i][x];
}
}
if(cminrmax){
cout<<y<<" “<<x<<” "<<rmax<<endl;
flag=1;
break;
}
}
if(flag
0){
cout<<“No saddle point”<<endl;
}
return 0;
}
题目得分 10
参考答案:
#include
using namespace std;
int main() {
const int ROW = 10;
const int COLUMN = 10;
int a[ROW][COLUMN];
int m, n;
cin >> m >> n;
for(int i = 0; i < m; ++i) {
for(int j = 0; j < n; ++j) {
cin >> a[i][j];
}
}
bool flag = false;
int max, min, max_row, max_column;
for(int i = 0; i < m; ++i) {
max_row = i;
max = a[i][0];
max_column = 0;
for(int j = 0; j < n; ++j) {
if(a[i][j] > max) {
max = a[i][j];
max_column = j;
}
}
min = a[0][max_column];
for(int j = 0; j < m; ++j) {
if(a[j][max_column] < min)
min = a[j][max_column] ;
}
if(max == min) {
cout << max_row << " " << max_column << " " << max << endl;
flag = true;
}
}
if(!flag)
cout << “No saddle point\n”;
return 0;
}
【描述】
中位数定义:一组数据按从小到大的顺序依次排列,处在中间位置的一个数或最中间两个数据的平均值(如果这组数的个数为奇数,则中位数为位于中间位置的那个数;如果这组数的个数为偶数,则中位数是位于中间位置的两个数的平均值)。
给出一组无序整数,求出中位数,如果求最中间两个数的平均数,向下取整即可(不需要使用浮点数)。
【输入】
包含多组测试数据,每一组测试数据的第一行为n,代表该组测试数据包含的数据个数,1≤n≤10000。
接着n行为n个数据。n为0时结束输入。
【输出】
输出中位数,每一组测试数据输出一行。
【输入示例】
4
10
30
20
40
3
40
30
50
4
1
2
3
4
0
【输出示例】
25
40
2

(10分)
我的答案:
#include
#include
using namespace std;

int comp(const void*p,const void *q){
return (reinterpret_cast<const int>§-reinterpret_cast<const int>(q));
}

int main(){
int n;
while(cin>>n,n!=0){
int a[n];
for(int i=0;i<n;i++){
cin>>a[i];
}
qsort(a,n,sizeof(int),comp);
if(n%2==1)
cout<<a[n/2]<<endl;
else
cout<<(a[n/2-1]+a[n/2])/2<<endl;
}
return 0;
}
题目得分 10
参考答案:
#include
#include
using namespace std;
int comp(const void *p, const void *q) {
return *(int *)p - *(int *)q;
}
int main() {
while(true) {
int n;
cin >> n;
if(!n)
break;
int array[n];
for(int i = 0; i < n; ++i)
cin >> array[i];
qsort(array, n, sizeof(int), comp);
if(n % 2 != 0)
cout << array[n / 2] << endl;
else
cout << (array[n / 2 - 1] + array[n / 2]) / 2 << endl;
}
return 0;
}

【描述】
本题要求编写程序,计算N个有理数的平均值。要求使用结构表示有理数。
【输入】
输入第1行给出正整数N(<=100);第2行中按照“a1/b1 a2/b2 ……”的格式给出N个分数形式的有理数,其中分子和分母全是整型范围内的整数;如果是负数,则负号一定出现在最前面。
【输出】
在一行中按照“a/b”的格式输出N个有理数的平均值。注意必须是该有理数的最简分数形式,若分母为1,则只输出分子。
【输入示例1】
4
1/2 1/6 3/6 -5/10
【输出示例1】
1/6
【输入示例2】
2
4/3 2/3
【输出示例2】
1

(10分)
我的答案:
int gdd(int x,int y){
return x/gcd(x,y)y;
}
int main(){
int n,g,y,zd=0,zn=0,c;
Rational a[100];
cin>>n;
for(int i=0;i<n;i++){
if(i==0){
cin>>a[i].n;
cin.get();
cin>>a[i].d;
g=a[i].d;
}
else{
cin.get();
cin>>a[i].n;
cin.get();
cin>>a[i].d;
g=gdd(g,a[i].d);
}
}
for(int i=0;i<n;i++){
a[i].n=a[i].n
(g/a[i].d);
a[i].d=g;
zn+=a[i].n;
}
zd=g;
zd=zd*n;
y=gcd(zn,zd);
zn=zn/y;
zd=zd/y;
if(zdzn)
cout<<1<<endl;
else if(zd
1)
cout<<zn<<endl;
else
cout<<zn<<"/"<<zd<<endl;
return 0;
}
题目得分 10
【描述】
本题要求编写程序,比较两个有理数的大小。要求用结构表示有理数。
【输入】
输入在一行中按照“a1/b1 a2/b2”的格式给出两个分数形式的有理数,其中分子和分母全是整型范围内的正整数。
【输出】
在一行中按照“a1/b1 关系符 a2/b2”的格式输出两个有理数的关系。其中“>”表示“大于”,“<”表示“小于”,“=”表示“等于”。
【输入示例1】
1/2 3/4
【输出示例1】
1/2 < 3/4
【输入示例2】
6/8 3/4
【输出示例2】
6/8 = 3/4

(10分)
我的答案:
int main(){
Rational num1,num2;
double a;
char b;
cin>>num1.n;
cin.get();
cin>>num1.d;
cin.get();
cin>>num2.n;
cin.get();
cin>>num2.d;
a=num2.n*(num1.d/(double)num2.d);
if(a<num1.n)
b=’>’;
else if(a==num1.n)
b=’=’;
else
b=’<’;
cout<<num1.n<<’/’<<num1.d<<" “<<b<<” "<<num2.n<<’/’<<num2.d<<endl;
return 0;
}
题目得分 10
【描述】
输入20个数,每个数都在1~10之间,求1~10中的众数(众数就是出现次数最多的数,如果存在一样多次数的众数,则输出值较小的)。
【输入】
输入20个1~10之间的数。
【输出】
输出20个1~10之间的数的众数。
【输入示例】
5 1 5 10 3 5 3 4 8 6 8 3 6 5 10 7 10 2 6 2
【输出示例】
5

(10分)
我的答案:
/* 请在此处编写comp函数 */
int comp(const void *a, const void *b){
int *numa = (int *)a, *numb = (int *)b;
return *numb - *numa;
}
题目得分 10
【描述】
本题要求编写程序,计算两个有理数的和。要求使用结构表示有理数。
【输入】
输入在一行中按照“a1/b1 a2/b2”的格式给出两个分数形式的有理数,其中分子和分母全是整型范围内的正整数。
【输出】
在一行中按照“a/b”的格式输出两个有理数的和。注意必须是该有理数的最简分数形式,若分母为1,则只输出分子。
【输入示例1】
1/3 1/6
【输出示例1】
1/2
【输入示例2】
4/3 2/3
【输出示例2】
2

(10分)
我的答案:
int gdd(int x,int y){
return x/gcd(x,y)y;
}
int main(){
int z,g,y;
Rational a,b;
cin>>a.n;
cin.get();
cin>>a.d;
cin.get();
cin>>b.n;
cin.get();
cin>>b.d;
g=gdd(b.d,a.d);
a.n=a.n
(g/a.d);
b.n=b.n*(g/b.d);
z=(a.n+b.n);
y=gcd(z,g);
z=z/y;
g=g/y;
if(zg)
cout<<1<<endl;
else if(g
1)
cout<<z<<endl;
else
cout<<z<<"/"<<g<<endl;
return 0;
}
题目得分 10
【描述】
判断输入的一个字符串是否为回文串,若是输出“Yes”,否则输出“No”。回文串是指正读和反读都一样的字符串,如level。
【输入】
输入一个字符串。
【输出】
输出“Yes”或“No”。
【输入示例】
abcddcba
【输出示例】
Yes
【来源】
《程序设计基础——以C++为例》第4章实验8。

(10分)
我的答案:
#include
#include
#include
using namespace std;
int main(){
string s;
string S;
cin>>s;
S+=s;
reverse(s.begin(),s.end());
if(s==S)
cout<<“Yes”<<endl;
else
cout<<“No”<<endl;

return 0;

}
题目得分 10
【描述】
输入一个字符串,统计并输出该字符串中26个英文字母(不区分大小写)出现的次数。
【输入】
输入一个字符串。
【输出】
分行输出26个英文字母(不区分大小写)出现的次数。
【输入示例】
I am a student.
【输出示例】
a:2
d:1
e:1
i:1
m:1
n:1
s:1
t:2
u:1
【来源】
《程序设计基础——以C++为例》第4章实验9。

(10分)
我的答案:
#include
#include
using namespace std;
int main(){
string s;
int z,a[100]={0};
getline(cin,s);
for(int i=0;i<s.size();i++){
s[i]=tolower(s[i]);
if(s[i]>=97&&s[i]<=122)
a[s[i]-96]++;
}
for(int i=1;i<=26;i++){
if(a[i]!=0){
cout<<(char)(96+i)<<":"<<a[i]<<endl;
}
}
return 0;
}
题目得分 10
【描述】
通讯录中的一条记录包含下述基本信息:朋友的姓名、出生日期、性别、固定电话号码、移动电话号码。本题要求编写程序,录入N条记录,并且根据要求显示任意某条记录。
【输入】
输入在第1行给出正整数N(<=10);随后N行,每行按照格式“姓名 生日 性别 固话 手机”给出一条记录。其中“姓名”是不超过10个字符、不包含空格的非空字符串;生日按“yyyy/mm/dd”的格式给出年月日;性别用“M”表示“男”、“F”表示“女”;“固话”和“手机”均为不超过15位的连续数字,前面有可能出现“+”。
在通讯录记录输入完成后,最后一行给出正整数K,并且随后给出K个整数,表示要查询的记录编号(从0到N-1顺序编号)。数字间以空格分隔。
【输出】
对每一条要查询的记录编号,在一行中按照“姓名 固话 手机 性别 生日”的格式输出该记录。若要查询的记录不存在,则输出“Not Found”。
【输入示例】
3
Chris 1984/03/10 F +86181779452 13707010007
LaoLao 1967/11/30 F 057187951100 +8618618623333
QiaoLin 1980/01/01 M 84172333 10086
2 1 7
【输出示例】
LaoLao 057187951100 +8618618623333 F 1967/11/30
Not Found(10分)
我的答案:
int main(){
int n,m,b;
cin>>n;
Person a[n];
for(int i=0;i<n;i++){
cin>>a[i].name;
cin>>a[i].birthday;
cin>>a[i].sex;
cin>>a[i].fixed;
cin>>a[i].mobile;
}
cin>>m;
for(int i=0;i<m;i++){
cin>>b;
if(b<0||b>=n)
cout<<“Not Found”;
else{
cout<<a[b].name<<" “<<a[b].fixed<<” “<<a[b].mobile<<” “<<a[b].sex<<” "<<a[b].birthday<<endl;
}
}

return 0;

}
题目得分 10
【描述】
定义和调用函数:bool isAnagram(string str1, string str2),检查两个单词是否是字母易位词,如果是,返回true;否则返回false。两个单词如果包含相同的字母,次序不同,则称为字母易位词(anagram)。例如,“silent”和“listen”是字母易位词。
【输入】
输入有两行,分别对应两个单词。
【输出】
若两个单词是字母易位词,输出true,否则输出false。
【输入示例】
silent
listen
【输出示例】
true
【来源】
《程序设计基础——以C++为例》第4章实验题11。

(10分)
我的答案:
bool isAnagram(string str1,string str2){
char c;
if(str1.size()!=str2.size())
return 0;
else{
for(int i=0;i<str1.size();i++){
for(int j=i+1;j<str1.size();j++){
if(str1[j]<str1[i]){
c=str1[i];
str1[i]=str1[j];
str1[j]=c;
}
}
}
for(int i=0;i<str2.size();i++){
for(int j=i+1;j<str2.size();j++){
if(str2[j]<str2[i]){
c=str2[i];
str2[i]=str2[j];
str2[j]=c;
}
}
}
if(str1==str2){
return 1;
}
}
}
题目得分 10
【描述】
输入一个字符串,求出其中最长的英文单词的长度,并输出。单词之间只能用空格间隔。
【输入】
输入一个字符串。
【输出】
输出字符串中最长的英文单词的长度。
【输入示例】
Nice to meet you
【输出示例】
4
【C++代码】
《程序设计基础——以C++为例》第4章实验题12。

(10分)
我的答案:
#include
#include
using namespace std;
int main(){
int max=0;
string s;
while(cin>>s){
char ch;
if(ch=cin.get()==’\n’)
break;
if(s.size()>max)
max=s.size();
}
cout<<max;
return 0;
}
题目得分 10
【描述】
输入5个字符串,输出其中最大的字符串(按照字典顺序)。
【输入】
输入5个字符串。
【输出】
输出5个字符串中最大的字符串。
【输入示例】
red
blue
yellow
green
purple
【输出示例】
yellow
【来源】
《程序设计基础——以C++为例》第4章实验10。

(10分)
我的答案:
#include
#include
using namespace std;
int main(){
int maxi=0;
string s[5];
string max;
for(int i=0;i<5;i++){
cin>>s[i];
if(i==0)
max=s[i];
if(s[i]>max){
max=s[i];
maxi=i;
}
}
cout<<s[maxi]<<endl;
return 0;
}
题目得分 10

【描述】
声明并实现一个Loan类,表示贷款。Loan类包括:
double类型的私有数据成员loanAmount,表示贷款额。double类型的私有数据成员annualInterestRate,表示贷款年利率。int类型的私有数据成员numberOfYears,表示贷款年限。
有参构造函数,将贷款额、贷款年利率、贷款年限设置为给定的参数。
更改器函数setLoanAmount、setAnnualInterestRate和setNumberOfYears,分别用于修改贷款额、贷款年利率、贷款年限。
访问器函数getLoanAmount、getAnnualInterestRate和getNumberOfYears,分别用于访问贷款额、贷款年利率、贷款年限。
成员函数getMonthlyPayment,返回月还款额。
成员函数getTotalPayment,返回总还款额。
【输入】
输入贷款额、贷款年利率、贷款年限。
【输出】
月还款额和总还款额。
【输入示例】
60000
6.25
15
【输出示例】
514.454
92601.7
【提示】
假设年利率为6.25%,则以6.25作为输入值。
计算月还款额的公式如下:

【来源】
《程序设计基础——以C++为例》第5章实验5。

(10分)
我的答案:
class Loan{
public:
Loan(double newloanAmount,double newannualInterestRate,int newnumberOfYears){
loanAmount=newloanAmount;
annualInterestRate=newannualInterestRate;
numberOfYears=newnumberOfYears;
}
double getMonthlyPayment() const{
double n;
n=(loanAmount*(annualInterestRate/1200))/(1-(1/pow(1+(annualInterestRate/1200),numberOfYears12)));
return n;
}
double getTotalPayment() const{
double m;
m=(loanAmount
(annualInterestRate/1200))/(1-(1/pow(1+(annualInterestRate/1200),numberOfYears*12)))12numberOfYears;
return m;
}

private:
	double loanAmount, annualInterestRate;
	int numberOfYears;

};
题目得分 10
【描述】
声明并实现一个Rectangle类,表示矩形。Rectangle类包括:
double类型的私有数据成员width和height,表示矩形的宽和高。
带默认参数的构造函数,将矩形的宽和高设置为给定的参数。宽和高的默认参数值为1。
更改器函数setWidth和setHeight,分别用于修改矩形的宽和高。
访问器函数getWidth和getHeight,分别用于访问矩形的宽和高。
成员函数computeArea,返回矩形的面积。
成员函数computePerimeter,返回矩形的周长。
【输入】
5 40
10 3.5
【输出】
200 90
35 27
【来源】
《程序设计基础——以C++为例》第5章实验2。

(10分)
我的答案:
class Rectangle{
public:
Rectangle(double w=1,double h=1){
width=w;
height=h;
}
void setWidth(double w){
width=w;
}
void setHeight(double h){
height=h;
}
double computeArea() const{
return width*height;
}
double computePerimeter() const{
return (width+height)*2;
}
private:
double width,height;
};
题目得分 10
【描述】
自定义异常类TriangleException。该类有一个string类型的私有数据成员message,用来存放异常信息;一个无参(默认)构造函数(默认值为“Exception”)和一个有参构造函数,用来设置异常信息;成员函数what,用来显示异常信息。
声明并实现一个Triangle类,表示三角形。Triangle类包括:
double类型的私有数据成员side1、side2、side3,表示三角形三条边。
私有成员函数isValid,判断三条边能否构成三角形。如果能构成三角形,返回true,否则返回false。
私有成员函数check,判断边是否大于0。如果边大于0,返回true,否则返回false。
带默认参数的构造函数,将三角形三条边设置为给定的参数。三条边的默认参数值为1。需要调用check函数检查边是否大于0,如果边小于等于0,则抛出TriangleException异常;以及调用isValid函数判断更改边后能否构成三角形,如果不能构成三角形,则抛出TriangleException异常。
更改器函数setSide1、setSide2和setSide3,分别用于修改三角形三条边。和构造函数一样,每个更改器函数需要调用check函数和isValid函数检查边的有效性。
访问器函数getSide1、getSide2和getSide3,分别用于访问三角形三条边。
成员函数computeArea,返回三角形面积。
成员函数computePerimeter,返回三角形周长。
【输入】
输入三角形的三条边,边长以空格间隔。
【输出】
若三条边有负数,输出“Negative side”。
若三条边能构成三角形,输出三角形面积和周长,以空格间隔。
若不能构成三角形,输出“Don’t make a triangle”。
【来源】
《程序设计基础——以C++为例》第5章实验8。

(10分)
我的答案:
class TriangleException{
public:
TriangleException(){
message=“Exception”;
}
TriangleException(string msg){
message=msg;
}
string what(){
return message;
}
private:
string message;
};

class Triangle{
private:
double side1, side2, side3;
bool isValid(){
if((side1+side2)>side3&&(side1+side3)>side2&&(side3+side2)>side1){
return true;
}
else{
return false;
}
}
bool check(){
if(side1>0&&side2>0&&side3>0){
return true;
}
else{
return false;
}
}
public:
Triangle(double s1=1,double s2=1,double s3=1){
side1=s1;
side2=s2;
side3=s3;
if(!check()){
throw TriangleException(“Negative side”);
}
if(!isValid()){
throw TriangleException(“Don’t make a triangle”);
}
}

	double computePerimeter(){
		return side1+side2+side3;
	}
	double computeArea(){
		double p=(side1+side2+side3)/2;
		return sqrt(p*(p-side1)*(p-side2)*(p-side3));
	}

};
题目得分 10
【描述】
声明并实现一个Point类,表示直角坐标系中的一个点。Point类包括:
double类型的私有数据成员x和y,表示坐标。
无参(默认)构造函数,将坐标设置为原点。
有参构造函数,将坐标设置为给定的参数。
访问器函数getX和getY,分别用于访问点的x坐标和y坐标。
【输入】
0,0 4,5
【输出】
(0,0)
(4,5)
【来源】
《程序设计基础——以C++为例》第5章实验1。

(10分)
我的答案:
class Point{
public:
Point(){
x=0;
y=0;
}
Point(double X,double Y){
x=X;
y=Y;
}
double getX(){
return x;
}
double getY(){
return y;
}
private:
double x,y;

};
题目得分 10
【描述】
自定义异常类NegativeNumberException,表示对负数执行操作时出现的异常,如计算负数的平方根。该类有一个string类型的私有数据成员message,用来存放异常信息;一个无参(默认)构造函数和一个有参构造函数,用来设置异常信息;成员函数what,用来显示异常信息。在main函数中,让用户输入某个数,并调用squareRoot函数,计算该数的平方根。如果输入的是负数,squareRoot函数将抛出NegativeNumberException异常,否则返回该数的平方根。
【输入】
输入一个数。
【输出】
输出该数的平方根或者输出错误信息“Invalid argument!”。
【输入示例】
-8
【输出示例】
Invalid argument!
【来源】
《程序设计基础——以C++为例》第5章实验7。

(10分)
我的答案:
class NegativeNumberException{
private:
string message;
public:
NegativeNumberException(){
message=“Invalid argument!”;
}
NegativeNumberException(string msg){
message=msg;
}
string what(){
return message;
}
};

double squareRoot(double n){
if(n<0)
throw NegativeNumberException();
else
return sqrt(n);
}
题目得分 10
【描述】
声明并实现一个Cylinder类,表示圆柱体。Cylinder类包括:
double类型的私有数据成员radius和height,分别表示圆柱体底半径和高。
带默认参数的构造函数,将圆柱体底半径和高设置为给定的参数。半径和高的默认参数值为1。
访问器函数,分别用于访问圆柱体底半径和高。
成员函数computeVolume,返回圆柱体体积。
成员函数computeSurfaceArea,返回圆柱体表面积。
假设PI为3.14159。
【输入】
输入圆柱体的底半径和高。
【输出】
输出圆柱体的体积和表面积。
【输入示例】
4 8
【输出示例】
402.124
301.593
【来源】
《程序设计基础——以C++为例》第5章实验3。

(10分)
我的答案:
class Cylinder{
public:
Cylinder(double r=1,double h=1){
radius=r;
height=h;
}
double computeVolume(){
return PIradiusradiusheight;
}
double computeSurfaceArea(){
return PI
2radiusradius+2PIradius*height;
}
private:
double radius,height;
};
题目得分 10
【描述】
声明并实现一个Time类,表示时间。Time类包括:
int类型的私有数据成员hour、minute、second,表示时、分、秒。
带默认参数的构造函数,将时、分、秒设置为给定的参数。时、分、秒的默认参数值为0。需要检查时、分、秒的有效性。若超出允许范围,则抛出invalid_argument标准异常。
更改器函数setHour、setMinute和setSecond,分别用于修改时、分、秒。每个更改器函数都要检查对应的时、分、秒的有效性。若超出允许范围,则抛出invalid_argument标准异常。
访问器函数getHour、getMinute和getSecond,分别用于访问时、分、秒。
成员函数setTime,用于同时修改时、分、秒。需要检查时、分、秒的有效性。若超出允许范围,则抛出invalid_argument标准异常。
成员函数printTime24,以24小时制格式输出时间,格式为时:分:秒。
成员函数printTime12,以12小时制格式输出时间,格式为上午或下午时:分:秒。
成员函数tick,将时间递增1秒。要考虑增加1秒后,时间增加到下一分钟、下一小时、下一天的情况。
【输入】
没有输入。
【输出】
00:00:00
AM12:00:00
02:00:00
AM2:00:00
21:34:00
PM9:34:00
12:25:42
PM12:25:42
Invalid argument!
00:00:00
AM12:00:00
【来源】
《程序设计基础——以C++为例》第5章实验6。

(10分)
我的答案:
class Time{
private:
int hour,minute,second;

public:
	Time(int h=0,int m=0,int s=0){
		if(h<0||h>24||m<0||m>60||s<0||s>60)
			throw invalid_argument("Invalid argument!"); 
		
		hour=h;
		minute=m;
		second=s;
	}
	void printTime24(){
		cout<<setfill('0')<<setw(2)<<hour<<":"<<setfill('0')<<setw(2)<<minute<<":"<<setfill('0')<<setw(2)<<second<<endl;
	}
	void printTime12(){
		
		if(hour==12){
			cout<<"PM"<<hour<<":"<<setw(2)<<setfill('0')<<minute<<":"<<setw(2)<<setfill('0')<<second<<endl;
		}
		else if(hour>12){
			cout<<"PM"<<(hour-12)<<":"<<setw(2)<<setfill('0')<<minute<<":"<<setw(2)<<setfill('0')<<second<<endl;
		}
		else if(hour==0){
				cout<<"AM"<<(hour+12)<<":"<<setw(2)<<setfill('0')<<minute<<":"<<setw(2)<<setfill('0')<<second<<endl;
		}
		else{
			cout<<"AM"<<hour<<":"<<setw(2)<<setfill('0')<<minute<<":"<<setw(2)<<setfill('0')<<second<<endl;
		}
	}
	void tick(){
		if(second+1==60){
			second=0;
			minute+=1;
		}
		if(minute==60){
			minute=0;
			hour+=1;
		}
		if(hour==24){
			hour=0;
		}
	}

};
题目得分 10
【描述】
利用上题的Rational类,求如下级数的和:

【输入】
输入一个正整数n(n≤16)。
【输出】
以有理数的形式输出级数前n项的和。
【输入示例】
1
【输出示例】
1/2(10分)
我的答案:
int main(){
int n;
cin>>n;
Rational rational;
for(int i=1;i<=n;i++)
rational=rational.add(Rational(i,i+1));
rational.print();
return 0;
}
题目得分 10
【描述】
声明并实现一个Account类,表示银行账户。Account类包括:
string类型的私有数据成员id,表示账号;string类型的私有数据成员name,表示客户名;double类型的私有数据成员balance,表示账户余额;double类型的私有数据成员annualInterestRate,表示年利率。
有参构造函数,将账号、客户名、账户余额、年利率设置为给定的参数。
更改器函数setId、setName、setBalance和setAnnualInterestRate,分别用于修改账号、客户名、账户余额、年利率。
访问器函数getId、getName、getBalance和getAnnualInterestRate,分别用于访问账号、客户名、账户余额、年利率。
成员函数withdraw,从账户中取款。
成员函数deposit,往账户中存款。
成员函数computeMonthlyInterestRate,返回月利率。
成员函数print,输出账户信息。
【输入】
输入账号、客户名、账户余额和年利率。
【输出】
输出账号、客户名、账户余额和月利率。
【输入示例】
112233 ZhangSan 20000 4.5
【输出示例】
112233
ZhangSan
20500
0.375%
【提示】
假设年利率为4.5%,则以4.5作为输入值。
月利率=年利率/12
【来源】
《程序设计基础——以C++为例》第5章实验4。

(10分)
我的答案:
class Account{
public:
Account(string Id,string Name,double Balance,double AnnualInterestRate){
id=Id;
name=Name;
balance=Balance;
annualInterestRate=AnnualInterestRate;
}
void withdraw(double n){
balance-=n;
}
void deposit(double n){
balance+=n;
}
double computeMonthlyInterestRate(){
return annualInterestRate/12;
}
void print(){
cout<<id<<endl;
cout<<name<<endl;
cout<<balance<<endl;
cout<<computeMonthlyInterestRate()<<"%"<<endl;
}

private:
	string id;
    string name;
    double balance;
    double annualInterestRate;	

};
题目得分 10
【描述】
有理数是由分子和分母组成的a/b形式的数,a是分子,b是分母。例如,1/3、3/4和10/4等都是有理数。有理数不能以0为分母,但可以以0为分子。整数a等价于有理数a/1。
有理数用于包含分数的精确运算。例如,1/3=0.333333……,这个数是不能用浮点数精确表示,为了得到精确的结果,必须使用有理数。
一个有理数可能有很多与其值相等的其他有理数,例如,1/3=2/6=3/9=4/12。为简单起见,用1/3表示所有值等于1/3的有理数。因此,需要对有理数进行优化,使分子和分母之间没有公约数(1除外)。求分子和分母绝对值的最大公约数,然后将分子和分母都除以此最大公约数,得到有理数的优化表示形式。
声明并实现一个Rational类,表示有理数。Rational类包括:
int类型的私有数据成员numerator、denominator,表示分子、分母。
私有成员函数gcd,用于求分子、分母的最大公约数。
带默认参数的构造函数,将分子、分母设置为给定的参数。分子、分母的默认参数值分别为0、1。
访问器函数getNumerator、getDenominator,分别用于访问分子、分母。
成员函数add、subtract、multiply、divide,实现有理数的+、-、×和÷运算。
成员函数equals,判断一个有理数与另一个有理数是否相等。如果相等,返回true,否则返回false。
成员函数doubleValue,将有理数转换为浮点数。
成员函数print,输出一个有理数。若分母为0,则输出“Inf”。
【输入】
4/2 2/3
【输出】
2
2/3
8/3
4/3
4/3
3
a!=b
0.666667
【来源】
《程序设计基础——以C++为例》第5章实验9。

(10分)
我的答案:
题目得分 0

【描述】
请根据main函数中对该类的操作,补充类实现部分完成代码。
该类有个私有静态变量count记录该类的所有对象数,主函数将会在不同语句之后输出对象数,只有正确地实现该类,保证count正确记录该类的对象数,才能输出正确的结果。
【输入】
没有输入。
【输出】
主函数的输出已经写好。

(10分)
我的答案:
Student(int x = 0){
id = x;
count++;
}
~Student(){
count++;
}
void friend PrintCount();
void friend Print(const Student s);
static void InitCount(){
count = 0;
}
//这章是谁出的lj题目
题目得分 10
参考答案:
#include
#include
using namespace std;
class Student {
private:
int id;
static int count;
public:
Student() {
id = 0;
++count;
}
Student(int id) {
this->id = id;
++count;
}
Student(const Student &s) {
id = s.id;
++count;
}
~Student() {
–count;
}
static void InitCount() {
count = 0;
}
friend void PrintCount();
friend void Print(Student s);
};
int Student::count;
void PrintCount() {
cout << “Total " << Student::count << " students” << endl;
}
void Print(Student s) {
cout << "the id is " << s.id << endl;
}
int main() {
Student::InitCount();
Student s;
PrintCount();
Student s1(10);
Student s2(s1);
PrintCount();
Print(s2); // 调用拷贝构造函数,调用结束调用析构函数
PrintCount();
return 0;
}
【描述】
声明并实现一个类模板Pair,表示数对。类模板Pair包括:
私有数据成员first和second,first的类型为类参数T1,second的类型为类参数T2。
有参构造函数,将first和second设置为给定的参数。
访问器函数,分别返回first和second的值。
成员函数print,输出一个数对。、
成员函数swap,交换两个数对的值。
重载关系运算符<、<=、>、>=、==、!=,判断两个数对之间的关系。如果满足关系,返回true,否则返回false。
【输入】
输入二行。第一行和第二行都是一个整数、一个浮点数,表示一个数对,整数和浮点数之间以空格间隔。
【输出】
见【输出示例】
【输入示例】
1 2.5
3 4.5
【输出示例】
p1:[1,2.5]
p2:[3,4.5]
p1:[3,4.5]
p2:[1,2.5]
p1 > p2
【提示】
约定数对p1小于p2,等价于p1.first小于p2.first或者p1.first大于等于p2.first且p1.second小于p2.second。
【来源】
《程序设计基础——以C++为例》第6章实验7。

(10分)
我的答案:
template <class T1,class T2>
class Pair
{
private:
T1 frist;
T2 second;
public:
Pair(T1 k,T2 v):frist(k),second(v) { };
void print(){
cout<<"["<<frist<<","<<second<<"]"<<endl;
}
void swap(Pair& p){
Pair temp=p;
p.frist=this->frist;
p.second=this->second;
this->frist=temp.frist;
this->second=temp.second;
}
friend bool operator<(const Pair& c1, const Pair& c2){
if(c1.second<c2.second)
return true;
return false;
}
friend bool operator==(const Pair& c1, const Pair& c2){
if(c1.fristc2.frist)
if(c1.second
c2.second)
return true;
return false;
}
friend bool operator<=(const Pair& c1, const Pair& c2){
if(c1.second<=c2.second)
return true;
return false;
}
friend bool operator>(const Pair& c1, const Pair& c2){
if(c1.second>c2.second)
return true;
return false;
}
friend bool operator>=(const Pair& c1, const Pair& c2){
if(c1.second>=c2.second)
return true;
return false;
}
friend bool operator!=(const Pair& c1, const Pair& c2){
if(c1c2)
return false;
return true;
}
};
题目得分 10
参考答案:
#include
using namespace std;
template <typename T1, typename T2>
class Pair {
public:
Pair(T1 first, T2 second);
T1 getFirst() const;
T2 getSecond() const;
void print() const;
Pair<T1, T2> swap(Pair<T1, T2> &right);
bool operator
(const Pair<T1, T2> &right);
bool operator!=(const Pair<T1, T2> &right);
bool operator<(const Pair<T1, T2> &right);
bool operator<=(const Pair<T1, T2> &right);
bool operator>(const Pair<T1, T2> &right);
bool operator>=(const Pair<T1, T2> &right);
private:
T1 first;
T2 second;
};
template <typename T1, typename T2>
Pair<T1, T2>::Pair(T1 value1, T2 value2):first(value1), second(value2) { }
template <typename T1, typename T2>
T1 Pair<T1, T2>::getFirst() const {
return first;
}
template <typename T1, typename T2>
T2 Pair<T1, T2>::getSecond() const {
return second;
}
template <typename T1, typename T2>
void Pair<T1, T2>::print() const {
cout << “[” << first << “,” << second << “]” << endl;
}
template <typename T1, typename T2>
Pair<T1, T2> Pair<T1, T2>::swap(Pair<T1, T2> &right) {
T1 temp1 = first;
T2 temp2 = second;
first = right.first;
second = right.second;
right.first = temp1;
right.second = temp2;
return *this;
}
template <typename T1, typename T2>
bool Pair<T1, T2>::operator==(const Pair<T1, T2> &right) {
return first == right.first && second == right.second;
}
template <typename T1, typename T2>
bool Pair<T1, T2>::operator!=(const Pair<T1, T2> &right) {
return !(*this == right);
}
template <typename T1, typename T2>
bool Pair<T1, T2>::operator<(const Pair<T1, T2> &right) {
return (first < right.first) || (!(first < right.first) && (second < right.second));
}
template <typename T1, typename T2>
bool Pair<T1, T2>::operator<=(const Pair<T1, T2> &right) {
return !(*this > right);
}
template <typename T1, typename T2>
bool Pair<T1, T2>::operator>(const Pair<T1, T2> &right) {
return (first > right.first) || (!(first > right.first) && (second > right.second));
}
template <typename T1, typename T2>
bool Pair<T1, T2>::operator>=(const Pair<T1, T2> &right) {
return !(*this < right);
}
int main() {
int x1, x2;
double y1, y2;
cin >> x1 >> y1;
cin >> x2 >> y2;
Pair<int, double> p1(x1, y1), p2(x2, y2);
cout << “p1:”;
p1.print();
cout << “p2:”;
p2.print();
p1.swap(p2);
cout << “p1:”;
p1.print();
cout << “p2:”;
p2.print();
if(p1 < p2)
cout << “p1 < p2” << endl;
else if(p1 == p2)
cout << “p1 == p2” << endl;
else
cout << “p1 > p2” << endl;
return 0;
}
【描述】
声明并实现一个带下标越界检查的TwoArray类,表示二维数组(矩阵),存放整数。TwoArray类包括:
int类型的指针pArray,指向存放数组元素的动态分配内存空间。
int类型的私有数据成员rowSize,存放数组行长度。
int类型的私有数据成员columnSize,存放数组列长度。
有参构造函数,将数组行、列长度设置为给定的参数,并动态分配对应的内存空间。
拷贝构造函数。
析构函数。
重载赋值运算符。
重载函数调用运算符。
两个访问器函数getRowSize和getColumnSize,用于获取数组行、列长度。
函数调用运算符()必须以成员函数形式进行重载。假设matrix是TwoArray类的对象,重载函数调用运算符的意图是使用matrix(i, j)这样的形式来访问二维数组元素,而不是原来的matrix[i][j]的形式。
重载函数调用运算符的语法如下:
函数类型operator()(参数表);
和重载下标运算符一样,在twoArray类中,重载函数调用运算符有两种形式:
int &operator()(int row, int column);
以及:
int operator()(int row, int column)const;
定义普通函数:void printTwoArray(const TwoArray &array)
按行输出二维数组,每个元素宽度为3。
【输入】
输入一个3×4矩阵和一个4×4矩阵。
【输出】
见【输出示例】
【输入示例】
0 1 2 3
2 3 4 5
4 5 6 7
0 1 2 3
3 4 5 6
6 7 8 9
9 10 11 12
【输出示例】
array1:
0 1 2 3
2 3 4 5
4 5 6 7
array2:
0 1 2 3
3 4 5 6
6 7 8 9
9 10 11 12
array3:
0 1 2 3
2 3 4 5
4 5 6 7
Row index out of range!
array1:
0 1 2 3
3 4 5 6
6 7 8 9
9 10 11 12
【来源】
《程序设计基础——以C++为例》第6章实验4。

(10分)
我的答案:
class TwoArray{
private:
int **pArray=NULL,rowSize,columnSize;
public:
TwoArray(int x,int y){
//pArray=x;
rowSize=x;
columnSize=y;
pArray = new int *[rowSize];
for (int i = 0;i < rowSize; ++i)
pArray[i] = new int[columnSize];
}
TwoArray(const TwoArray& p){
columnSize = p.columnSize;
rowSize = p.rowSize;
pArray = new int *[p.rowSize];
for (int i = 0;i < p.rowSize; ++i)
pArray[i] = new int[p.columnSize];

	for(int i = 0; i < rowSize; ++i)
	    for(int j = 0; j < columnSize; ++j)
      		pArray[i][j]=p.pArray[i][j];
}	
int getRowSize(){
	return rowSize;
}
int getColumnSize(){
	return columnSize;
}
int &operator()(int row, int column){
	if(row>=rowSize||column>=columnSize)
		throw out_of_range("Row index out of range!");
	return pArray[row][column]; 
}
friend void printTwoArray(const TwoArray &array){
	for(int i = 0; i < array.rowSize; ++i){
		for(int j = 0; j < array.columnSize; ++j){
  		  cout<<setw(3)<<setfill(' ')<<array.pArray[i][j];
		}
		cout<<endl;
	}
}

};
题目得分 10
参考答案:
#include
#include
#include
using namespace std;
class TwoArray {
public:
TwoArray(int rowSize, int columnSize);
TwoArray(const TwoArray &a);
~TwoArray();
const TwoArray &operator=(const TwoArray &right);
int &operator()(int row, int column);
int operator()(int row, int column) const;
int getRowSize() const;
int getColumnSize() const;
private:
int pArray;
int rowSize;
int columnSize;
};
TwoArray::TwoArray(int rowSize, int columnSize) {
if(rowSize > 0 && columnSize > 0) {
this->rowSize = rowSize;
this->columnSize = columnSize;
}
else
throw invalid_argument(“Invalid argument!”);
pArray = new int[rowSize * columnSize];
}
TwoArray::TwoArray(const TwoArray &a) {
rowSize = a.rowSize;
columnSize = a.columnSize;
pArray = new int[rowSize * columnSize];
for(int i = 0; i < rowSize; ++i)
for(int j = 0; j < columnSize; ++j)
pArray[i * columnSize + j] = a.pArray[i * columnSize + j];
}
TwoArray::~TwoArray() {
delete[] pArray;
}
const TwoArray &TwoArray::operator=(const TwoArray &right) {
if(this != &right) {
if(rowSize != right.rowSize || columnSize != right.columnSize) {
delete [] pArray;
rowSize = right.rowSize;
columnSize = right.columnSize;
pArray = new int[rowSize * columnSize];
}
for(int i = 0; i < rowSize; ++i)
for(int j = 0; j < columnSize; ++j)
pArray[i * columnSize + j] = right.pArray[i * columnSize + j];
}
return this;
}
int &TwoArray::operator()(int row, int column) {
if(row < 0 || row > rowSize - 1)
throw out_of_range(“Row index out of range!”);
if(column < 0 || column > columnSize - 1)
throw out_of_range(“Column index out of range!”);
return pArray[row * columnSize + column];
}
int TwoArray::operator()(int row, int column) const {
if(row < 0 || row > rowSize - 1)
throw out_of_range(“Row index out of range!”);
if(column < 0 || column > columnSize - 1)
throw out_of_range(“Column index out of range!”);
return pArray[row * columnSize + column];
}
int TwoArray::getRowSize() const {
return rowSize;
}
int TwoArray::getColumnSize() const {
return columnSize;
}
void printTwoArray(const TwoArray &array) {
for(int i = 0; i < array.getRowSize(); ++i) {
for(int j = 0; j < array.getColumnSize(); ++j)
cout << setw(3) << array(i, j);
cout << endl;
}
}
int main() {
TwoArray array1(3, 4);
for(int i = 0; i < array1.getRowSize(); ++i)
for(int j = 0; j < array1.getColumnSize(); ++j)
cin >> array1(i, j);
cout << “array1:” << endl;
printTwoArray(array1);
TwoArray array2(4, 4);
for(int i = 0; i < array2.getRowSize(); ++i)
for(int j = 0; j < array2.getColumnSize(); ++j)
cin >> array2(i, j);
cout << “array2:” << endl;
printTwoArray(array2);
TwoArray array3(array1);
cout << “array3:” << endl;
printTwoArray(array3);
try {
array1(3, 4) = 1000;
}
catch(out_of_range &ex) {
cout << ex.what() << endl;
}
array1 = array2;
cout << “array1:” << endl;
printTwoArray(array1);
return 0;
}
【描述】
声明和实现一个向量类MyVector,包括一个点的坐标位置x、y和z,实现其构造函数和三个友元函数,完成两个向量的加法、减法、点乘与叉乘运算。
【输入】
输入一个点的坐标位置x、y和z。
【输出】
见【输出示例】
【输入示例】
3 4 5
【输出示例】
(1,0,0)
(-1,-3,0)
(15,-10,-1)
18(10分)
我的答案:
MyVector::MyVector(int X,int Y,int Z){
x=X;
y=Y;
z=Z;
}
MyVector::MyVector(const MyVector &m){
x=m.x;
y=m.y;
z=m.z;
}
void MyVector::display(){
cout<<"("<<x<<","<<y<<","<<z<<")"<<endl;
}
MyVector add(MyVector &v1, MyVector &v2){
MyVector t;
t.x=v1.x+v2.x;
t.y=v1.y+v2.y;
t.z=v1.z+v2.z;
return t;
}
MyVector sub(MyVector &v1, MyVector &v2){
MyVector t;
t.x=v1.x-v2.x;
t.y=v1.y-v2.y;
t.z=v1.z-v2.z;
return t;
}
int dot(MyVector &v1, MyVector &v2){
return (v1.x
v2.x+v1.y
v2.y+v1.zv2.z);
}
MyVector cross(MyVector &v1, MyVector &v2){
MyVector t;
t.x=v1.y
v2.z-v2.yv1.z;
t.y=v1.z
v2.x-v2.zv1.x;
t.z=v1.x
v2.y-v2.x*v1.y;
return t;
}
题目得分 10
参考答案:
#include
using namespace std;
class MyVector {
public:
MyVector(int = 0, int = 0, int = 0);
MyVector(const MyVector &);
void display();
friend MyVector add(MyVector &v1, MyVector &v2); // 向量假发
friend MyVector sub(MyVector &v1, MyVector &v2); // 向量减法
friend int dot(MyVector &v1, MyVector &v2); // 向量点乘
friend MyVector cross(MyVector &v1, MyVector &v2); // 向量叉乘
private:
int x, y, z;
};
MyVector::MyVector(int x, int y, int z) {
this->x = x;
this->y = y;
this->z = z;
}
MyVector::MyVector(const MyVector &v) {
this->x = v.x;
this->y = v.y;
this->z = v.z;
}
void MyVector::display(){
cout << “(” << x << “,” << y << “,” << z << “)” << endl;
}
//向量加法
MyVector add(MyVector &v1, MyVector &v2) {
MyVector v;
v.x = v1.x + v2.x;
v.y = v1.y + v2.y;
v.z = v1.z + v2.z;
return v;
}
//向量减法
MyVector sub(MyVector &v1, MyVector &v2){
MyVector v;
v.x = v1.x - v2.x;
v.y = v1.y - v2.y;
v.z = v1.z - v2.z;
return v;
}
//向量点乘
int dot(MyVector &v1, MyVector &v2) {
return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z;
}
//向量叉乘
MyVector cross(MyVector &v1, MyVector &v2) {
MyVector v;
v.x = v1.y * v2.z - v1.z * v2.y;
v.y = v1.z * v2.x - v1.x * v2.z;
v.z = v1.x * v2.y - v1.y * v2.x;
return v;
}
int main() {
int x, y, z;
cin >> x >> y >> z;
MyVector a;
MyVector b(1);
MyVector c(2, 3);
MyVector d(x, y, z);
MyVector e©;
MyVector xx = add(a, b);
xx.display();
MyVector yy = sub(b, c);
yy.display();
MyVector zz = cross(c, d);
zz.display();
int w = dot(d, e);
cout << w << endl;
return 0;
}
【描述】
以成员函数的形式重载关系运算符(<、<=、、!=、>、>=),使用半径比较Circle对象大小。
【输入】
输入一行,两个圆半径,圆半径以空格间隔。
【输出】
分行输出圆半径对应的圆对象的比较结果。
【输入示例】
5 6
【输出示例】
true
true
false
true
false
false
【提示】
Circle类可以参阅《程序设计基础——以C++为例》第5章【例5.1】。
【来源】
《程序设计基础——以C++为例》第6章实验3。
(10分)
我的答案:
class Circle{
private:
double r;
public:
Circle(double R){
r=R;
}
bool operator< (const Circle &right);
bool operator<= (const Circle &right);
bool operator
(const Circle &right);
bool operator!= (const Circle &right);
bool operator> (const Circle &right);
bool operator>= (const Circle &right);
};
bool Circle::operator< (const Circle &right){
return bool(r<right.r);
}
bool Circle::operator<= (const Circle &right){
return bool(r<=right.r);
}
bool Circle::operator== (const Circle &right){
return bool(rright.r);
}
bool Circle::operator!= (const Circle &right){
return bool(r!=right.r);
}
bool Circle::operator> (const Circle &right){
return bool(r>right.r);
}
bool Circle::operator>= (const Circle &right){
return bool(r>=right.r);
}
题目得分 10
参考答案:
#include
using namespace std;
const double PI = 3.14159;
class Circle {
public:
Circle(double radius = 1);
void setRadius(double radius);
double getRadius() const;
double computeArea() const;
bool operator<(const Circle & c);
bool operator<=(const Circle & c);
bool operator
(const Circle & c);
bool operator!=(const Circle & c);
bool operator>(const Circle & c);
bool operator>=(const Circle & c);
private:
double radius;
};
Circle::Circle(double radius) {
this->radius = radius;
}
void Circle::setRadius(double radius) {
this->radius = radius;
}
double Circle::getRadius() const {
return radius;
}
double Circle::computeArea() const {
return PI * radius * radius;
}
bool Circle::operator<(const Circle & c) {
return radius < c.radius;
}
bool Circle::operator<=(const Circle & c) {
return radius <= c.radius;
}
bool Circle::operator==(const Circle & c) {
return radius == c.radius;
}
bool Circle::operator!=(const Circle & c) {
return radius != c.radius;
}
bool Circle::operator>(const Circle & c) {
return radius > c.radius;
}
bool Circle::operator>=(const Circle & c) {
return radius >= c.radius;
}
int main() {
double radius1, radius2;
cin >> radius1 >> radius2;
Circle c1(radius1);
Circle c2(radius2);
cout << boolalpha;
cout << (c1 < c2) << endl;
cout << (c1 <= c2) << endl;
cout << (c1 == c2) << endl;
cout << (c1 != c2) << endl;
cout << (c1 > c2) << endl;
cout << (c1 >= c2) << endl;
return 0;
}
【描述】
声明并实现一个Time类,表示时间。Time类包括:
int类型的私有数据成员hour、minute、second,表示时、分、秒。
带默认参数的构造函数,将时、分、秒设置为给定的参数。时、分、秒的默认参数值为0。需要检查时、分、秒的有效性。如果超出允许范围,则抛出invalid_argument(“Invalid argument!”)异常。
访问器函数getHour、getMinute和getSecond,分别用于访问时、分、秒。
成员函数setTime,用于同时修改时、分、秒。需要检查时、分、秒的有效性。如果超出允许范围,则抛出invalid_argument(“Invalid argument!”)异常。
重载自增运算符++,将时间递增1秒。要考虑增加1秒后,时间增加到下一分钟、下一小时、下一天的情况。要考虑前置++和后置++。
重载关系运算符==,判断两个时间是否相等。
使用类型转换函数,将时间转换为秒数。
重载流插入运算符<<,以24小时制格式输出时间,格式为时:分:秒。
【输入】
输入一行,有3个数据,分别表示时、分、秒,以空格间隔。
【输出】
见【输出示例】
【输入示例】
23 59 57
【输出示例】
t1:23:59:57
t2:00:00:00
t1:23:59:58
t2:23:59:57
t1 != t2
86398
【来源】
《程序设计基础——以C++为例》第6章实验5。

(10分)
我的答案:
class Time {
private:
int hour, minute,second;
public:
Time(const Time& p);
Time(int x=0, int y=0, int z = 0) {
hour = x;
minute = y;
second = z;
if (hour > 24 || minute > 60 || second > 60 || hour * minute * second >= 86400 - 1)
throw std::invalid_argument(“Invalid argument!”);
}
operator int()
{
return hour6060+minute*60+second;
}
Time& operator++(){
tick();
return *this;
}
const Time operator++(int){
Time obj(*this);
++(*this);
return obj;
}
void hplus() {
if (hour == 23)
hour = 0;
else
hour++;
}
void mplus() {
if (minute == 59) {
minute = 0;
hplus();
}
minute++;
}
void tick() {
if (second == 59) {
second = 0;
mplus();
}
second++;
}

friend ostream& operator<<(ostream& stream, const Time& x)
{
stream<< setw(2) << setfill(‘0’) << x.hour << “:” << setw(2) << setfill(‘0’)
<< x.minute << “:” << setw(2) << setfill(‘0’) << x.second<<endl;
return stream;
}

friend bool operator==(const Time& c1, const Time& c2){
if(c1.hourc2.hour)
if(c1.minute
c2.minute)
if(c1.second==c2.second)
return true;
return false;
}

};
Time::Time(const Time& p){
hour = p.hour;
minute = p.minute;
second = p.second;
}
题目得分 10
参考答案:
#include
#include
#include
using namespace std;
class Time {
public:
Time(int hour = 0, int minute = 0, int second = 0);
void setTime(int hour, int minute, int second);
int getHour() const;
int getMinute() const;
int getSecond() const;
Time &operator++();
Time operator++(int);
bool operator==(const Time &right);
operator int();
private:
int hour;
int minute;
int second;
friend ostream &operator<<(ostream &out, const Time &t);
};
Time::Time(int hour, int minute, int second) {
setTime(hour, minute, second);
}
void Time::setTime(int hour, int minute, int second) {
if(hour >= 0 && hour < 24)
this->hour = hour;
else
throw invalid_argument(“Invalid argument!”);
if(minute >= 0 && minute < 60)
this->minute = minute;
else
throw invalid_argument(“Invalid argument!”);
if(second >= 0 && second < 60)
this->second = second;
else
throw invalid_argument(“Invalid argument!”);
}
int Time::getHour() const {
return hour;
}
int Time::getMinute() const {
return minute;
}
int Time::getSecond() const {
return second;
}
Time &Time::operator++() {
++second;
if(second >= 60) {
second -= 60;
++minute;
if(minute >= 60) {
minute -= 60;
++hour;
hour %= 24;
}
}
return this;
}
Time Time::operator++(int) {
Time temp = this;
++this;
return temp;
}
bool Time::operator==(const Time &right) {
return ((hour == right.hour) &&
(minute == right.minute) &&
(second == right.second));
}
Time::operator int() {
return (hour * 3600 + minute * 60 + second);
}
ostream &operator<<(ostream &out, const Time &t) {
out << setfill(‘0’) << setw(2) << t.hour << “:”
<< setw(2) << t.minute << “:” << setw(2) << t.second << endl;
return out;
}
int main() {
int hour, minute, second;
cin >> hour >> minute >> second;
try {
Time t1(hour, minute, second);
Time t2;
cout << "t1: ";
cout << t1;
cout << "t2: ";
cout << t2;
t2 = t1++;
cout << "t1: ";
cout << t1;
cout << "t2: ";
cout << t2;
if(t1 == t2)
cout << “t1 == t2” << endl;
else
cout << “t1 != t2” << endl;
int s = t1;
cout << s << endl;
}
catch(invalid_argument &ex) {
cout << ex.what() << endl;
}
return 0;
}
【描述】
声明并实现一个Rational类,表示有理数。有理数的+、-、×和÷运算要求通过重载运算符来实现。Rational类包括:
两个int类型的私有数据成员numerator、denominator,表示分子、分母。
一个私有成员函数gcd,用于求分子、分母的最大公约数。
一个私有成员函数compare,用于判断两个有理数之间的大小关系。一个有理数与另一个有理数相比是大于、等于还是小于,分别返回1、0和-1。
一个带默认参数值的构造函数,将分子、分母设置为给定的参数。分子、分母的默认参数值分别为0、1。
两个访问器函数getNumerator、getDenominator,分别用于获取分子、分母。
重载算术运算符+、-、
、/,实现有理数算术运算。
重载自增运算符++和自减运算符–。要同时考虑前置和后置。
重载复合算术赋值运算符+=、-=、
=、/=。
重载关系运算符<、<=、>、>=、==、!=,判断两个有理数之间的关系。如果满足关系,返回true,否则返回false。
使用类型转换函数,将有理数转换为浮点数。
重载流提取运算符>>和流插入运算符<<。输入输出一个有理数。
【输入】
输入两行。第一行为第一个有理数的分子、分母,以空格间隔。第二行为第二个有理数的分子、分母,以空格间隔。
【输出】
见【输出示例】。
【输入示例】
7 3
1 3
【输出示例】
a=7/3
b=1/3
7/3+1/3=8/3
7/3-1/3=2
7/3
1/3=7/9
7/3/1/3=7
7/3<1/3=false
7/3>1/3=true
7/3==1/3=false
7/3!=1/3=true
c=1/2
2.5+1/2=3
3/2
3/2
3/2
5/2
17/6
【来源】
《程序设计基础——以C++为例》第6章实验6。

(10分)
我的答案:
class Rational {
private:
int numerator, denominator;

int gcd(int x, int y) {
	int r;
	while (1) {
		r = x % y;
		if (r == 0)
			break;
		x = y;
		y = r;
	}
	return y;
}
Rational(const Rational& p);

friend Rational operator+(const Rational& c1, const Rational& c2);
friend Rational operator + (const double & r1, const Rational & r2);
friend Rational operator-(const Rational& c1, const Rational& c2);
friend Rational operator*(const Rational& c1, const Rational& c2);
friend Rational operator/(const Rational& c1, const Rational& c2);

friend Rational operator+=(const Rational& c1, const Rational& c2);
friend Rational operator-=(const Rational& c1, const Rational& c2);
friend Rational operator*=(const Rational& c1, const Rational& c2);
friend Rational operator/=(const Rational& c1, const Rational& c2);

friend bool operator<(const Rational& c1, const Rational& c2);
friend bool operator==(const Rational& c1, const Rational& c2);
friend bool operator<=(const Rational& c1, const Rational& c2);
friend bool operator>(const Rational& c1, const Rational& c2);
friend bool operator>=(const Rational& c1, const Rational& c2);
friend bool operator!=(const Rational& c1, const Rational& c2);

friend ostream& operator<<(ostream& stream, const Rational& x)
{
Rational obj;
obj.numerator = x.numerator;
obj.denominator =x.denominator;
obj.Divisor();
if(obj.denominator==1)
stream<<obj.numerator;
else
stream<<obj.numerator<<"/"<<obj.denominator;
return stream;
}

friend istream& operator>>(istream& stream, Rational& x){
stream>>x.numerator>>x.denominator;
return stream;
}
public:
Rational& operator++(){
numerator+=denominator;
return *this;
}
const Rational operator++(int){
Rational obj(*this);
++(*this);
return obj;
}
Rational(int x = 0, int y = 1) {
if (y < 0) {
x = -x;
y = -y;
}
numerator = x;
denominator = y;
}

void Divisor(){
int n = gcd(numerator, denominator);
numerator /= n;
denominator /= n;
}

};

Rational::Rational(const Rational& p)
{
numerator = p.numerator;
denominator = p.denominator;

}
Rational operator + (const Rational & r1, const Rational & r2)
{
Rational obj;
obj.numerator = r1.numerator * r2.denominator + r2.numerator * r1.denominator;
obj.denominator = r1.denominator * r2.denominator;
return obj;
}
Rational operator + (const double & r1, const Rational & r2)
{
Rational obj;
obj.numerator = (int)(r1+(double)r2.numerator/r2.denominator);
obj.denominator = 1 ;
return obj;
}
Rational operator - (const Rational & r1, const Rational & r2)
{
Rational obj;
obj.numerator = r1.numerator * r2.denominator - r2.numerator * r1.denominator;
obj.denominator = r1.denominator * r2.denominator;
return obj;
}
Rational operator * (const Rational & r1, const Rational & r2)
{
Rational obj;
obj.numerator = r1.numerator * r2.numerator;
obj.denominator = r1.denominator * r2.denominator;
return obj;
}
Rational operator / (const Rational & r1, const Rational & r2)
{
Rational obj;
obj.numerator = r1.numerator * r2.numerator;
obj.denominator = r2.denominator * r1.denominator;
return obj;
}

bool operator<(const Rational& c1, const Rational& c2){
return c1.numerator * c2.denominator <c2.numerator * c1.denominator;
}
bool operator==(const Rational& c1, const Rational& c2){
return c1.numerator * c2.denominator == c2.numerator * c1.denominator;
}
bool operator<=(const Rational& c1, const Rational& c2){
return c1.numerator * c2.denominator <=c2.numerator * c1.denominator;
}
bool operator>(const Rational& c1, const Rational& c2){
return c1.numerator * c2.denominator >c2.numerator * c1.denominator;
}
bool operator>=(const Rational& c1, const Rational& c2){
return c1.numerator * c2.denominator >=c2.numerator * c1.denominator;
}
bool operator!=(const Rational& c1, const Rational& c2){
return c1.numerator * c2.denominator != c2.numerator * c1.denominator;
}

Rational operator+=(const Rational& c1, const Rational& c2){
Rational obj;
obj=c1+c2;
return obj;
}
Rational operator-=(const Rational& c1, const Rational& c2){
Rational obj;
obj.numerator=0;
return obj;
}
Rational operator*=(const Rational& c1, const Rational& c2){
Rational obj;
obj.numerator=c1.numeratorc1.numerator;
obj.denominator=c1.denominator
c1.denominator;
return obj;
}
Rational operator/=(const Rational& c1, const Rational& c2){
Rational obj;
obj.numerator=1;
obj.denominator=1;
return obj;
}
题目得分 10
参考答案:
#include
#include
using namespace std;
class Rational {
public:
Rational(int n = 0, int d = 1);
int getNumerator() const;
int getDenominator() const;
Rational operator+(const Rational &right);
Rational operator-(const Rational &right);
Rational operator*(const Rational &right);
Rational operator/(const Rational &right);
Rational &operator++(); // 前置++
Rational &operator–(); // 前置–
Rational operator++(int);// 后置++
Rational operator–(int);// 后置–
const Rational &operator+=(const Rational &right);
const Rational &operator-=(const Rational &right);
const Rational &operator*=(const Rational &right);
const Rational &operator/=(const Rational &right);
bool operator<(const Rational &right);
bool operator<=(const Rational &right);
bool operator>(const Rational &right);
bool operator>=(const Rational &right);
bool operator==(const Rational &right);
bool operator!=(const Rational &right);
operator double();
private:
int numerator; // 分子
int denominator; // 分母
int gcd(int n, int d); // 求最大公约数
int compare(const Rational &right);
friend istream &operator>>(istream &in, Rational &r);
friend ostream &operator<<(ostream &out, const Rational &r);
};
Rational::Rational (int n, int d) {
int g = gcd(n, d);
if(d < 0) {
n = -n;
d = -d;
}
numerator = n / g;
denominator = d / g;
}
int Rational::getNumerator() const {
return numerator;
}
int Rational::getDenominator() const {
return denominator;
}
Rational Rational::operator+(const Rational &right) {
int n = numerator * right.denominator + denominator * right.numerator;
int d = denominator * right.denominator;
return Rational(n, d);
}
Rational Rational::operator-(const Rational &right) {
int n = numerator * right.denominator - denominator * right.numerator;
int d = denominator * right.denominator;
return Rational(n, d);
}
Rational Rational::operator*(const Rational &right) {
int n = numerator * right.numerator;
int d = denominator * right.denominator;
return Rational(n, d);
}
Rational Rational::operator/(const Rational &right) {
int n = numerator * right.denominator;
int d = denominator * right.numerator;
return Rational(n, d);
}
Rational &Rational::operator++() {
numerator += denominator;
return *this;
}
Rational &Rational::operator–() {
numerator -= denominator;
return *this;
}
Rational Rational::operator++(int) {
Rational temp(numerator, denominator);
numerator += denominator;
return temp;
}
Rational Rational::operator–(int) {
Rational temp(numerator, denominator);
numerator -= denominator;
return temp;
}
const Rational &Rational::operator+=(const Rational &right) {
*this = *this + right;
return *this;
}
const Rational &Rational::operator-=(const Rational &right) {
*this = *this - right;
return this;
}
const Rational &Rational::operator
=(const Rational &right) {
*this = *this * right;
return *this;
}
const Rational &Rational::operator/=(const Rational &right) {
*this = *this / right;
return this;
}
bool Rational::operator<(const Rational &right) {
return (this->compare(right) < 0);
}
bool Rational::operator<=(const Rational &right) {
return (this->compare(right) <= 0);
}
bool Rational::operator>(const Rational &right) {
return (this->compare(right) > 0);
}
bool Rational::operator>=(const Rational &right) {
return (this->compare(right) >= 0);
}
bool Rational::operator==(const Rational &right) {
return (this->compare(right) == 0);
}
bool Rational::operator!=(const Rational &right) {
return (this->compare(right) != 0);
}
Rational::operator double() {
return static_cast(numerator) / denominator;
}
istream &operator>>(istream &in, Rational &r) {
in >> r.numerator >> r.denominator;
int g = r.gcd(r.numerator, r.denominator);
r.numerator /= g;
r.denominator /= g;
return in;
}
ostream &operator<<(ostream &out, const Rational &r) {
out << r.numerator;
if(r.numerator != 0 && r.denominator != 1)
cout << “/” << r.denominator;
return out;
}
int Rational::gcd(int m, int n) {
if((m == 0) && (n == 0))
return 0;
if(m == 0)
return n;
if(n == 0)
return m;
if(m < 0)
m = -m;
if(n < 0)
n = -n;
int r;
while(true) {
r = m % n;
if(r == 0)
break;
m = n;
n = r;
}
return n;
}
int Rational::compare(const Rational &right) {
int n = numerator * right.denominator - denominator * right.numerator;
int d = denominator * right.denominator;
Rational temp = Rational(n, d);
if(temp.numerator > 0)
return 1;
else if(temp.numerator == 0)
return 0;
else
return -1;
}
int main() {
Rational a, b;
cin >> a;
cin >> b;
cout << “a=” << a << endl;
cout << “b=” << b << endl;
cout << a << “+” << b << “=” << a + b << endl;
cout << a << “-” << b << “=” << a - b << endl;
cout << a << "
" << b << “=” << a * b << endl;
cout << a << “/” << b << “=” << a / b << endl;
cout << boolalpha << a << “<” << b << “=” << (a < b) << endl;
cout << boolalpha << a << “>” << b << “=” << (a > b) << endl;
cout << boolalpha << a << "" << b << “=” << (a == b) << endl;
cout << boolalpha << a << “!=” << b << “=” << (a != b) << endl;
Rational c(1, 2);
cout << “c=” << c << endl;
cout << 2.5 << “+” << c << “=” << (2.5 + c) << endl;
cout << ++c << endl;
cout << c << endl;
cout << c++ << endl;
cout << c << endl;
cout << (c += b) << endl;
return 0;
}
【描述】
第6题中的TwoArray类,表示二维数组(矩阵),只能存放整数。修改TwoArray类,使其成为一个类模板。并新增重载关系运算符
和!=,判断两个矩阵是否相等。
【输入】
输入一个3×4矩阵和一个4×4矩阵。
【输出】
见【输出示例】
【输入示例】
0 1 2 3
2 3 4 5
4 5 6 7
0 1 2 3
3 4 5 6
6 7 8 9
9 10 11 12
【输出示例】
array1:
0 1 2 3
2 3 4 5
4 5 6 7
array2:
0 1 2 3
3 4 5 6
6 7 8 9
9 10 11 12
array3:
Row index out ofrange!
array1:
0 1 2 3
3 4 5 6
6 7 8 9
9 10 11 12
array1 == array2
【提示】
设置输出宽度为3。
【来源】
《程序设计基础——以C++为例》第6章实验8

(10分)
我的答案:
template
class TwoArray{
private:
T **pArray=NULL,rowSize,columnSize;
public:
TwoArray(T x,T y){
rowSize=x;
columnSize=y;
pArray = new int *[rowSize];
for (int i = 0;i < rowSize; ++i)
pArray[i] = new int[columnSize];
}
TwoArray(const TwoArray& p){
columnSize = p.columnSize;
rowSize = p.rowSize;
pArray = new int *[p.rowSize];
for (int i = 0;i < p.rowSize; ++i)
pArray[i] = new int[p.columnSize];

	for(int i = 0; i < rowSize; ++i)
	    for(int j = 0; j < columnSize; ++j)
      		pArray[i][j]=p.pArray[i][j];
}	
int getRowSize(){
	return rowSize;
}
int getColumnSize(){
	return columnSize;
}
int &operator()(int row, int column){
	if(row>=rowSize||column>=columnSize)
		throw std::out_of_range("Row index out of range!");
	return pArray[row][column]; 
}
friend void printTwoArray(const TwoArray &array){
	for(int i = 0; i < array.rowSize; ++i){
		for(int j = 0; j < array.columnSize; ++j){
  		  cout<<setw(3)<<setfill(' ')<<array.pArray[i][j];
		}
		cout<<endl;
	}
}

friend bool operator==(const TwoArray& c1, const TwoArray& c2){
	if(c1.columnSize!=c2.columnSize||c1.rowSize!=c2.rowSize)
		return false; 
	for(int i = 0; i < c1.rowSize; ++i)
	    for(int j = 0; j < c1.columnSize; ++j)
	    	if(c1.pArray[i][j]!=c2.pArray[i][j])
						return false;
	return true;
}

};
题目得分 10
参考答案:
#include
#include
#include
using namespace std;
template
class TwoArray {
public:
TwoArray(int rowSize, int columnSize);
TwoArray(const TwoArray &a);
~TwoArray();
const TwoArray &operator=(const TwoArray &right);
T &operator()(int row, int column);
T operator()(int row, int column) const;
bool operator==(const TwoArray &right);
bool operator!=(const TwoArray &right);
int getRowSize() const;
int getColumnSize() const;
private:
T *pArray;
int rowSize;
int columnSize;
};
template
TwoArray::TwoArray(int rowSize, int columnSize) {
if(rowSize > 0 && columnSize > 0) {
this->rowSize = rowSize;
this->columnSize = columnSize;
}
else
throw invalid_argument(“Invalid argument!”);
pArray = new T[rowSize * columnSize];
}
template
TwoArray::TwoArray(const TwoArray &a) {
rowSize = a.rowSize;
columnSize = a.columnSize;
pArray = new T[rowSize * columnSize];
for(int i = 0; i < rowSize; ++i)
for(int j = 0; j < columnSize; ++j)
pArray[i * columnSize + j] = a.pArray[i * columnSize + j];
}
template
TwoArray::~TwoArray() {
delete[] pArray;
}
template
const TwoArray &TwoArray::operator=(const TwoArray &right) {
if(this != &right) {
if(rowSize != right.rowSize || columnSize != right.columnSize) {
delete [] pArray;
rowSize = right.rowSize;
columnSize = right.columnSize;
pArray = new T[rowSize * columnSize];
}
for(int i = 0; i < rowSize; ++i)
for(int j = 0; j < columnSize; ++j)
pArray[i * columnSize + j] = right.pArray[i * columnSize + j];
}
return *this;
}
template
T &TwoArray::operator()(int row, int column) {
if(row < 0 || row > rowSize - 1)
throw out_of_range(“Row index out of range!”);
if(column < 0 || column > columnSize - 1)
throw out_of_range(“Column index out of range!”);
return pArray[row * columnSize + column];
}
template
T TwoArray::operator()(int row, int column) const {
if(row < 0 || row > rowSize - 1)
throw out_of_range(“Row index out of range!”);
if(column < 0 || column > columnSize - 1)
throw out_of_range(“Column index out of range!”);
return pArray[row * columnSize + column];
}
template
bool TwoArray::operator==(const TwoArray &right) {
if(rowSize != right.rowSize || columnSize != right.columnSize)
return false;
for(int i = 0; i < rowSize; ++i)
for(int j = 0; j < columnSize; ++j)
if(pArray[i * columnSize + j] != right.pArray[i * columnSize + j])
return false;
return true;
}
template
bool TwoArray::operator!=(const TwoArray &right) {
return !(*this == right);
}
template
int TwoArray::getRowSize() const {
return rowSize;
}
template
int TwoArray::getColumnSize() const {
return columnSize;
}
template
void printTwoArray(const TwoArray &array) {
for(int i = 0; i < array.getRowSize(); ++i) {
for(int j = 0; j < array.getColumnSize(); ++j)
cout << setw(3) << array(i, j);
cout << endl;
}
}
int main() {
TwoArray array1(3, 4);
for(int i = 0; i < array1.getRowSize(); ++i)
for(int j = 0; j < array1.getColumnSize(); ++j)
cin >> array1(i, j);
cout << “array1:” << endl;
printTwoArray(array1);
TwoArray array2(4, 4);
for(int i = 0; i < array2.getRowSize(); ++i)
for(int j = 0; j < array2.getColumnSize(); ++j)
cin >> array2(i, j);
cout << “array2:” << endl ;
printTwoArray(array2);
TwoArray array3(array1);
cout << “array3:” << endl ;
printTwoArray(array3);
try {
array1(3, 4) = 1000;
}
catch(out_of_range &ex) {
cout << ex.what() << endl;
}
array1 = array2;
cout << “array1:” << endl ;
printTwoArray(array1);
if(array1 == array2)
cout << “array1 == array2” << endl;
else
cout << “array1 != array2” << endl;
return 0;
}
【描述】
声明并实现一个Line类,表示线段。Line类包括:
Point类的私有对象数据成员start和end,表示线段的两个端点。
有参构造函数,将线段端点设置为给定的参数。
成员函数slope,计算线段的斜率。
【输入】
输入4个数,分别表示点1坐标(x1, y1)和点2坐标(x2, y2)。
【输出】
输出直线的斜率。
【输入示例】
10 20 30 70
【输出示例】
2.5
【提示】
Point类可以参阅《程序基础基础——以C++为例》第5章实验1。
【来源】
《程序设计基础——以C为例》第6章实验1。

(10分)
我的答案:
class Point{
private:
double x,y;
public:
Point(double X,double Y){
x=X;
y=Y;
}
friend class Line;
};

class Line{
private:
double x,y;
public:
Line(Point &a,Point &b){
x=a.x-b.x;
y=a.y-b.y;
}
double slope(){
return y/x;
}
};
题目得分 10
参考答案:
#include
#include
using namespace std;
class Point {
public:
Point() {
x = y = 0;
}
Point(double x, double y) {
this->x = x;
this->y = y;
}
double getX() const {
return x;
}
double getY() const {
return y;
}
private:
double x;
double y;
};
class Line {
public:
Line(Point start, Point end);
double slope();
private:
Point start;
Point end;
};
Line::Line(Point start, Point end):start(start), end(end) { }
double Line::slope() {
double d1 = end.getX() - start.getX();
double d2 = end.getY() - start.getY();
return d2 / d1;
}
int main() {
double x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2;
Point start(x1, y1);
Point end(x2, y2);
Line line(start, end);
cout << line.slope() << endl;
return 0;
}

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值