C++数组练习题(一)

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

1.逆序输出

从键盘输入n个整数(n<100),存放在一个一维数组中,逆序输出能被3整除 的元素,并逆序输出数组下标为3的倍数的元素。
输入格式:第一个整数为个数n,后续为n个整数
输出格式:第一行能被3整除的元素,第二行为下标为3的倍数的元素,各个数值之间用空格分隔。
Sample Input
10 2 7 9 10 5 4 3 6 8 20
Sample Output
6 3 9
20 3 10 2

#include<iostream>
using namespace std;
const int Max = 100;
int main()
{
    
    
	int a[Max], n, i;
	cin >> n;
	for (i = 0; i < n; i++)//向数组里存入数据
		cin >> a[i];
	for (i = n - 1; i >= 0; i--)//逆序输出符合条件元素
		if (a[i] % 3 == 0)//判断元素能否被3整除
			cout << a[i] << " ";
	cout << endl;
	for (i = n - 1; i >= 0; i--)//逆序输出符合条件元素
		if (i % 3 == 0)//判断元素下标是否为3的倍数
			cout << a[i] << " ";
	cout << endl;
	return 0;
}

2.逆序存储

从键盘输入n(n<100)个整数,存放在一个一维数组a中,将它们逆序存放在另一个整型数组b中,并按b数组中下标从小到大的顺序输出下标为3的倍数的数组元素。
输入格式:第一个数为数组中元素个数n,之后为n个元素。
输出格式:下标为3的倍数的元素,各个数值之间用空格分隔。
Sample Input
10 2 7 9 10 5 4 3 6 8 20
Sample Output
20 3 10 2

#include<iostream>
using namespace std;
const int Max = 100;
int main()
{
    
    
	int a[Max], b[Max], n, i;
	cin >> n;
	for (i = 0; i < n; i++)
	{
    
    
		cin >> a[i];//向数组里存入数据
		b[n - 1 - i] = a[i];//将数组a中元素逆序存入数组b中
	}
	for (i = 0; i < n; i++)
		if (i % 3 == 0)//判断元素下标是否为3的倍数
			cout << b[i] << " ";
	cout << endl;
	return 0;
}

3.平均值

从键盘输入任意个整数(以0结束,假设不超过100个),存放在一个一维数组中,计算这组数的平均值(实型)。
Sample Input
15 2 7 9 10 5 4 3 6 8 20 0
Sample Output
8.09091

#include<iostream>
using namespace std;
const int Max = 100;
int main()
{
    
    
	int a[Max], i=0;
	double s = 0;
	cin >> a[i];//向数组中存入数据
	while (a[i])//输入一个数以0结束
	{
    
    
		s += a[i];//求所有元素的和
		i++;
		cin >> a[i];
	}
	cout << s / i << endl;
	return 0;
}

4.最大值

从键盘输入任意个整数(以0结束,个数不超过100),存放在一个一维数组中,输出这组数的最大值。
Sample Input
10 2 7 9 11 5 4 3 6 8 20 0
Sample Output
20

#include<iostream>
using namespace std;
const int Max = 100;
int main()
{
    
    
	int a[Max], n=0,max,i;
	cin >> a[n];//向数组中存入数据
	while (a[n])//输入一个数以0结束
	{
    
    
		n++;
		cin >> a[n];
	}
	max = 0;//假设最大值下标为0
	for (i = 1; i < n; i++)
		if (a[i] > a[max])//当有元素比当前最大元素大时,就将此元素下标赋值给max
			max = i;
	cout << a[max] << endl;//输出max下标下的元素,即最大值
	return 0;
}

本题使用下标方式来表示数组中元素最大值的方法,这种方法可以解决同类型的问题。(例如:求最大值所在的位置,最小值,最小值所在位置)

5.斐波那契数列

输入一个正整数n(n<100),将斐波那契数列的前n项保存到一维数组中,并输出数组中对3取余为2的元素。
斐波那契数列为:0 1 1 2 3 5 8 13 21 34 55……,起始项为第0项。
(斐波那契数列:从第三项开始,每一项等于前两项的和。)
Sample Input
10
Sample Output
2 5 8

#include<iostream>
using namespace std;
const int Max = 100;
int main()
{
    
    
	int a[Max], n, i;
	a[0] = 0; //给前三个元素赋值
	a[1] = a[2] = 1; 
	cin >> n;
	for (i = 3; i < n; i++)
		a[i] = a[i - 1] + a[i - 2];//从第三项开始,每一项等于前两项的和
	for (i = 0; i < n; i++)//遍历数组
		if (a[i] % 3 == 2)
			cout << a[i] << " ";//输出符合条件的元素
	cout << endl;
	return 0;
}

6.指定位置插入

从键盘输入任意个整数(以0结束,假设个数不超过100个),从0下标开始依次存放在一维数组中,再输入插入位置pos(0<=pos<=n)及被插入的元素x,将x插入到数组的pos下标处pos。输出插入后数组中的所有元素。
Sample Input
8 2 17 9 11 5 4 3 6 21 20 0
8 100
Sample Output
8 2 17 9 11 5 4 3 100 6 21 20

#include<iostream>
using namespace std;
const int Max = 100;
int main()
{
    
    
	int a[Max],n=0,i,pos,x;
	cin >> a[n];
	while (a[n])//向数组中输入元素,以0结束
	{
    
    
		n++;
		cin >> a[n];
	}
	cin >> pos >> x;
	for (i = n - 1; i >= pos; i--)//从最后一个元素开始,每个元素向后移动一个位置,直到被插入位置的元素向后移动后
	{
    
    
		a[i + 1] = a[i];
	}
	a[pos] = x;//将要插入的元素,放到指定位置
	n++;//插入元素后数组长度加一
	for (i = 0; i < n; i++)//遍历数组
	{
    
    
		cout << a[i] << " ";//输出所有元素
	}
	cout << endl;
	return 0;
}

7.删除

从键盘输入任意个整数(以0结束),假设整数个数为n(n<100),则这些数据存放在一维数组的0~n-1下标中,再输入被删除元素所在下标pos(0<=pos<n),将pos下标的元素从数组中删除,若pos位置不合法,则不删除元素。输出删除后数组中的所有元素。
(本题删除一个元素,就是将要删除位置之后的元素向前移动一个位置,从而达到删除的目的)
Sample Input
10 2 7 9 11 5 4 3 6 8 20 0
8
Sample Output
10 2 7 9 11 5 4 3 8 20

#include<iostream>
using namespace std;
const int Max = 100;
int main()
{
    
    
	int a[Max],n=0,i,pos;
	cin >> a[n];
	while (a[n])//向数组中输入元素,以0结束
	{
    
    
		n++;
		cin >> a[n];
	}
	cin >> pos;
	for (i =pos; i<n-1; i++)//从要删除的元素开始,每个元素向前移动一个位置,直到最后一个元素
	{
    
    
		a[i] = a[i+1];
	}
	n--;//删除元素后数组长度减一
	for (i = 0; i < n; i++)//遍历数组
	{
    
    
		cout << a[i] << " ";//输出所有元素
	}
	cout << endl;
	return 0;
}

8.删除元素

从键盘输入任意个整数(以0结束,并假设个数n不超过100),存放在一维数组的0~n-1下标中,再输入要删除的元素x,将数组中所有的x全部删除。输出删除后数组中的所有元素。
本题删除为x的多个元素,将不等于x的元素存回原数组,将不等于x元素的总个数作为数组的长度输出数组,达到删除的目的)
Sample Input
10 2 7 9 5 11 5 4 3 6 5 8 20 0
5
Sample Output
10 2 7 9 11 4 3 6 8 20

#include<iostream>
using namespace std;
const int Max = 100;
int main()
{
    
    
	int a[Max],n=0,i,x,j=0;
	cin >> a[n];
	while (a[n])//向数组中输入元素,以0结束
	{
    
    
		n++;
		cin >> a[n];
	}
	cin >> x;
	for (i =0; i<n; i++)//遍历数组,将等于x的元素删除
	{
    
    
		if (a[i] != x)//将本数组中不等于x的元素存回原数组
		{
    
    
			a[j] = a[i];
			j++;
		}
	}
	n=j;//j为删除等于x的元素之后的数组长度
	for (i = 0; i < n; i++)//遍历数组
	{
    
    
		cout << a[i] << " ";//输出所有元素
	}
	cout << endl;
	return 0;
}

9.排序

从键盘输入任意个整数(以0结束,假设个数最大不超过100),将这些数存放在一个一维数组中,将它们从小到大排序后输出。
(排序算法我比较常用冒泡排序,冒泡排序会专门介绍,这里就不作过多说明)
Sample Input
15 2 7 9 11 5 4 3 6 8 20 0
Sample Output
2 3 4 5 6 7 8 9 11 15 20

#include<iostream>
using namespace std;
const int Max = 100;
int main()
{
    
    
	int a[Max],n=0,i,j;
	cin >> a[n];
	while (a[n])//向数组中输入元素,以0结束
	{
    
    
		n++;
		cin >> a[n];
	}
	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;
			}
		}
	}
	for (i = 0; i < n; i++)//遍历数组
	{
    
    
		cout << a[i] << " ";//输出所有元素
	}
	cout << endl;
	return 0;
}

10.中位数

从键盘输入n(n<100)个整数(以0结束),存放在一个一维数组中,输出其中位数。
说明:中位数(Medians)统计学名词,是指将数据按从小到大顺序排列起来,形成一个数列,居于数列中间位置的那个数据。若数列元素个数为奇数,则中位数为最中间的元素;
若数列元素个数为偶数,则中位数为最中间两个元素的平均数。例如:输入2 7 9 11 5 4 3 6 8 20 0则排序后结果为2 3 4 5 6 7 8 9 11 20,则中间两个数的平均值为6.5
Sample Input
2 7 9 11 5 4 3 6 8 20 0
Sample Output
6.5

#include<iostream>
using namespace std;
const int Max = 100;
int main()
{
    
    
	int a[Max],n=0,i,j;
	cin >> a[n];
	while (a[n])//向数组中输入元素,以0结束
	{
    
    
		n++;
		cin >> a[n];
	}
	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;
			}
		}
	}
	if (n % 2 == 1)//若数列元素个数为奇数,则中位数为最中间的元素
		cout << a[n/2] << endl;
	else//若数列元素个数为偶数,则中位数为最中间两个元素的平均数
	{
    
    
		cout << (double)(a[n / 2] + a[n / 2 - 1]) / 2 << endl;//因为平均值有可能不是整数,所以将整形转化为实数型
	}
	return 0;
}

11.奇偶排序

从键盘输入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;
int main()
{
    
    
	int a[Max],n=0,i,j;
	cin >> a[n];
	while (a[n])//向数组中输入元素,以0结束
	{
    
    
		n++;
		cin >> a[n];
	}
	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;
			}
		}
	}
	for (i = 0; i < n; i++)//遍历数组
		cout << a[i] << " ";//输出所有元素
	cout << endl;
	return 0;
}

12.筛选法求素数

输入m、n(m,n<100),输出[m,n]之间的素数。要求:使用筛选法求素数。
求100以内素数的筛选过程:在一张纸上写上1到100全部整数,然后逐个判断它们是否是素数, 找出所有的非素数,把它挖掉,最后剩下的就是素数。提示:可以将1100这些数存储于数组1100下标,挖掉的数据置为0。
具体做法如下:
<1> 先将1挖掉(因为1不是素数)。
<2> 找到数组中第一个非零值(2),把2的倍数挖掉。
<3> 重复步骤<2>,再把3,。。。的倍数挖掉,直至11时结束(实际上可以挖掉7的倍数后即可结束)。
<4> 数组中非零值即为素数。
Sample Input
5 19
Sample Output
5 7 11 13 17 19

#include<iostream>
using namespace std;
const int Max = 101;
int main()
{
    
    
    int a[Max], i, j,m,n;
	for (i = 1; i < 101; i++)//可以将1~100这些数存储于数组1~100下标
		a[i] = i;
	a[1] = 0;//挖掉的数据置为0
	for (j = 2; j <= 11;j++)
		for (i = j + 1; i < 101; i++)//遍历数组将符合条件的置为0
			if (a[i] % j == 0)
				a[i] = 0;
	cin >> m >> n;
	for (i = m; i <= n; i++)
		if (a[i] != 0)//数组中非零值即为素数
			cout << a[i] << " ";
	cout << endl;
	return 0;
}

以上的12道题除了9,10,11道用到排序算法之外,其他题均为一维数组的基础题。
(二维数组,字符串数组,数组与函数结合的题目在这两天我整理好之后也会发出)

猜你喜欢

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