计科C++上机练习-代码参考与题目汇总

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

上机练习C++题目汇总代码参照
1-1001
练习cin与cout
Time/Memory Limit:1000 MS/32768 K
Submitted:46Accepted:44
Problem Description
从键盘接收3各双精度的浮点数,然后在屏幕上将它们以浮点数的形式显示处理。

要求使用cin和cout来实现。

Input
输入3个浮点数,以空格或者回车作为数据之间的分隔。

Output
3三个浮点数,占一行。

Sample Input
1.23 4.56 987.1
Sample Output
1.23
4.56
987.1
1-1002
LP的车票
Time/Memory Limit:1000 MS/32768 K
Submitted:80Accepted:46
Problem Description
五一就要到了,LP迫不及待地开始买动车票回家,他惊讶地发现他的票竟然是“ACM特别订制票”,票上有几个主要信息:发车时间,到站时间,还有动车的车号,你能根据他给的信息把他的票打印出来吗?
Input
有多组输入数据,分别为发车时间(hh:mm:ss),到站时间(hh:mm:ss),还有动车车号。

Output
按Sample Ouput的样式输出。

Sample Input
12 0 0 14 0 0 ZSACM411
8 45 0 9 30 0 ACM12080
Sample Output
*******************
ZSACM411
12:00:00---14:00:00
*******************
*******************
ACM12080
08:45:00---09:30:00
*******************
1-1003
计算个人收入所得税
Time/Memory Limit:1000 MS/32768 K
Submitted:53Accepted:45
Problem Description
个人收入所得税起征点为2000元,超过的部分x为应纳税所得额,纳税按如下规则计算:
应纳税所得额税率速算扣除数x<5005%0
500<=x<200010%25
2000<=x<500015%125
x>=500020%375
例:收入为7000元,则所得税为:(7000-2000)×20%-375=625元。

Input
输入数据为多组个人收入,每组占一行。

Output
对于每组个人收入,输出应缴个人收入所得税,保留两位小数,每组输出占一行。

Sample Input
2800
4500
7000
Sample Output
55.00
250.00
625.00
1-1004
找出最大最小
Time/Memory Limit:1000 MS/32768 K
Submitted:59Accepted:43
Problem Description
编写函数,以数组名做形式参数,对整数数组,将其中最小的数与第一个数进行交换,把最大的数与最后一个数对换。

动态生成数组
Input
第一行一个整数N,代表下面有N行测试数据。

下面N行,每一行第一个整数M,代表该数组共M个数据。

后面是M个数据。

M<10
Output
按照要求输出结果。

Sample Input
4
4 5 4 3 1
6 5
7
8 8 4 9
6 99 45 41 5
7 86 54
3 41 99 23
Sample Output
1 4 3 5
4 7 8 8
5 9
41 45 54 57 86 99
23 41 99
2-1001
3个数排序(要求用函数实现,参数用引用类型)Time/Memory Limit:1000 MS/32768 K
Submitted:94Accepted:56
Problem Description
输入3个整数,按由小到大的顺序排列后输出
Input
输入数据包含多个测试实例,每个测试实例占一行,每行有3个整数
Output
对于每个测试实例,一个测试实例输出一行。

Sample Input
34,21,25
3,2,1
Sample Output
21,25,34
1,2,3
2-1002
找出每组数据中的最大值(函数重载)
Time/Memory Limit:1000 MS/32768 K
Submitted:56Accepted:44
Problem Description
定义函数Max实现找出每组测试数据中的最大值,在主函数中进行调用,要求Max函数能够实现分别在2个int型数据、2个字符串(不包括空格,长度不超过50)、3个double型数据中找到最大值。

(1.2 4.5 987.1 ?)Input
输入数据有三组,每组占一行。

Output
对于每组输入数据,输出一行。

Sample Input
1 2
55.6 25.7 88.8
good morning
Sample Output
max:2
max:88.8
max:morning
2-1003
求平均值
Time/Memory Limit:1000 MS/32768 K
Submitted:71Accepted:55
Problem Description
求n个数的平均值。

要求用new和delete进行内存空间申请和释放。

Input
输入数据有多组,每组占一行,每行的第一个数是n,表示要求n个数的平均值,后面的n个数是具体的数值。

Output
对于每组输入数据,输出平均值,结果保留2位小数,每组输出占一行。

Sample Input
3 99 98 97
4 100 99 98 97
Sample Output
98.00
98.50
3-1001
对象作为普通函数的参数
Time/Memory Limit:1000 MS/32768 K
Submitted:72Accepted:51
Problem Description
定义一个日期类,它的数据成员有年、月、日;它的函数成员有设置值、打印输出和判断是否闰年。

在类外部有一个普通函数,它的功能是判断一个日期类对象是否闰年,并在屏幕上显示:“leap year”或者“not leap year”。

Input
输入数据有多行,每行代表一个日期。

Output
输出有多行,每行代表一个日期的判断结果。

Sample Input
2000 1 2
2001 3 8
2012 1 1
Sample Output
2000/1/2 leap year
2001/3/8 not leap year
2012/1/1 leap year
3-1002
构造函数--定义矩形
Time/Memory Limit:1000 MS/32768 K
Submitted:55Accepted:52
Problem Description
定义一个矩形类,要求定义成员函数实现:构造长方形、计算长方形的面积、周长,假定长和宽分别由两个整型变量high和width表示。

Input
输入数据有多行,每行有两个数据,代表矩形的长和宽。

Output
每组输出占一行,为矩形的面积和周长并用空格隔开. Sample Input
1 2
10 20
100 200
Sample Output
2 6
200 60
20000 600
3-1003
数列中n个整数排序
Time/Memory Limit:1000 MS/32768 K
Submitted:63Accepted:46
Problem Description
设计一个包含size个数的数列,要求能够把从指定位置
x开始的y个数排列成降序,并输出新的完整的数列。

可将数列存放在一维数组中。

例如,原来列有10个数,值为{1,8,3,0,5,9,7,6,9,8},若要求把从第4个数开始的5个数排成降序,则得到的新数列为{1,8,3,9,7,6,5,0,9,8}。

试建立一个类LIST,来完成上述功能。

class LIST
{
public:
LIST(int len); //构造函数,用len初始化size,根据size动态分配数组存储空间,arr指向该存储空间,从键盘输入数据到arr数组中
void sortpart(int m,int n);//将数列从第m个元素开始的n个数排成降序
void output();//输出整个数列
~LIST();//析构函数,释放arr指向的存储空间private:
int size;//数列元素的个数
int *arr;//数列数组的起始指针
};
Input
输入数据有多组,每组:
第1行输入整型数列的个数n;
第2行输入整型数列各个元素的值;
第3行输入x和y,表示将超长整型数列从指定位置x 开始的y个数排列成降序;
Output
每组输出排序后的结果,占一行。

每个元素后带一个空格。

Sample Input
10
1 4
2 7
3 8 2 8 23 6
3 5
Sample Output
1 4 8 7 3
2 2 8 2
3 6
4-1001
学生成绩等级
Time/Memory Limit:1000 MS/32768 K
Submitted:62Accepted:53
Problem Description
有一个学生类student,包括学生姓名、成绩,设计一个友元函数,输出成绩对应的等级:大于等于90:A;80~90:B;70~79:C;60~69:D;小于60:E。

Input
先输入数据n,表示有n个学生,接下是n个学生信息,包括姓名和成绩。

Output
对于所有的输入数据,输出学生数据(包括姓名、成绩和等级)。

Sample Input
5
st1 67
st2 80
st3 90
st4 56
st5 88
Sample Output
st1 67 D
st2 80 B
st3 90 A
st4 56 E
st5 88 B
4-1002
类的静态与友元——职工薪水
Time/Memory Limit:1000 MS/32768 K
Submitted:86Accepted:53
Problem Description
设计一个职工类,职工类中包括职工姓名、薪水和所有职工薪水总和(静态成员)三个数据成员,成员函数包括有构造函数、析构函数、及返回所有职工薪水总和的静态成员函数,此外还包含有一个友元函数,用来比较两个员工的薪水,结果返回薪水大的那个员工。

要求将类定义完整并且用主函数进行测试。

Input
输入包括3个测试数据,薪水为整数。

Output
输出包括5行,分别是3个职工的信息,总的薪水总和,还有薪水最高的那个职工的信息。

姓名与薪水之间空两格。

Sample Input
Zhangsan 1200
Lisi 2400
Wangwu 1800
Sample Output
Zhangsan 1200
Lisi 2400
Wangwu 1800
Allsalary is:5400
The highest salary is:Lisi 2400
4-1003
明天几号?
Time/Memory Limit:1000 MS/32768 K
Submitted:75Accepted:51
Problem Description
定义日期类,要求能计算明天的日期。

Input
输入数据有多组,每组占一行,每行中有三个数,用空格分隔。

Output
对于每组输入数据,输出两行。

今天的日期及明天的日期。

格式见样例。

Sample Input
3 20 1997
Sample Output
3-20-1997
3-21-1997
Hints
1. 能够进入下一个月;
2. 能够进入下一年;
3. 不考虑闰年。

5-1001
学生成绩类
Time/Memory Limit:1000 MS/32768 K
Submitted:53Accepted:51
Problem Description
设计一个学生类student包含:姓名、学号和3门功课成绩,要求能实现:
分别输出10个学生中每门功课的最高分及相应学生的姓名和学号。

提示:可以用友元函数来实现.
Input
输入数据只有一组,共有10个学生的信息。

每行输入一个学生的姓名,学号,和3门成绩。

Output
输出10个学生中单科最高分,并输出该生的姓名和学号。

Sample Input
asdf 1234 11 11 11
fad 1235 22 22 22
qwe 1345 33 33 33
knkj 1819 44 44 44
ajdk 1987 55 55 55
djakf 1973 66 66 66
akldjf 1938 77 77 77
adaz 1743 88 88 88
zjijlk 1873 99 99 99
adjfkl 2523 00 00 00
Sample Output
99 zjijlk 1873
99 zjijlk 1873
99 zjijlk 1873
5-1002
长方形的面积和长方体的体积
Time/Memory Limit:1000 MS/32768 K
Submitted:70Accepted:50
Problem Description 先创建一个长方形类,有带参的构造函数,能够计算长方形的面积;在派生出长方体类,有带参的构造函数,能够计算长方体的表面积和体积。

Input
输入数据有多组,每组包含2行数据,第1行2个实数,表示长方形的长和宽;第2行3个实数,表示长方体的长、宽和高。

Output
输出长方形的面积和长方体的表面积和体积;具体格式见样例。

Sample Input
2 3
4 5 6
Sample Output
6
148 120
5-1003
图书销售
Time/Memory Limit:1000 MS/32768 K
Submitted:62Accepted:50
Problem Description
设计一个图书类实现图书的销售管理,包括:书名,价格和销售数量等数据,
要求,可以将销售纪录良好的图书(要求销售量>=100本)的图书书名及价格显示出来,并能统计总共卖了多少本图书。

Input
输入数据有多行,每行包括书名,价格和销售数据;Output
输出占多行,销售纪录良好的图书信息(书名和价格)各占一行,最后一行是所有图书的销售总量。

Sample Input
C++ 31 200
Java 42.5 97
English 23 110
Sample Output
C++ 31
English 23
total:407
6-1001
类的虚基类——客货两用汽车
Time/Memory Limit:1000 MS/32768 K
Submitted:67Accepted:53
Problem Description
设计一个Automobile(汽车)类,派生出Car类和Wagon 类,而后两类又派生出StationWagon类,将四个类定义完整,并在主函数中定义StationWagon类的对象进行测试。

其中:Automobile类包含有power数据成员,及show 成员函数
Car类包含有seat数据成员,及show成员函数
Wagon包含有load数据成员,及show成员函数
StationWagon包含有show成员函数。

Input
输入有多组数据,每组数据包含:动力,座位数,载重量
Output
每组数据输出多行
Sample Input
105 5 200
120 5 250
Sample Output
Automobile constructing...
Car constructing...
Wagon constructing...
StationWagon constructing...
StationWagon's power:105 seat:5 load:200 Automobile constructing...
Car constructing...
Wagon constructing...
StationWagon constructing...
StationWagon's power:120 seat:5 load:250
6-1002
求矩阵之和
Time/Memory Limit:1000 MS/32768 K
Submitted:67Accepted:52
Problem Description
设计一个矩阵类Data,包括矩阵的行、列、矩阵数据等私有数据成员。

要求实现两个矩阵的加。

Input
输入数据有多组,每组输入数据有三行,第一行两个整数,分别代表矩阵的行数n和列数m;第二行有n*m 个整数,是第一个矩阵的数据;第三行也有n*m个整数,是第二个矩阵的数据。

Output
对于每组输入数据,输出两个矩阵的和,元素之间用空格分隔。

Sample Input
3 3
1 1 1
2 2 2
3 3 3
1 1 1 1 1 1 1 1 1
2 3
1 2 3 4 5 6
1 1 1 1 1 1
Sample Output
2 2 2
3 3 3
4 4 4
2 3 4
5 6 76-1003
减肥啦~
Time/Memory Limit:1000 MS/32768 K
Submitted:76Accepted:54
Problem Description
sj和cl最近喜欢上了跳绳(其实是为了减肥啦~),于是她们组织了同学进行了一场比赛,看谁跳的快。

定义一个参赛者的类,包括编号num,姓名name,以及跳的有效次数n下和所用的时间s秒(假设都能匀速跳,并且没有失误)。

请找出跳的最快的那位同学,并输出她的信息(如果遇到跳的一样快的同学,则输出编号小的那个)。

Input
输入数据有多组,每组第一行为m,代表m人参赛。

接下来为m行,每行为参赛者的编号、姓名、跳的次数n、所用时间s。

Output
输出每组跳的最快的同学的编号、姓名。

Sample Input
4
001 chsdf 150 60
002 chein 180 68
003 shizhen 145 58
004 yain 200 75
Sample Output
004 yain
7-1001
类的继承
Time/Memory Limit:1000 MS/32768 K
Submitted:54Accepted:50
Problem Description
定义一个类point,它有两个私有数据成员:点的横坐标x和纵坐标y。

三个公有的成员函数:构造、修改和显示,用于操作数据成员x和y。

类point的定义如下:
class point
{
private:
double x;
double y;
public:
point(double x,double y);
void set_point(double a,double b);
void show();
};
我们用这个类派生出一个类circle。

其新增数据成员为圆的半径,及求圆面积成员函数。

根据以上信息,用point类派生circle类,并给出圆的面积。

PI=3.14 Input
测试数据有多行,第一行表示有几个测试数据,接下来
的每行有三个浮点数,分别表示圆心坐标和圆的半径。

Output
按上面要求,输出圆的面积。

结果保留两位小数。

Sample Input
2
1 1 520.1314
2 2 3.1415926
Sample Output
849485.15
30.99
7-1002
空间坐标运算
Time/Memory Limit:1000 MS/32768 K
Submitted:59Accepted:50
Problem Description
LP这几天因为高数非常心烦,因为他说他把初中的坐标如何运算给忘了(其实是他当初根本就学不会),现在你帮他算算吧!
Input
每组数据有两行:
第一行为第二行输入的数据的格式,共有2种:
1.空间坐标(xyz) '+'或'-' 某个坐标轴('x'||'y'||'z')
2.空间坐标(xyz) '+'或'-' 空间坐标(xyz)
第二行输入所对应个数的整型数字。

Output
输出运算后得到的坐标。

(输出格式详见Sample Output)
Sample Input
xyz + y
1 2 3 2
xyz + x
3 2 1 4
xyz - xyz
3 2 1 1 1 1
Sample Output
(1,4,3)
(7,2,1)
(2,1,0)
7-1003
明天几号?
Time/Memory Limit:1000 MS/32768 K
Submitted:63Accepted:49
Problem Description
定义日期类,包含年、月、日等数据,要求重载++运算符,计算明天的日期。

Input
输入数据有多组,每组占一行,每行中有三个数,用空格分隔。

Output 对于每组输入数据,输出两行。

今天的日期及明天的日期。

格式见样例。

Sample Input
3 20 1997
Sample Output
3-20-1997
3-21-1997
Hints
1. 能够进入下一个月;
2. 能够进入下一年;
3. 不考虑闰年。

8-1001
函数模板——求数组中的最大元素
Time/Memory Limit:1000 MS/32768 K
Submitted:81Accepted:55
Problem Description
编写两个函数模板,分别完成输入数组,求数组中的最大元素。

并写出调用此函数模板的完整程序,使得函数调用时,数组的类型可以是整型也可以是双精度类型。

Input
输入数据有多行,第一行有一个整数m,表示整型数组和双精度数组各有m个,接下来m行为整型数组,m 行为双精度数组,每行的第一个数n为元素的个数,接下来有n个元素。

Output
对于每组输入数据,输出一行。

Sample Input
2
8 1 2 3 4 5 6 7 8
7 8 9 7 10 2 5 1
5 1.1 2.2 3.3 4.4 5.5
6 9.6 5.2 3.2 7.8 8.4 6.2
Sample Output
8
10
5.5
9.6
8-1002
找出每组数据中的最大值(模板)
Time/Memory Limit:1000 MS/32768 K
Submitted:65Accepted:53
Problem Description
定义函数Max实现找出每组测试数据中的最大值,在主函数中进行调用,要求Max函数能够实现分别在2个int型数据、2个字符串(不包括空格,长度不超过50)、3个double型数据中找到最大值。

Input
输入数据有三组,每组占一行。

Output
对于每组输入数据,输出一行。

Sample Input
1 2
55.6 25.7 88.8
good morning
Sample Output
max:2
max:88.8
max:morning
Hints
函数模板与非模板函数重载
8-1003
运算符重载矩阵相加
Time/Memory Limit:1000 MS/32768 K
Submitted:61Accepted:52
Problem Description
有两个矩阵a和b,均为2行3列。

求两个矩阵之和。

重载运算符“+”,使之能用于矩阵相加。

如:c=a+b。

补充下面的类:
class Matrix
{
public:
Matrix();
friend Matrix operator+(Matrix &,Matrix &);
void input();
void show();
private:int mat[2][3];
};
Input
输入数据有多组,每组数据第一行有一个整数N,代表有N个2行3列的矩阵。

Output
输出N个数组相加的矩阵。

按照矩阵格式输出,每个数据后面均有空格。

Sample Input
2
1 2 3
4 5 6
1 1 1
1 1 1
3
1 2 3
4 5 6
1 2 3
4 5 6
1 2 3
4 5 6
Sample Output
2 3 4
5 6 7
3 6 9
12 15 18
End.
代码参考:
1-1001:
#include<iostream> using namespace std; int main()
{
float x,y,z;
cin>>x>>y>>z;
cout<<x<<endl;
cout<<y<<endl;
cout<<z<<endl;
return 0;
}
1-1002:#include<iostream>
using namespace std;
int main()
{
int a,b,c,d,e,f,i;
char s[100];
while(cin>>a>>b>>c>>d>>e>>f>>s)
{
for(i=0;i<19;i++)
cout<<'*';
cout<<endl;
cout<<s<<endl;
cout.width(2);
cout.fill('0');
cout<<a<<':';
cout.width(2);
cout.fill('0');
cout<<b<<':';
cout.width(2);
cout.fill('0');
cout<<c;
cout<<"---";
cout.width(2);
cout.fill('0');
cout<<d<<':';
cout.width(2);
cout.fill('0');
cout<<e<<':';
cout.width(2);
cout.fill('0');
cout<<f;
cout<<endl;
for(i=0;i<19;i++)
cout<<'*';
cout<<endl;
}
return 0;
}
1-1003:
#include<iostream>
using namespace std;
int main()
{
float x,y,z;
while(cin>>x)
{
z=x-2000;
if(z<500)
y=z*0.05;
else if(z<2000)
y=z*0.1-25;
else if(z<5000)
y=z*0.15-125;
else
y=z*0.2-375;
cout.precision(2);
cout.setf(ios::fixed);
cout<<y<<endl;
}
return 0;
}
1-1004:
#include<iostream>
using namespace std;int main()
{
int N,M,i,max,min,x,y;
int s[11];
cin>>N;
while(N--)
{max=0;min=0;
cin>>M;
for(i=0;i<M;i++)
cin>>s[i];
for(i=0;i<M;i++)
{
if(s[i]>s[max])
max=i;
}
x=s[M-1];s[M-1]=s[max];s[max]=x;
for(i=0;i<M;i++)
{
if(s[i]<s[min])
min=i;
}
y=s[0];s[0]=s[min];s[min]=y;
for(i=0;i<M;i++)
cout<<s[i]<<' ';
cout<<endl;
}
return 0;
}
2-1001:
#include<iostream>
using namespace std;
void d(int *a,int *b)
{
int c;
c=*a;*a=*b;*b=c;
}
int main()
{
int x,y,z;
char ch;
while(cin>>x>>ch>>y>>ch>>z)
{
if(x<=y&&x<=z)
{
if(y>=z)
d(&y,&z);
cout<<x<<','<<y<<','<<z;
}
if(y<=x&&y<=z)
{
if(x>=z)
d(&x,&z);
cout<<y<<','<<x<<','<<z;
}
if(z<=x&&z<=y)
{
if(y>=x)
d(&x,&y);
cout<<z<<','<<y<<','<<x;
}
cout<<endl;
}
return 0;
}
2-1002:
#include<iostream>
using namespace std;
#include<cmath>
void Max(int a,int b)
{
if(a>b)
cout<<"max:"<<a<<endl;
else
cout<<"max:"<<b<<endl;
}
void Max(double x,double y,double z) {
if(x>y&&x>z)
cout<<"max:"<<x<<endl;
if(y>x&&y>z)
cout<<"max:"<<y<<endl;
if(z>x&&z>y)
cout<<"max:"<<z<<endl;
}
void Max(char n[],char m[])
{
if(strcmp(n,m)<=0)
cout<<"max:"<<m<<endl;
else
cout<<"max:"<<n<<endl;
}
int main()
{
int a,b;
double x,y,z;
char n[50],m[50];
cin>>a>>b;
cin>>x>>y>>z;
cin>>n>>m;
Max(a,b);
Max(x,y,z);
Max(n,m);
return 0;
}
2-1003:
#include<iostream>
using namespace std;
int main()
{
int n,i;
double y;
int *p;
while(cin>>n)
{y=0;
p=new int[n];
for(i=0;i<n;i++)
cin>>p[i];
for(i=0;i<n;i++)
y=y+p[i];
y=y/n;
cout.precision(2);
cout.setf(ios::fixed);
cout<<y<<endl;
delete[]p;
}
return 0;
}
3-1001:
#include<iostream>
using namespace std;
class Complex
{
private:
int year,month,date;
public:
Complex(int y,int m,int d)
{
year=y;month=m;date=d;
}
void abComplex()
{
cout<<year<<'/'<<month<<'/'<<date<<' ';
}
};
int main()
{
int y,m,d;
while(cin>>y>>m>>d)
{
Complex s(y,m,d);
s.abComplex();
if(y%400==0||y%4==0&&y%100!=0)
cout<<"leap year";
else
cout<<"not leap year";
cout<<endl;
}
return 0;
}
3-1002:
#include<iostream>
using namespace std;
class Complex
{
private:
int high,width;
public:
Complex(int h,int w)
{
high=h;width=w;
}
void abComplex()
{
int C,S;
C=(high+width)*2;
S=high*width;
cout<<S<<' '<<C<<endl;
}
};
int main()
{
int h,w;
int C,S;
while(cin>>h>>w)
{
Complex s(h,w);
s.abComplex();
}
return 0;
}
3-1003:
#include<iostream>
using namespace std;
class LIST
{
public:
LIST(int len)
{
int i;
size=len;arr=new int[size];
for(i=0;i<size;i++)
cin>>arr[i];
}
void sortpart(int m,int n)
{
int i,j,a;
for(i=0;i<n;i++)
{
for(j=m-1;j<m+n-i-2;j++)
{
if(arr[j]<arr[j+1])
{
a=arr[j];
arr[j]=arr[j+1];
arr[j+1]=a;
}
}
}
}
void output()
{
int i;
for(i=0;i<size;i++)
cout<<arr[i]<<' ';
}
~LIST()
{
delete []arr;
}
private:
int size;
int *arr;
};
int main()
{
int n;
int x,y;
while(cin>>n)
{
LIST s(n);
cin>>x>>y;
s.sortpart(x,y);
s.output();
cout<<endl;
}
return 0;
}
4-1001:
#include<iostream>
#include<string>
using namespace std;
class student
{
private:
int score;
string name;
public:
friend void fun(student &a);
student (string n,int s)
{
name=n;score=s;
}
void out()
{
cout<<name<<' '<<score<<' ';
}
};
void fun(student &a)
{
a.out();
if(a.score>=90)
cout<<'A'<<endl;
else if(a.score>=80)
cout<<'B'<<endl;
else if(a.score>=70)
cout<<'C'<<endl;
else if(a.score>=60)
cout<<'D'<<endl;
else
cout<<'E'<<endl;
}
int main()
{
int n,score;
string name;
cin>>n;
while(n--)
{
cin>>name>>score;
student s(name,score);
fun(s);
}
return 0;
}
4-1002:
#include<iostream>
#include<string>
using namespace std;
class aa {
private:
string name;
double sal;
static int tol;
public:
aa(string na,double s)
{
name=na;
sal=s;
tol+=sal;
}
static int f1()
{
ret urn tol;
}
void out1()
{
cout<<name<<" "<<sal<<endl;
}
static void out2()
{
cout<<"Allsalary is:"<<f1()<<endl;
}
void out3()
{
cout<<"The highest salary is:"<<name<<" "<<sal<<endl;
}
friend aa judge(aa &x1,aa &x2)
{
if(x1.sal>=x2.sal)
return x1;
return x2;
}
};
int aa::tol=0.0;
int main()
{
int s;
string name;
cin>>name>>s;
aa r1(name,s);
cin>>name>>s;
aa r2(name,s);
cin>>name>>s;
aa r3(name,s);
r1.out1();
r2.out1 ();
r3.out1();
aa::out2();
aa a=judge(r1,r2);
aa b=judge(a,r3);
b.out3();
return 0;
}
4-1003:
#include<iostream>
using namespace std;
class myDate
{
private:
int year,month,date;
public:
myDate(int y,int m,int d)
{
year=y;month=m;date=d;
}
void out()
{
cout<<month<<'-'<<date<<'-'<<year<<endl;
if(month==1||month==3||month==5||month==7|| month==8||month==10||month==12)
{
if(month!=12)
{
if(date==31)
{month+=1;date=1;}
else
date+=1;
}
else
{
if(date==31)
{year+=1;month=1;date=1;}
else date+=1;
}
}
else if(month==2)
{
if(date==28)
{month+=1;date=1;}
else
date+=1;
}
else
{
if(date==30)
{
month+=1;date=1;
}
else
date+=1;
}
cout<<month<<'-'<<date<<'-'<<year<<endl;
}
};
int main()
{
int year,month,date;
while(cin>>month>>date>>year)
{
myDate a(year,month,date);
a.out();
}
return 0;
}
5-1001:
#include<iostream>
#include<string>
using namespace std;
class student
{
private:
string name;
int id;
int a[3];
public:
void st(string na,int n,int b[])
{
name=na;
id=n;
for(int i=0;i<3;i++)
a[i]=b[i];
}
friend void fun(student*y);
};
void fun(student*y)
{
for(int i=0;i<3;i++)
{
int fun=y[0].a[i];
int t=0;
for(int j=0;j<10;j++)
{
if(fun<y[j].a[i])
{
fun=y[j].a[i];
t=j;
}
}
cout<<y[t].a[i]<<" "<<y[t].name<<" "<<y[t].id<<endl;
}
}
int main()
{
string name;
int id;
int a[3];
student*stu=new student[10];
for(int i=0;i<10;i++)
{
cin>>name>>id;
for(int j=0;j<3;j++)
{
cin>>a[j];
}
stu[i].st(name,id,a);
}
fun(stu);
delete [] stu;
return 0;
}
5-1002:
#include<iostream>
using namespace std;
class C
{
private:
float x,y;
public:
C(float x1,float y1)
{
x=x1;y=y1;
}
void out1()
{
cout<<x*y<<endl;
}
};
class T:public C
{
private:
float a,b,c;
public:
T(float x1,float y1,float a1,float b1,float c1):C(x1,y1)
{
a=a1;b=b1;c=c1;
}
void out()
{
out1();
cout<<2*a*b+2*a*c+2*b*c<<' '<<a*b*c<<endl;
}
};
int main()
{
float a,b,x,y,z;
while(cin>>a>>b>>x>>y>>z)
{
T s(a,b,x,y,z);
s.out();
}
return 0;
}
5-1003:
#include<iostream>
#include<string>
using namespace std;
class Book
{
private:
float x;
int y;
string name;
static int sum;
public:
Book(string na,float x1,int y1)
{
name=na;x=x1;y=y1;
}
static void out(Book &a)
{
sum+=a.y;
if(a.y>=100)
cout<<<<' '<<a.x<<endl;
}
static void fun()
{
cout<<"total:"<<sum<<endl;
}
};
int Book::sum=0;
int main()
{
int sum;
float x;
string name;
int y;
while(cin>>name>>x>>y)
{
Book s(name,x,y);
Book::out(s);
}
Book::fun();
return 0;
}
6-1001:
#include<iostream>
#include<string>
using namespace std;
class Auto
{
protected:
int power;
public:
Auto(int p)
{
power=p;
}
void show()
{
cout<<"Automobile constructing..."<<endl;
cout<<"Car constructing..."<<endl;
cout<<"Wagon constructing..."<<endl;
cout<<"StationWagon constructing..."<<endl;
cout<<"StationWagon's power:"<<power;
}
};
class car:virtual public Auto
{
protected:
int seat;
public:
car(int s):Auto(power)
{
seat=s;
}
void show()
{
cout<<" seat:"<<seat;
}};
class wagon:virtual public Auto
{
protected:
int load;
public:
wagon(int l):Auto(power)
{
load=l;
}
void show()
{
cout<<" load:"<<load<<endl;
}
};
class sta:public car,public wagon
{
protected:
public:
sta(int power,int seat,int load):Auto(power),car(seat),wagon(load)
{
}
void show()
{
Auto::show();
car::show();
wagon::show();
}
};
int main()
{
int power,seat,load;
while(cin>>power>>seat>>load)
{
sta obj(power,seat,load);
obj.show();
}
return 0;
}
6-1002:
#include<iostream>
#include<cstring>
using namespace std;
class Data
{
private:
int n,m;
int a[100],b[100];
public:
Data(int n1,int m1,int a1[],int b1[])
{
n=n1;m=m1;
for(int i=0;i<n*m;i++)
a[i]=a1[i];
a[i]='/0';
for(i=0;i<n*m;i++)
b[i]=b1[i];
b[i]='/0';
}
void out()
{
int c[100];
for(int i=0;i<n*m;i++)
c[i]=a[i]+b[i];
for(i=0;i<n*m;i++)
{
cout<<c[i];
if((i+1)%m!=0)
cout<<' ';
else
cout<<endl;
}
}
};
int main()
{
int n,m,a[100],b[100],i;
while(cin>>n>>m)
{
for(i=0;i<n*m;i++)
cin>>a[i];
for(i=0;i<n*m;i++)
cin>>b[i];
Data s(n,m,a,b);
s.out();
}
return 0;
}
6-1003:
#include<iostream>
#include<string>
using namespace std;
class stu
{
private:
string name,num;
int n,s;public:
void show(string nu,string na,int n1,int s1)
{
num=nu;name=na;n=n1;s=s1;
}
friend stu fun(stu &x,stu &y)
{
if((x.n/(x.s*1.0))>(y.n/(y.s*1.0)))
return x;
else if((x.n/(x.s*1.0))<(y.n/(y.s*1.0)))
return y;
else if(x.num<y.num)
return x;
else
return y;
}
void out()
{
cout<<num<<' '<<name<<endl;
}
};
int main()
{
string num,name;
int n,s,m,i;
while(cin>>m)
{
stu a[100],b;
for(i=0;i<m;i++)
{
cin>>num>>name>>n>>s;
a[i].show(num,name,n,s);
}
for(i=0;i<m-1;i++)
{b=fun(a[i],a[i+1]);
a[i+1]=b;
}
b.out();
}
return 0;
}
7-1001:
#include<iostream>
using namespace std;
class point
{
private:
double x,y;
public:
point(double x1,double y1)
{
x=x1;y=y1;
}
void set_point(double a,double b)
{
x=a;y=b;
}
void show()
{
cout<<"("<<x<<','<<y<<")"<<endl;
}
};
class circle:public point
{
private:
double r;
public:
circle(double x,double y,double r1):point(x,y) {
r=r1;
}
void out()
{
cout<<r*r*3.14<<endl;
}
};
int main()
{
double x,y,r,t;
cin>>t;
while(t--)
{
cin>>x>>y>>r;
circle a(x,y,r);
cout.precision(2);
cout.setf(ios::fixed);
a.out();
}
return 0;
}
7-1002:
#include<iostream>
#include<string>
using namespace std;
class asd
{
private:
int x,y,z;
public:
asd(int xx=0,int yy=0,int zz=0)
{x=xx;y=yy;z=zz;}
friend asd operator+(asd &a,asd &b)
{
asd t;
t.x=a.x+b.x;t.y=a.y+b.y;t.z=a.z+b.z;
return t;
}
friend asd operator-(asd &a,asd &b)
{
asd t;
t.x=a.x-b.x;t.y=a.y-b.y;t.z=a.z-b.z;
return t;
}
void put()
{cout<<"("<<x<<","<<y<<","<<z<<")"<<endl;} };
void main()
{
int x,y,z,x1,y1,z1;
string a,b,c;
while(cin>>a>>b>>c)
{
if(b=="+")
{
if(c=="x")
{
cin>>x>>y>>z>>x1;
asd a1(x,y,z),a2(x1,0,0),a3;
a3=a1+a2;
a3.put();
}
if(c=="y")
{
cin>>x>>y>>z>>y1;
asd a1(x,y,z),a2(0,y1,0),a3;
a3=a1+a2;
a3.put();
}
if(c=="z")
{
cin>>x>>y>>z>>z1;
asd a1(x,y,z),a2(0,0,z1),a3;
a3=a1+a2;
a3.put();
}
if(c=="xyz")
{
cin>>x>>y>>z>>x1>>y1>>z1;
asd a1(x,y,z),a2(x1,y1,z1),a3;
a3=a1+a2;
a3.put();
}
}
else if(b=="-")
{
if(c=="x")
{
cin>>x>>y>>z>>x1;
asd a1(x,y,z),a2(x1,0,0),a3;
a3=a1-a2;
a3.put();
}
if(c=="y")
{
cin>>x>>y>>z>>y1;
asd a1(x,y,z),a2(0,y1,0),a3;
a3=a1-a2;
a3.put();
}
if(c=="z")
{
cin>>x>>y>>z>>z1;
asd a1(x,y,z),a2(0,0,z1),a3;
a3=a1-a2;
a3.put();
}
if(c=="xyz")
{
cin>>x>>y>>z>>x1>>y1>>z1;
asd a1(x,y,z),a2(x1,y1,z1),a3;
a3=a1-a2;
a3.put();
}
}
}
}
7-1003:
#include<iostream>
using namespace std;
class date
{
private:
int year,month,da;
public:
date(int y,int m,int d)
{
year=y;month=m;da=d;
}
void out()
{
cout<<month<<"-"<<da<<"-"<<year<<endl;
}
date operator++(int)
{
da++;
if((month==1||month==3||month==5||month==7| |month==8||month==10)&&da>31)
{
month++;da=1;
}
else if(month==12&&da>31)
{
year++;month=1;da=1;
}
else if(month==2&&da>28)
{
month++;da=1;
}
else if(da>30)
{
month++;da=1;
}
return *this;
}
};
int main()
{
int year,month,da;
while(cin>>month>>da>>year)
{
date s(year,month,da);
s.out();
s++;
s.out();
}
return 0;
}
8-1001:
#include<iostream>
using namespace std;
template<typename T>
T fun(T *p,int n)
{
int i;
T max;
max=p[0];
for(i=0;i<n;i++)
if(max<p[i])
max=p[i];。

相关文档
最新文档