C++数组练习题(二)

我是计算机专业的一位大一本科生,C++小白,下面是我们学校在学习C++时用的基础练习题,我感觉这些题比较适合初学C++的码友,所以利用空闲时间将这些题整理出来,一是为了让初学C++的码友有所参考,二也是为了复习一下所学过知识。(因准备急促,如有代码错误,还请各位码友指正。)

1.矩阵各行、列求和

输入m行n列(假设m,n不超过50。程序运行时输入)的矩阵存储在一个二维整型数组中,分别求出各行元素之和, 各列元素和。提示:各行元素和、各列元素和需要分别使用一个一维数组row、col实现存储, 再输出row、col中的值。
Sample Input
3 4
1 2 3 4
5 6 7 8
9 10 11 12
Sample Output
row:10 26 42
col:15 18 21 24

#include<iostream>
using namespace std;
const int Max = 50;
int main()
{
    
    
	int a[Max][Max];//定义一个二维数组
	int row[Max] = {
    
     0 }, col[Max] = {
    
     0 };//定义两个一维数组,并初始化
	int m, n, i, j;
	cin >> m >> n;
	for (i = 0; i < m; i++)
	{
    
    
		for (j = 0; j < n; j++)
		{
    
    
			cin >> a[i][j];//向二维数组中存入数据
		}
	}
	for (i = 0; i < m; i++)
	{
    
    
		for (j = 0; j < n; j++)
		{
    
    
			row[i] += a[i][j];//计算每行元素之和,并存入数组row中
		}
	}
	for (j = 0; j < n; j++)
	{
    
    
		for (i = 0; i < m; i++)
		{
    
    
			col[j] += a[i][j];//计算每列元素之和,并存入数组col中
		}
	}
	cout << "row:";
	for (i = 0; i < m; i++)
		cout << row[i] << " ";
	cout << endl;
	cout << "col:";
	for (j = 0; j < n; j++)
		cout << col[j] << " ";
	cout << endl;
	return 0;
}

2.对角线元素之和

输入一个n*n(假设n值不超过50)的矩阵存储于二维数组,求出两条对角线元素值之和。如果对角线有重叠数据(n为奇数时),不重复相加。
Sample Input
5
11 12 13 14 40
55 56 57 58 41
19 10 11 12 42
31 32 33 34 43
51 52 53 54 55
Sample Output
348

#include<iostream>
using namespace std;
const int Max = 50;
int main()
{
    
    
	int a[Max][Max];//定义一个二维数组
	int n, i, j, s = 0;
	cin >> n;
	for (i = 0; i < n; i++)//向二维数组中存入数据
	{
    
    
		for (j = 0; j < n; j++)
		{
    
    
			cin >> a[i][j];
		}
	}
	for (i = 0; i < n; i++)//遍历数组
	{
    
    
		for (j = 0; j < n; j++)
		{
    
    
			if (i == j || i + j == n - 1)//判断该元素是否在对角线上,如果在求和
				s += a[i][j];
		}
	}
	cout << s << endl;
	return 0;
}

3.矩阵周边元素

输入一个mn的矩阵(整型),输出矩阵周边元素之和。输入格式:先输入m,n的值(假设m,n值不大于50),再输入mn个数据。
Sample Input
5 5
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25
Sample Output
208

#include<iostream>
using namespace std;
const int Max = 50;
int main()
{
    
    
	int a[Max][Max];//定义一个二维数组
	int i, j,m,n,s=0;
	cin >> m >> n;
	for (i = 0; i < m; i++)//向二维数组中存入数据
	{
    
    
		for (j = 0; j < n; j++)
		{
    
    
			cin >> a[i][j];
		}
	}
	for (i = 0; i < m; i++)//遍历数组
	{
    
    
		for (j = 0; j < n; j++)
		{
    
    
			if (i == 0 || i == m - 1 || j == 0||j==n-1)//判断该元素是否为矩阵周边元素,如果是求和
				s += a[i][j];
		}
	}
	cout <<s<<endl;
	return 0;
}

4.奇偶排序

从键盘输入n(n<100)个整数(以0结束),存放在一个一维数组中,将它们按奇数在前、偶数在后,同为奇数或偶数的按从小到大的顺序排序,并输出排序后的结果。
其中:排序过程用函数实现。
Sample Input
10 2 7 9 11 5 4 3 6 8 20 0
Sample Output
3 5 7 9 11 2 4 6 8 10 20

#include<iostream>
using namespace std;
const int Max = 100;
void Sort(int a[], int n);//对排序函数进行声明
int main()
{
    
    
	int a[Max], n = 0, i, j;
	cin >> a[n];
	while (a[n])//向数组中输入元素,以0结束
	{
    
    
		n++;
		cin >> a[n];
	}
	Sort(a,n);//调用排序函数
	for (i = 0; i < n; i++)//遍历数组
		cout << a[i] << " ";//输出所有元素
	cout << endl;
	return 0;
}
void Sort(int a[], int n)//排序函数
{
    
    
	int i, j;
	for (i = 0; i < n - 1; i++)//冒泡排序
	{
    
    
		for (j = 0; j < n - 1 - i; j++)
		{
    
    
			if ((a[j] % 2 < a[j + 1] % 2) || ((a[j] % 2 == a[j + 1] % 2) && (a[j] > a[j + 1])))//元素交换的条件有两个:1.当前一个元素为偶数,后一个元素为奇数时;2.前后两个元素同为奇数或偶数,并且前一个元素比后于一个元素大时
			{
    
    
				int temp = a[j];
				a[j] = a[j + 1];
				a[j + 1] = temp;
			}
		}
	}
}

5.逆序求和

从键盘输入一组整数(以0结束),计算这组整数逆序后之和。
其中将一个整数逆序用函数实现,整数123逆序为321、整数-345逆序为-543。
Sample Input
234 894 -94 736 0
Sample Output
1518

#include<iostream>
using namespace std;
const int Max = 100;
int f(int n);//对逆序函数进行声明
int main()
{
    
    
	int a[Max], n = 0, i,s=0;
	cin >> a[n];
	while (a[n])//向数组中输入元素,以0结束
	{
    
    
		n++;
		cin >> a[n];
	}
	for (i = 0; i < n; i++)
		s += f(a[i]);//调用逆序函数
	cout <<s<< endl;
	return 0;
}
int f(int n)//逆序函数
{
    
    
	int t = 1, s = 0;
	if (n < 0)
	{
    
    
		t = -1;
		n = -n;
	}
	while (n)
	{
    
    
		s = s * 10 + n % 10;
		n = n / 10;
	}
	return s* t;
}

6.数字移动

编写move()函数,实现将若干个整数顺序向后移动3位,最后3位变成最前面3位。从键盘输入n个整数(3<=n<=10),调用move函数实现移动,输出移动后的结果。
输入格式:第一个整数为个数n,后续为n个整数。
Sample Input
5 1 2 3 4 5
Sample Output
3 4 5 1 2

#include<iostream>
using namespace std;
const int Max = 10;
void move(int a[], int n);//对move函数声明
int main()
{
    
    
	int a[Max],i, n;
	cin >> n;
	for (i = 0; i < n; i++)//向数组中输入元素
		cin >> a[i];
	move(a, n);//调用move函数
	for (i = 0; i < n; i++)//遍历数组,输出所有元素
		cout << a[i] << " ";
	cout << endl;
	return 0;
}
void move(int a[], int n)
{
    
    
	int a1, a2, a3, i;
	a1 = a[n - 3];
	a2 = a[n - 2];
	a3 = a[n - 1];
	for (i = n - 4; i >= 0; i--)//倒数第四个之前每个元素向后移动三个位置
		a[i + 3] = a[i];
	a[0] = a1;
	a[1] = a2;
	a[2] = a3;
}

7.查找元素

输入n(n<100)个整数(以0结束),存放在一维数组中,再输入一个整数x,
在数组中查找指定元素x。若数组中存在x,则输出x在数组第一次出现的下标,若数组中不存在x,则输出-1。要求在数组中查找x的过程用一个函数实现。
Sample Input
1 2 3 4 5 6 7 8 9 10 0 8
Sample Output
7

#include<iostream>
using namespace std;
const int Max = 100;
int find(int a[], int n,int x);//对查找函数声明
int main()
{
    
    
	int a[Max], n = 0,x;
	cin >> a[n];
	while (a[n])//向数组中存入数据,以0结束
	{
    
    
		n++;
		cin >> a[n];
	}
	cin >> x;
	cout << find(a, n, x) << endl;//调用查找函数
	return 0;
}
int find(int a[], int n, int x)//查找函数
{
    
    
	int i;
	for (i = 0; i < n; i++)
	{
    
    
		if (a[i] == x)
			break;
	}
	if (i < n)
		return i;
	else
		return -1;
}

8.平均值

输入n(n<100)个整数(以0结束),存放在一维数组中,计算n个数的平均值。
要求:计算平均值利用自定义函数实现,主函数中实现n个数据的输入,函数调用及平均值输出。
Sample Input
1 3 5 9 8 7 0
Sample Output
5.5

#include<iostream>
using namespace std;
const int Max = 100;
double f(int a[], int n);//对求平均值函数声明
int main()
{
    
    
	int a[Max], n = 0;
	cin >> a[n];
	while (a[n])//向数组中存入数据,以0结束
	{
    
    
		n++;
		cin >> a[n];
	}
	cout << f(a, n) << endl;//调用求平均值函数
	return 0;
}
double f(int a[], int n)//求平均值函数
{
    
    
	int i;
	double s = 0;
	for (i = 0; i < n; i++)
		s += a[i];
	return s / n;
}

9.有序插入

从键盘输入n个整数(n<100),存放在一个一维数组中,将它们从大到小排序,再从键盘输入一个整数x,将该数插入到该数组中,使得插入后依然为降序,输出插入后数组中的所有元素。
其中将数组排序和将整数x插入到数组中使其依然有序分别用函数完成。
输入格式:第一个为个数n,后续为n个整数,再输入一个整数x。
Sample Input
10 7 6 5 4 3 2 20 11 9 8
12
Sample Output
20 12 11 9 8 7 6 5 4 3 2

#include<iostream>
using namespace std;
const int Max = 100;
void Sort(int a[], int n);//对排序函数声明
void f(int a[], int& n, int x);//对插入函数声明
int main()
{
    
    
	int a[Max],n, i,x;
	cin >> n;
	for (i = 0; i < n; i++)//向数组中存入数据
		cin >> a[i];
	Sort(a, n);//调用排序函数
	cin >> x;
	f(a, n, x);//调用插入函数
	for (i = 0; i < n; i++)
		cout << a[i] << " ";
	cout << endl;
	return 0;
}
void Sort(int a[], int n)//排序函数
{
    
    
	int j, i;
	for (i = 0; i < n - 1; i++)
	{
    
    
		for (j = 0; j < n - 1 - i; j++)
		{
    
    
			if (a[j] < a[j + 1])
			{
    
    
				int temp = a[j];
				a[j] = a[j + 1];
				a[j + 1] = temp;
			}
		}
	}
}
void f(int a[], int& n, int x)//插入函数
{
    
    
	int pos,i;
	for (i = 0; i < n; i++)
	{
    
    
		if (a[i] < x)
		{
    
    
			pos = i;
			break;
		}
	}
	for (i = n - 1; i >= pos; i--)
		a[i + 1] = a[i];
	a[pos] = x;
	n++;
}

以上的前3道题均为二维数组的基础题;后6道均为数组与函数结合的基础题。

扫描二维码关注公众号,回复: 12457669 查看本文章

猜你喜欢

转载自blog.csdn.net/Archie20/article/details/112394895