C++第二学期期末题库(qlu题库,自用)
重点:继承,友元,重载,进制,构造函数

第一次实验
填空
1、

2、


3、

可以看出,将李白所处的情境整理出来后是一颗二叉树结构,并且我们可以通过递归的方式来实现其遍历过程
当然在进行递归的时候我们要注意终止条件的判断,这题的判断是:
从家门口开始走,有两种可能,遇到店和遇到花
仅当李白在店内的次数不超过5次,并且在花店的次数也不超过9次,并且最后一次已经确定时,就可以进行深度搜索。
代码:
#include<bits/stdc++.h>
using namespace std;
int ans=0;
void dfs(int dian,int hua,int jiu){
if(dian > 5 || hua > 9){
return;
}
if(dian == 5 && hua == 9 && jiu == 1){
ans++;
return;
}
dfs(dian+1,hua,jiu*2);
dfs(dian,hua+1,jiu-1);
}
int main(){
dfs(0,0,2);
cout << ans;
return 0;
}
4、
小明最近看过一部名为《第39级台阶》的影片。离开电影院时,他清点了一遍礼堂前的台阶数目,并发现正好是39个台阶!
站在台阶前,他突然又想着一个问题:
如果每次上升时我最多只能提升一级或两级台阶。按照正确的步行方法应当开始时应当先抬左脚,并且随后左右脚交替抬高以保证平衡。由此可见总步数必定为偶数。那么面对39级台阶这样的楼梯设置共有多少种不同的通过路线呢?
请你利用计算机的优势,帮助小明寻找答案。
__ 【正确答案: 51167078 】
因此我们需要对每一个分支进行前序遍历(根左右),这样左子节点只需移动一步而右子节点则需要移动两步。这样就将递归调用从访问具体节点转换为数值参数 dfs(1), dfs(2)。
思路比较清晰,只需要递归:
当走一步,走了一节台阶:f(step+1,n+1);
当走一步,走了两节台阶:f(step+1,n+2);
递归的出口就是偶数步,并且台阶数为39节。
代码:
//第39级台阶
#include<bits/stdc++.h>
using namespace std;
int num=0;
void f(int step,int n)
{
if(n>39)
return;
if(n==39 && step%2==0)
{
num++;
return;
}
f(step+1,n+1);
f(step+1,n+2);
}
int main ()
{
f(0,0);
printf("%d",num);
return 0;
}
编程题
1、
该商店规定:三空汽水瓶可兑换一瓶汽水。小张持有十个空汽水瓶。她最多能换取多少瓶汽水饮用?答案是5瓶,请详细计算如下:首先以9个空瓶子兑换3瓶满装的饮料(剩余1个空瓶子)。饮用完这3杯之后会产生另外2个新 empties(加上最初的剩余1个),此时共有3 empty bottles available for further exchange. 于是可以用其中的3 empty bottles再次兑换1 bottle of drink, 喝掉之后产生1 new empty bottle, 此时共有1 empty bottle left. 接下来, 等待老板提供借用1 empty bottle用于兑换, 饮用之后产生1 full drink, 喝完后利用原有的2 empty bottles(加上刚还给老板的那个)进行最后一次兑换获得最后一份饮料并归还empty bottles to the shopkeeper. 如果小张持有n个empty bottles, 最多能兑换多少满装的饮料呢?
输入例子:
3
10
81
0
输出例子:
1
5
40
#include<stdio.h>
int main ()
{
int i,j,a;
for (i=1; i<100; i++)
{
for (j=1; j<100; j++)
{
a=i*19+j*23;
if (a==823&&i>j)
printf ("%d\n",j);
}
}
return 0;
}
2、学生成绩管理系统
【问题描述】编写一个基于结构体得学生成绩信息管理系统。
主要功能如下: 1. 用结构体存放所有数据。
2. 每个功能都用函数实现。
3. 输入10个学生的学号和三门课程的成绩。
4. 计算每个学生的总分。
5. 按总分从高到低排序。
6. 加上名次一列。
7. 输出最后的二维表格样式的成绩,含学号、三门课成绩、总分、名次。
请编写五个函数完成相应功能的实现。其中:
input_data(STU s[]):输入原始数据函数。
calculate(STU s[]):计算总分函数。
sort_total(STU s[]):根据总分降序排序函数。
add_rank(STU s[]):增加名次列函数。
print_data(STU s[]):输出二维表格样式成绩的函数。
【输入形式】用户依次输入10名学生的学号和三门课成绩。
输出形式
【样例输入】
10001 67 71 69
10002 78 83.5 94.2
10003 61 67 87.5
10004 88 89 92
10005 66.7 78.6 93
10006 67 72 77
10007 74 63 89
10008 93 74 66
10009 74 76 81
10010 78 91 83
【样例输出】
Number: Chinese: Mathematics: English: Total score: Ranking
10004 88 89 92 269 1
10002 78 83.5 94.2 255.7 2
10010 78 91 83 252 3
10005 66.7 78.6 93 238.3 4
10008 93 74 66 233 5
10009 74 76 81 231 6
10007 74 63 89 226 7
10006 67 72 77 216 8
10003 61 67 87.5 215.5 9
10001 67 71 69 207 10
样例说明
评分标准
评分标准
#include<iostream>
#include<iomanip>
using namespace std;
const int N=10;
struct STU
{
public:
int num;
double score1;
double score2;
double score3;
double sum;
};
STU s[N];
void input_data(STU s[],int i)
{
cin>>s[i].num>>s[i].score1>>s[i].score2>>s[i].score3;
}
void calculate(STU s[],int i)
{
s[i].sum=s[i].score1+s[i].score2+s[i].score3;
}
void total(STU s[],int i)
{
STU temp;
for(int i=1;i<=10;i++)
{
for(int j=1;j<=10-i;j++)
{
if(s[j].sum<s[j+1].sum)
{
temp=s[j+1];
s[j+1]=s[j];
s[j]=temp;
}
}
}
}
void print_s(STU s[],int i)
{
cout<<std::left<<setw(15)<<s[i].num<<setw(15)<<s[i].score1<<setw(15)<<s[i].score2;
cout<<std::left<<setw(15)<<s[i].score3<<setw(15)<<s[i].sum<<setw(15)<<i;
cout<<endl;
}
int main()
{
for(int i=1;i<=10;i++)
{
input_data(s,i);
calculate(s,i);
total(s,i);
}
cout<<"Number: Chinese: Mathematics: English: Total score: Ranking";
cout<<endl;
for(int i=1;i<=10;i++)
{
print_s(s,i);
}
return 0;
}
3、
输入形式
输出形式
2
0
86399**【样例输出】**
00:00:00
23:59:59
#include <iostream>
#include <iomanip>
using namespace std;
// 函数将秒数转换为HH:MM:SS格式
void convertSecondsToTime(int t) {
int hours = t / 3600;
int minutes = (t % 3600) / 60;
int seconds = t % 60;
// 使用setw和setfill来格式化输出,保证HH, MM, SS都是两位数
cout << setw(2) << setfill('0') << hours << ":"
<< setw(2) << setfill('0') << minutes << ":"
<< setw(2) << setfill('0') << seconds << endl;
}
int main() {
int T;
cin >> T; // 读取数据组数
for (int i = 0; i < T; ++i) {
int t;
cin >> t; // 读取每组的秒数
convertSecondsToTime(t); // 转换并输出
}
return 0;
}
4、
问题描述
问题描述
【输入形式】用户依次输入5名学生的学号、姓名和总成绩。
输出形式
【样例输入】
1 aaaa 73 2 bbbbb 78 3 wanf 88 4 li 68 5 jing 89
【样例输出】
5 jing 89
3 wanf 88
2 bbbbb 78
1 aaaa 73
4 li 68
样例说明
样例描述
样例描述
评分标准
评分标准
评分标准
#include<iostream>
#include<string>
#include<iomanip>
using namespace std;
class student
{
public:
int num;
string name;
int score;
student()
{
num=0;
name="000";
score=0;
}
};
student tmp;
void test01(student stu[])
{
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 5; j++)
{
if (stu[j].score < stu[j + 1].score)
{
tmp = stu[j];
stu[j] = stu[j + 1];
stu[j + 1] = tmp;
}//使用平均成绩将学生信息进行排序
}
}
}
int main()
{
student stu[10];
//student stu1;
for(int i=1;i<=5;i++)
{
cin>>stu[i].num>>stu[i].name>>stu[i].score;
}
test01(stu);
for(int j=0;j<5;j++)
{
//printf("%d %s %d",stu[j].num,stu[j].name,stu[j].score);
cout<<setw(15)<<stu[j].num;
cout<<setw(15)<<stu[j].name;
cout<<setw(15)<<stu[j].score;
cout<<endl;
}
return 0;
}
5、
问题描述
问题描述
【输入形式】根据系统提示,输入年、月、日。
【输出形式】输出该日在本年中是第几天。
【样例输入】
2022 12 3
【样例输出】
input year,month,day:
12/3 is the 337th day in 2022.
【样例说明】根据系统提示,输入年、月、日。输出该日在本年中是第几天。
【评分标准】 结果完全正确得15分,每个测试点5分。提交程序名为:xt7-1-1.c或xt7-1-1.cpp
,#include<iostream>
using namespace std;
struct Time
{
public:
int year;
int month;
int day;
};
bool leapYear(int year)
{
if(year%4==0&&year%100!=0)
{
return true;
}
if(year%400==0)
{
return true;
}
else return false;
}
int month_day(int month,int year)
{
switch(month)
{
case 4:
case 6:
case 9:
case 11:
return 30;
case 2:
if(leapYear(year))
{
return 29;
}
else
{
return 28;
}
default:
return 31;
}
}
int showTime(int day,int month,int year)
{
int mon;
for(int i=1;i<month;i++)
{
mon+=month_day(i,year);
}
return mon+day;
}
int main()
{
Time t1;
cout<<"input year,month,day:"<<endl;
cin>>t1.year>>t1.month>>t1.day;
int res=showTime(t1.day,t1.month,t1.year);
cout<<t1.month<<"/"<<t1.day<<" is the "<<res<<"th day in "<<t1.year<<".";
//cout<<res;
return 0;
}
重载运算符
函数类型 operator 运算符(形参表)
{
函数体;
}
第二次实验
1、
问题描述
初始化说明
年龄: 身高: 体重:
8 120 60
14 156 78
18 170 110
【输入形式】用构造函数进行初始化, 用户无需输入数据。
【输出形式】分别输出3个对象的序号、年龄、身高、体重,以及输出总人数。
【样例输入】
【样例输出】
Number1:
Age:9years old
Height:122cm
Weight:62jin
Number2:
Age:15years old
Height:158cm
Weight:80jin
Number3:
Age:19years old
Height:172cm
Weight:112jin
Number of people:3
样例说明
样例说明
评分标准
评分标准
/*编写设计一个People类,用于计算成长数据。该类的数据成员有年龄(age)、身高(height)、
体重(weight)和人数(num),其中人数为静态数据成员,成员函数有构造函数(People)、进
食(Eatting)、运动(Sporting)、睡眠(Sleeping)、显示(Show)和显示人数(ShowNum)。其中
构造函数由已知参数年龄(a)、身高(h)和体重(w)构造对象,进食函数使体重加1,运动函数使
身高加1,睡眠函数使年龄、身高、体重各加1,显示函数用于显示人的年龄、身高、体重,显示
人数函数为静态成员函数,用于显示人的个数。假设年龄的单位为岁,身高的单位为厘米,体重
的单位为市斤,要求所有数据成员为protected访问权限,所有成员函数为public访问权限,
在主函数中通过对象直接访问类的所有成员函数。 */
#include <iostream> // 预处理命令
using namespace std; // 使用标准命名空间std
// 人(people)类
class People
{
protected:
// 数据成员:
int age; // 年龄
int height; // 身高
int weight; // 体重
static int num; // 人数
public:
// 公有函数:
People(int a, int h, int w); // 构造函数
void Eatting(){ weight++; } // 进食使体重加1
void Sporting(){ height++; } // 运动使身高加1
void Sleeping(); // 睡眠
void Show() const; // 显示人的信息
static void ShowNum() // 显示人数
{ cout << "Number of people:" << num << endl; }
};
int People::num = 0; // 初始化静态数据成员num
// 人(people)类的实现部分
People::People(int a, int h, int w): age(a), height(h), weight(w)
{ num++; } // 由已知信息构造对象, 人数num将自加1
void People::Sleeping() // 睡眠
{
age++; // 睡眠使年龄加1
height++; // 睡眠使身高加1
weight++; // 睡眠使体重加1
}
void People::Show() const
// 显示人的信息
{
cout << "Number" << num << ':' << endl; // 显示人的序号
cout << "Age:" << age << "years old" << endl; // 显示年龄
cout << "Height:" << height << "cm" <<endl; // 显示身高
cout << "Weight:" << weight << "jin" << endl;// 显示体重
cout << endl; // 换行
}
int main(void) // 主函数main(void)
{
People obj1(8, 120, 60); // 定义对象
obj1.Eatting(); // 进食
obj1.Sporting(); // 运动
obj1.Sleeping(); // 睡眠
obj1.Show(); // 显示信息
People obj2(14, 156, 78); // 定义对象
obj2.Eatting(); // 进食
obj2.Sporting(); // 运动
obj2.Sleeping(); // 睡眠
obj2.Show(); // 显示信息
People obj3(18, 170, 110); // 定义对象
obj3.Eatting(); // 进食
obj3.Sporting(); // 运动
obj3.Sleeping(); // 睡眠
obj3.Show(); // 显示信息
People::ShowNum(); // 显示人数
system("PAUSE"); // 调用库函数system( ),输出系统提示信息
return 0; // 返回值0,返回操作系统
}
2、
输入形式
输入形式
输入形式
输入形式
输入形式
输入形式
输入形式
输入形式
输入形式
输入形式
输入包含一个非负整数a,表示要转换的数。0<=a<=2147483647
【输出形式】
输出这个整数的16进制表示
【样例输入】
【样例输出】
#include <iostream>
using namespace std;
string h="0123456789ABCDEF";
int main()
{
int x;
string s="";
cin>>x;
do
{
s=h[x%16]+s;
x/=16;
}
while(x);
cout<<s<<endl;
return 0;
}
这是一个do-while循环,用于将十进制数x转换为十六进制。
x%16$: 求取x模16的结果。该结果对应于h字符串中的某个字符, 即x当前最低位的有效数字在十六进制下的表现形式。$h[x%16]$: 利用索引运算符访问h序列中的特定字符。$s = h[x%16] + s; $: 将该字符插入到s序列最前端位置, 实际上生成新的串变量并赋值给s。$x /= 16; $: 对x进行基数转换操作, 实际上是移除了x当前低位的所有十六进制表示信息。
循环继续执行,直到x变为0,此时我们已经处理了x的所有位。
第三次实验
太简单,不写了嘿嘿
第四次实验
1、
问题描述
问题描述
【输入形式】按照系统提示,分别输入两个复数和一个实数。
【输出形式】分别输出两个复数之和、实数和复数之和、复数和实数之和。
【样例输入】
12.4 -5.3
3.2 -1.6
-7.8
【样例输出】
Input real part and imaginary part of complex number:
Input real part and imaginary part of complex number:
Input a real number:
c1 + c2 = ( 15.6-6.9i )
i + c1 = ( 4.6-5.3i )
c1 + i = ( 4.6-5.3i )
样例说明
样例说明
评分标准
评分标准
评分标准
#include<bits/stdc++.h>
using namespace std;
typedef double db;
class Complex
{
public:
db real;
db imag;
public:
void set_com()
{
cin>>real>>imag;
}
char aa(db t)
{
if(t>0) return '+';
}
};
Complex Complex::operator+(Complex &c2)
{
Complex c3;
c3.imag=imag+c2.imag;
c3.real=real+c2.real;
return c3;
}
Complex Complex::operator-(Complex &c2)
{
Complex c3;
c3.imag=imag-c2.imag;
c3.real=real-c2.real;
return c3;
}
Complex Complex::operator*(Complex &c2)
{
Complex c3;
c3.imag=imag*c2.real+c2.imag*real;
c3.real=real*c2.real-imag*c2.imag;
cout<<"c1*c2=("<<c3.real<<aa(c3.imag)<<c3.imag<<"i)"<<endl;
return c3;
}
Complex Complex::operator+(Complex &c2)
{
Complex c3,c4,c5,c6;
c4.imag=-c2.imag;
c4.real=c2.real;
c5=c4*c2;
c6=c1*c4;
c3.imag=c6.imag/c5.imag;
c3.real=c6.real/c5.real;
cout<<"c1/c2=("<<c3.real<<aa(c3.imag)<<c3.imag<<"i)"<<endl;
return c3;
}
int main()
{
cout<<"Please enter the real and imaginary parts of c1:"<<endl;
Complex c1;
set_com();
cout<<"Please enter the real and imaginary parts of c2:"<<endl;
Complex c2;
set_com();
Complex c3=c1+c2;
cout<<"c1+c2=("<<c3.real<<c3.aa(c3.imag)<<c3.imag<<"i)"<<endl;
Complex c4=c1-c2;
cout<<"c1-c2=("<<c4.real<<c4.aa(c4.imag)<<c4.imag<<"i)"<<endl;
Complex c5=c1*c2;
cout<<"c1*c2=("<<c5.real<<c5.aa(c5.imag)<<c5.imag<<"i)"<<endl;
Complex c6=c1/c2;
cout<<"c1/c2=("<<c6.real<<c6.aa(c6.imag)<<c6.imag<<"i)"<<endl;
return 0;
}
第六次实验
略
第七次实验
略
第八次实验
1、
问题描述
问题描述
class Vehicle
{ protected:
string NO;//编号
public:
virtual void display()=0;//输出应收费用
}
以Vehicle为基类 ,构建出Car、Truck和Bus三个派生类 。
Car的收费公式为: 载客数8+重量2;
Truck的收费公式为:重量*5;
Bus的收费公式为: 载客数*3;
实现上述类并编写主函数,在主函数中需满足有一个基础类 Vehicle 的指针数组,并规定其数量控制在10个以内。
Vehicle *pv[10];
主函数基于输入信息创建相应的Car、Truck及Bus对象类型。为Car类指定乘客容量与总重量参数;为Truck类仅提供总重量参数;为Bus类设定乘客容量参数。假设所有乘客容量与总重量均为整数值,并提示应采用虚函数来实现多态性。
输入形式
【输出形式】要求输出各车的编号和收费值。
【样例输入】
2 00012 2000
1 00011 4 600
3 00015 26
0
【样例输出】
00012 10000
00011 1232
00015 78
样例说明
样例解释
评分标准
评分标准
#include<bits/stdc++.h>
using namespace std;
class Vehicle
{
public:
string NO;
virtual void display()=0;
};
class Car:public Vehicle
{
public:
int weight;
int num;
virtual void display()
{
cout<<NO<<" "<<weight*2+num*8<<endl;
}
};
class Truck:public Vehicle
{
public:
int weight;
int num;
virtual void display()
{
cout<<NO<<" "<<weight*5<<endl;
}
};
class Bus:public Vehicle
{
public:
int weight;
int num;
virtual void display()
{
cout<<NO<<" "<<num*3<<endl;
}
};
void dodisplay(Vehicle &v1)
{
v1.display();
}
void test01()
{
Car c1;
Truck t1;
Bus b1;
for(int i=0;i<10;i++)
{
int n;
cin>>n;
if(n==0) break;
if(n==1)
{
cin>>c1.NO;
cin>>c1.num>>c1.weight;
dodisplay(c1);
}
else if(n==2)
{
cin>>t1.NO>>t1.weight;
dodisplay(t1);
}
else if(n==3)
{
cin>>b1.NO>>b1.num;
dodisplay(b1);
}
}
}
int main()
{
test01();
return 0;
}
2、
问题描述
轿车Car :每小时8元,超过30分钟按一小时计算,未超过30分钟不收费;
客车Bus 的票价规定如下:每日每座位费用为十元整;乘客可享半价优惠:乘车时间不超过三十分钟者只须支付五元整;若乘车时间少于一小时但超过三十分钟,则须支付十元整的车费;若乘车时间不足一小时且仅行驶了三十分钟,则按一小时计算并收费十元整
货车Truck :每小时15元,未达1小时的按1小时计算;
其他未知类型:按默认方法计算,见裁判程序。
为了更好地实现功能需求,请增添必要的类体及其相关函数体,并确保Parker停车场类能够处理停入的n辆车的费用。
程序框架如下 :
#include
#include
using namespace std;
const float Car_Price= 8;
const float Bus_Price= 10;
const float Truck_Price=15;
class Vehicle{
protected:
float price;
int minute;
public:
virtual float computeFee(){return 20.0f*minute/60;};
virtual string getType(){return "unknow"; };
float getPrice();
void setPrice(float _price);
int getMinute();
void setMinute(int _minute);
Vehicle(float _price,int _minute);
Vehicle(){
};
};
/ 你的代码将被嵌入这里 /
int main(){
int n,minute; //停车辆次,时长
char type;
Vehicle *vp;
Parker p;
cin>>n;
for(int i=1;i<=n;i++){
cin>>type>>minute;
switch(type){
case 'c':vp=new Car(minute);break;
case 't':vp=new Truck(minute);break;
case 'b':vp=new Bus(minute);break;
default: vp=new Vehicle();vp->setMinute(minute);
}
cout<<"type:"<
p.park(vp);
delete vp;
}
cout<<"sum:"<<p.getSum()<<endl;
return 0;
}
输入形式
改写说明
输出形式
输出形式
【样例输入】
4
b 135
c 240
t 56
c 42
【样例输出】
type:bus minute:135 fee:25
type:car minute:240 fee:32
type:truck minute:56 fee:15
type:car minute:42 fee:8
sum:80
样例说明
样例说明
评分标准
评分标准
/*在停车场收费系统中,收费者会根据车型的不同按不同的单价和计费方式收取不同的停车费,其中:
轿车Car:每小时8元,超过30分钟按一小时计算,未超过30分钟不收费
客车Bus:每小时10元,30分钟以内按半小时计费,未达1小时但超过30分钟按1小时计算
货车Truck:每小时15元,未达1小时的按1小时计算
其他未知类型:按默认方法计算,见裁判程序。
请补充必要的类和函数定义,使停车场类Parker可收取停入的n辆车的费用。
*/
#include <iostream>
#include <string>
using namespace std;
const float Car_Price= 8;
const float Bus_Price= 10;
const float Truck_Price=15;
class Vehicle{
protected:
float price;
int minute;
public:
virtual float computeFee(){return 20.0f*minute/60;};
virtual string getType(){return "unknow"; };
float getPrice();
void setPrice(float _price);
int getMinute();
void setMinute(int _minute);
Vehicle(float _price,int _minute);
Vehicle(){
};
};
void Vehicle::setMinute(int _minute){
minute=_minute;
}
int Vehicle::getMinute(){
return minute;
}
void Vehicle::setPrice(float _price){
price=_price;
}
float Vehicle::getPrice(){
return price;
}
Vehicle::Vehicle(float _price,int _minute){
price=_price;
minute=_minute;
}
class Car:public Vehicle
{
public:
Car(int m):Vehicle(Car_Price,m){};
virtual string getType(){return "car";};
virtual float computeFee()
{
int h=minute/60;
if(minute-h*60>30) h++;
return h*price;
};
};
class Bus:public Vehicle
{
public:
Bus(int m):Vehicle(Bus_Price,m){};
virtual string getType(){return "bus";};
virtual float computeFee()
{
int h=minute/30;
if(h*30<minute) h++;
return h*price/2;
};
};
class Truck:public Vehicle
{
public:
Truck(int m):Vehicle(Truck_Price,m){};
virtual string getType(){return "truck";};
viruztual float computeFee()
{
int h=minute/60;
if(h*60<minute) h++;
return h*price;
};
};
class Parker{
double sum=0;
public:
void park(Vehicle *a){
sum+=a->computeFee();
}
double getSum(){
return sum;
}
};
int main(){
int n,minute; //停车辆次,时长
char type;
Vehicle *vp;
Parker p;
cin>>n;
for(int i=1;i<=n;i++){
cin>>type>>minute;
switch(type){
case 'c':vp=new Car(minute);break;
case 't':vp=new Truck(minute);break;
case 'b':vp=new Bus(minute);break;
default: vp=new Vehicle();vp->setMinute(minute);
}
cout<<"type:"<<vp->getType() <<" minute:"<<vp->getMinute()<<" fee:"<<vp->computeFee()<<endl;
p.park(vp);
delete vp;
}
cout<<"sum:"<<p.getSum()<<endl;
return 0;
}
作业2
1、
问题描述
问题描述
【输入形式】根据系统提示,输入两个2行3列的矩阵。
【输出形式】输出两个矩阵a和b,以及两个矩阵的和。
【样例输入】
1 2 3 4 5 6
5 6 7 8 9 0
【样例输出】
input value of matrix:
input value of matrix:
Matrix a:
1 2 3
4 5 6
Matrix b:
5 6 7
8 9 0
Matrix c = Matrix a + Matrix b :
6 8 10
12 14 6
样例说明
样例说明
【评分标准】 结果完全正确得15分,每个测试点5分。提交程序名为:xt10-4.c或xt10-4.c
#include<bits/stdc++.h>
using namespace std;
class Matrix
{
public:
int seq[2][3];
void display(char a)
{
if(a=='c')
{
cout<<"Matrix c = Matrix a + Matrix b :"<<endl;
}
else
cout<<"Matrix "<<a<<":"<<endl;
for(int i=0;i<2;i++)
{
for(int j=0;j<3;j++)
{
cout<<seq[i][j]<<" ";
}
cout<<endl;
}
cout<<endl;
}
void input()
{
cout<<"input value of matrix:"<<endl;
for(int i=0;i<2;i++)
{
for(int j=0;j<3;j++)
{
cin>>seq[i][j];
}
}
}
Matrix operator +(Matrix&);
};
Matrix Matrix::operator +(Matrix& matrix)
{
Matrix rem;
for(int i=0;i<2;i++)
{
for(int j=0;j<3;j++)
{
int sum=seq[i][j]+matrix.seq[i][j];
rem.seq[i][j]=sum;
}
}
return rem;
}
int main()
{
Matrix m1;
m1.input();
Matrix m2;
m2.input();
Matrix m3=m1+m2;
m1.display('a');
m2.display('b');
m3.display('c');
}
