第五章
1
题目
输入十个不相等的正整数,求其平均值,并求最接近平均值的那个整数,如果有两个,则两个都输出(20分)
题目内容:
输入十个不相等的正整数,求其平均值,并求最接近平均值的那个整数,如果有两个,则两个都输出
输入格式:10个正整数
输出格式:最接近平均值的所有正整数,数与数之间用回车隔开
输入样例:
1 2 3 4 5 6 7 8 9 10
输出样例:
5
6
参考答案
#include <iostream>
#include <cmath>
using namespace std;
int main(){
int nums[10];
float sum = 0;
for (int i = 0; i < 10; i++){
cin >> nums[i];
sum += nums[i];
}
float minDif = fabs(sum / 10 - nums[0]);
for (int i = 1; i < 10; i++){
if (fabs(sum / 10 - nums[i]) < minDif)
minDif = fabs(sum / 10 - nums[i]);
}
for (int i = 0; i < 10; i++){
if (fabs(sum / 10 - nums[i]) == minDif)
cout << nums[i] << endl;
}
}
2
题目
输入一个5*5数组元素的值,并输入一个整数m(m<10 m>1)(10分)
题目内容:
输入一个5*5数组元素的值,并输入一个整数m(m<10),将二维数组的右上半角元素的值乘以m,然后输出相乘后的二维数组元素。每个数组元素占5个字符宽度,并且右对齐,每输出一行后换行。
输入格式:先按行输入二维数组元素,然后输入整数m
输出格式:输出相乘后的数组元素,每个数组元素占5个字符宽度,并且右对齐,每输出一行后换行
输入样例:
1 2 3 4 5
2 3 4 5 6
3 4 5 6 7
4 5 6 7 8
5 6 7 8 9
3
输出样例:
3 6 9 12 15 2 9 12 15 18 3 4 15 18 21 4 5 6 21 24 5 6 7 8 27
参考答案
#include <iostream>
#include <iomanip>
using namespace std;
int main(){
int nums[5][5], n;
for (int i = 0; i < 5; i++){
for (int j = 0; j < 5; j++)
cin >> nums[i][j];
}
cin >> n;
for (int i = 0; i < 5; i++){
for (int j = 0; j < 5; j++){
if (j >= i)
nums[i][j] *= n;
cout << setw(5) << nums[i][j];
}
cout << endl;
}
}
3
题目
输入6个不等长字符串,将这6个字符串按照字符串长度排序,并按从长到短的顺序输出字符串。(20分)
题目内容:输入6个不等长字符串,将这6个字符串按照字符串长度排序,并按从长到短的顺序输出字符串。
输入格式:输入六个长度不超过45的不等长字符串
输出格式:输出按长度排序后的字符串,字符串之间用回车隔开
输入样例:
美国
巴基斯坦
乌兹别克斯坦
哈萨克斯坦
俄罗斯
中华人民共和国
输出样例:
中华人民共和国
乌兹别克斯坦
哈萨克斯坦
巴基斯坦
俄罗斯
美国
参考答案
#include <iostream>
#include <cstring>
using namespace std;
int main(){
char strs[6][45];
for (int i = 0; i < 6; i++){
cin >> strs[i];
}
int rank[6] = {0, 1, 2, 3, 4, 5};
for (int i = 1; i < 6; i++){
for (int j = i; strlen(strs[rank[j]]) > strlen(strs[rank[j - 1]]); j--){
int t = rank[j - 1];
rank[j - 1] = rank[j];
rank[j] = t;
}
}
for (int i = 0; i < 6; i++)
cout << strs[rank[i]] << endl;
}
第六章
1
题目
图书 ISBN 号码中识别码的计算(35分)
题目内容:
给出一个不包含识别码的 ISBN 号码(前 11 位),计算其识别码,并输出完整的 ISBN 号码。
每一本正式出版的图书都有一个 ISBN 号码与之对应,ISBN 码包括 9 位数字、1 位识别码和 3 位分隔符,其规定格式如:“x-xxx-xxxxx-x”,其中符号“-”就是分隔符(键盘上的减号),最后一位是识别码,例如“0-670-82162-4”就是一个标准的 ISBN 码。
ISBN 码的首位数字表示书籍的出版语言,例如,符号“0”代表英语;第一个分隔符“-”之后的三位数字代表出版社,第二个分隔符后的五位数字“82162”代表该书在该出版社的编号;最后一位识别码的计算方法:
1、第 1 位数字乘以 1,加上第 2 位数字乘以 2,再加上第 3 位数字乘以 3,……,以此类推;
2、再把所得结果对 11 求余,所得的余数即为识别码,如果余数为 10,则识别码为大写字母 X。
代码:(编写函数ISBN,完善以下代码)#include <iostream> #include <cstring> using namespace std; int main() { char charISBN11[12], *charISBN; cin>>charISBN11; charISBN=ISBN(charISBN11); cout<<charISBN<<endl; delete charISBN; return 0; }
输入格式:图书的 ISBN 号码(前 11 位)
输出格式:完整的图书的 ISBN 号码(13 位)
输入样例:
0-670-82162
输出样例:
0-670-82162-4
参考答案
#include <iostream>
using namespace std;
char isbn[14];
char *ISBN(char *isbn11){
//char *isbn = new char[14];
for (int i = 0; i < 11; i++){
isbn[i] = isbn11[i];
}
isbn[11] = '-';
int iden = 0;
for (int i = 0, j = 1; i < 11; i++){
if (isbn[i] >= '0' && isbn[i] <= '9'){
iden += (int)(isbn[i] - '0') * j;
j++;
}
}
iden = iden % 11;
if (iden < 10)
isbn[12] = iden + '0';
else isbn[12] = 'X';
return isbn;
}
int main()
{
char charISBN11[14], *charISBN;
cin >> charISBN11;
charISBN = ISBN(charISBN11);
cout << charISBN << endl;
//delete charISBN;
return 0;
}
注1:
为何将isbn的长度定义为14而不是13?原因是C风格字符串必须以终止符即’\0’结尾,如果长度只有13则无法储存’\0’,输出时会出现未定义的结果。
注2:
在多数编译环境,一个语句块结束时,在该语句块中定义的变量会被自动删除,故而如果将char isbn[14];定义在ISBN()函数中,返回isbn首元素的指针(return isbn;)后isbn会被删除,从而无法访问在ISBN函数中对isbn字符串修改出的值(在visual studio中会表现为经典的“烫烫烫烫烫”),因此此代码将isbn[14]在函数外定义。但这种写法需将题目中的 delete charISBN; 删去,因为delete只能删除由new创建的内存。
另有写法:
char *isbn = new char[14];
这种写法写在ISBN函数中可正常运行,且无需删除题目中的 delete charISBN; ,因为程序不会主动删除由new提供的内存。更提倡这种写法。
2
题目
最长单词(35分)
题目内容:
求字符串单词最长的一个单词(如果有多个最长单词,以最左边的为准),并输出该单词。
代码:(编写函数fun,完善以下代码)#include <iostream> using namespace std; int main() { char c1[100]; char c2[100]; cin.getline(c1,100); fun(c1,c2); cout<<c2<<endl; return 0; }
输入格式:
一条英文句子
输出格式:
英文句子中的最长单词
输入样例:
A Grain of Sand William Blake
输出样例:
William
#include <iostream>
using namespace std;
void fun(char *sentence, char *word){
int count = 0, tcount = 0;
int place = 0;
for (int i = 0; i == 0 || sentence[i - 1] != 0; i++){
if (sentence[i] >= 'A' && sentence[i] <= 'z')
tcount++;
else {
if (tcount > count){
place = i - tcount;
count = tcount;
}
tcount = 0;
}
}
for (int i = 0; i < count; i++)
word[i] = sentence[place + i];
word[count] = 0;
}
int main()
{
char c1[100];
char c2[100];
cin.getline(c1, 100);
fun(c1, c2);
cout << c2 << endl;
return 0;
}
3
题目
数据的有序插入(30分)
题目内容:
插入一个数据到一个有序的一维数组中,要求插入后,该数组任然保持原有顺序。
代码:#include <iostream> using namespace std; int main() { int data[11]={12,34,56,78,90,167,258,389,945,1890}; int num; cin>>num; FunSort(data,10,num); int I; cout<<data[0]; for(i=1;i<11;i++) cout<<" "<<data[i]; cout<<endl; return 0; }
输入格式:一个整数
输出格式:数据插入完成后,输出数组所保存的所有数据,数据之间用一个空格分隔。
输入样例:
57
输出样例:
12 34 56 57 78 90 167 258 389 945 1890
参考答案
#include <iostream>
using namespace std;
void FunSort(int array[], int count, int num){
if (num > array[count - 1]){
array[count] = num;
return;
}
else
array[count] = array[count - 1];
int temp = num;
for (int i = 0; i < count + 1; i++){
if (num <= array[i]){
temp = array[i + 1];
array[i + 1] = array[i];
array[i] = num;
num = temp;
}
}
}
int main()
{
int data[11] = {12,34,56,78,90,167,258,389,945,1890};
int num;
cin >> num;
FunSort(data, 10, num);
int i;
cout << data[0];
for(i = 1; i < 11; i++)
cout << " " << data[i];
cout << endl;
return 0;
}
第七章
1
题目
题目内容:定义一个球类,数据成员为球半径(double)
设计计算球的面积和体积的成员函数,写出主函数测试编写的类。
求球面积公式:S=4*π*r2
求球体积公式:V=4/3*π*r3
其中π是圆周率,
请注意这里给出的是数学表达式,不是c++表达式
请补充完整类的定义#include <iostream> using namespace std; const double PI=3.14159; void Sphere::show() { cout<<"半径为"<<r<<"的球表面积为"<<getS()<<endl; cout<<"半径为"<<r<<"的球体积为"<<getV()<<endl; } int main() { double r1,r2; cin>>r1>>r2; Sphere q1(r1); q1.show(); q2.show(); q2.setR(r2); q2.show(); return 0; }
输入格式:两个球的球半径
输出格式:按show函数格式输出
输入样例:
5 6
输出样例:
半径为5的球表面积为314.159
半径为5的球体积为523.598
半径为0的球表面积为0
半径为0的球体积为0
半径为6的球表面积为452.389
半径为6的球体积为904.778
参考答案
#include <iostream>
using namespace std;
const double PI = 3.14159;
class Sphere {
public:
double r = 0;
Sphere() = default;
Sphere(double input){
this->r = input;
}
void show();
double getS(){
return 4 * PI * r * r;
}
double getV(){
return 4 * PI * r * r * r / 3;
}
void setR(double input){
r = input;
}
};
void Sphere::show()
{
cout << "半径为" << r << "的球表面积为" << getS() << endl;
cout << "半径为" << r << "的球体积为" << getV() << endl;
}
int main()
{
double r1, r2;
cin >> r1 >> r2;
Sphere q1(r1), q2;
q1.show();
q2.show();
q2.setR(r2);
q2.show();
return 0;
}
2
题目
定义一个日期类(25分)
题目内容:
定义一个日期类Date,包含年、月、日三个数据成员(int),定义带有3个参数的构造函数,
以及一个求日期是当年的第几天的成员函数和输出日期的成员函数,
日期的显示格式为年/月/日。编写主函数进行测试。
(每年各月天数分别为31,28,31,30,31,30,31,31,30,31,30,31,闰年2月为29天,
闰年的条件year%4==0&&year % 100!=0)||year%400==0))
请补充完整类的定义#include <iostream> using namespace std; void Date::show() { cout<<year<<"/"<<month<<"/"<<day<<endl; } int main() { int y,m,d; cin>>y>>m>>d; Date d1(y,m,d),d2; d1.show(); cout<<d1.getDay()<<endl; cin>>y>>m>>d; d2.SetD(y,m,d); d2.show(); cout<<d2.getDay()<<endl; system("pause"); return 0; }
输入格式:年 月 日
输出格式:见输出样例
输入样例:
2020 4 8
1997 7 1
输出样例:
2020/4/8
99
1997/7/1
182
#include <iostream>
using namespace std;
class Date {
private:
int year = 1900, month = 1, day = 1;
public:
Date() = default;
Date(int y, int m, int d){
year = y;
month = m;
day = d;
}
void SetD(int y, int m, int d){
year = y;
month = m;
day = d;
}
void show();
int getDay(){
int days = 0;
for (int m = 1; m < month; m++){
if (m > 7)
m++;
if (m == 2 && ((year % 4 == 0 && year % 100!=0) || year % 400 == 0))
days += 29;
else if (m == 2)
days += 28;
else if (m % 2)
days += 31;
else days += 30;
}
days += day;
return days;
}
};
void Date::show()
{
cout << year << "/" << month << "/" << day << endl;
}
int main()
{
int y,m,d;
cin >> y >> m >> d;
Date d1(y,m,d), d2;
d1.show();
cout << d1.getDay() << endl;
cin >> y >> m >> d;
d2.SetD(y, m, d);
d2.show();
cout << d2.getDay() << endl;
system("pause");
return 0;
}
3
题目
定义一个复数类(25分)
题目内容:
定义复数类Complex,并使用成员函数add实现复数加法,并输出结果。注意虚部为负数时,要写成3-4i,不能写成3±4i,如果实部或者虚部为0,则可以不输出,例如:4i不能输出为0+4i,而是直接输出4i,4不能输出为4+0i,而是直接输出4。
请定义Complex类。#include<iostream> using namespace std; int main() { int r1,r2,i1,i2; cin>>r1>>i1>>r2>>i2; Complex a(r1,i1),b(r2,i2),c; c=a.add(b);//c=a+b c.show(); return 0; }
输入格式:实部和虚部
输出格式:见输出样例
输入样例:
3 4 5 6
输出样例:
8+10i
参考答案
#include <iostream>
using namespace std;
class Complex {
public:
int r = 0, i = 1;
Complex() = default;
Complex(int rin, int iin){
r = rin;
i = iin;
}
Complex add(Complex b){ //题目要求的写法
Complex c;
c.r = r + b.r;
c.i = i + b.i;
return c;
}
void show(){
if (r && i){
cout << r ;
if (i > 0)
cout << '+';
cout << i << 'i' << endl;
}
else if (r && !i)
cout << r << endl;
else if (!r && i)
cout << i << 'i';
}
};
Complex operator+(Complex a, Complex b){ //重载运算符
Complex c;
c.r = a.r + b.r;
c.i = a.i + b.i;
return c;
}
int main()
{
int r1,r2,i1,i2;
cin >> r1 >> i1 >> r2 >> i2;
Complex a(r1, i1), b(r2, i2), c;
c = a.add(b);//c = a + b;
c.show();
return 0;
}
4
题目
元素类和集合类(30分)
题目内容:下面有元素类Element和集合类Set的定义,请看代码及代码后面的注释将类的定义补充完整,其中,集合中不能有重复的元素。#include<iostream> #include<cmath> using namespace std; const int MaxElement=100; class Element{ //“元素”类 public: int n; Element(int i=0):n(i){} }; class MySet{ //“集合”类 Element *element; //指向存储元素的数组的指针 int size; //数组大小 int counter; //数组中元素的个数 int current; //用于表示扫描位置,及当前被扫描元素在数组中的下标 public: MySet():element(new Element[100]), size(100), counter(0), current(0){} ~MySet(){ delete[]element; } void add(Element ele); //向集合中添加一个元素,保持由小到大的顺序。 void remove(Element ele); //删除集合中指定的元素 void scanFirst(){ current=0; } //将扫描位置定位于首元素 void scanNext(){ ++current; } //将扫描位置定位于下一个元素 Element get()const{ return element[current]; } //返回当前被扫描的元素 bool isEnded()const{ return current >= counter; } //如果已经没有更多的元素可扫描了,返回true。 void show(); //显示集合中所有元素 }; void MySet::show(){ scanFirst(); //扫描位置定位于首元素 while(!isEnded()){ cout<<get().n<<" "; scanNext(); //扫描定位于下一个元素 } cout<<endl; } int main(){ int a[7],i; for(i=0;i<7;i++) cin>>a[i]; MySet set; i=0; set.add(Element(a[i++])); set.add(Element(a[i++])); set.add(Element(a[i++])); set.show(); set.remove(Element(a[i++])); set.remove(Element(a[i++])); set.add(Element(a[i++])); set.show(); system("pause"); return 0; }
输入格式:数组a的元素
输出格式:见输出样例
输入样例:
3 8 5 0 5 4 8
输出样例:
0 3 5 8
0 3 8
参考答案
#include<iostream>
#include<cmath>
using namespace std;
const int MaxElement = 100;
class Element{ //“元素”类
public:
int n;
Element(int i = 0):n(i){}
};
class MySet{ //“集合”类
Element *element; //指向存储元素的数组的指针
int size; //数组大小
int counter; //数组中元素的个数
int current; //用于表示扫描位置,及当前被扫描元素在数组中的下标
public:
MySet():element(new Element[100]), size(100), counter(0), current(0){}
~MySet(){ delete[]element; }
void add(Element ele); //向集合中添加一个元素,保持由小到大的顺序。
void remove(Element ele); //删除集合中指定的元素
void scanFirst(){ current=0; } //将扫描位置定位于首元素
void scanNext(){ ++current; } //将扫描位置定位于下一个元素
Element get()const{ return element[current]; } //返回当前被扫描的元素
bool isEnded()const{ return current >= counter; } //如果已经没有更多的元素可扫描了,返回true。
void show(); //显示集合中所有元素
};
void MySet::add(Element ele){
for (int i = 0; i <= counter; i++){
if (i == counter){
element[i].n = ele.n;
counter++;
break;
} else if (element[i].n == ele.n)
break;
else if (element[i].n > ele.n){
for (int j = counter; j >= i; j--)
element[j].n = element[j - 1].n;
element[i].n = ele.n;
counter++;
break;
}
}
}
void MySet::remove(Element ele){
for (int i = 0; i < counter; i++){
if (ele.n == element[i].n){
for (int j = i; j < counter; j++)
element[j].n = element[j + 1].n;
counter--;
break;
}
}
}
void MySet::show(){
scanFirst(); //扫描位置定位于首元素
while(!isEnded()){
cout<<get().n<<" ";
scanNext(); //扫描定位于下一个元素
}
cout<<endl;
}
int main(){
int a[7], i;
for(i = 0; i < 7; i++)
cin>>a[i];
MySet set;
i = 0;
set.add(Element(a[i++]));
set.add(Element(a[i++]));
set.add(Element(a[i++]));
set.add(Element(a[i++]));
set.show();
set.remove(Element(a[i++]));
set.remove(Element(a[i++]));
set.add(Element(a[i++]));
set.show();
system("pause");
return 0;
}
Created by goolwind on 2021/1/5
1494





