C++数组的逆序输出

1.题目

初始化一个数组,并逆序输出

2.方法一完整代码

//4、写出一个函数,使从键盘上输入的一个字符串反序存放,并在主函数中输入和输出该字符串
#include 
#define N 8//宏定义数组长度为8
using namespace std;

int main()
{
	int arr[N] = { 1,2,3,4,5,6,7,8 };
	cout << "逆序前:" << endl;
	for (int i = 0; i < 8; i++)
	{
		cout << arr[i] << " ";
	}
	cout << endl;

	int start = 0;
	for (int i = 0; i < N / 2; i++)
	{
		int temp = arr[i];
		arr[i] = arr[N - i - 1];
		arr[N - i - 1] = temp;
	}
	cout << "逆序后:" << endl;
	for (int i = 0; i < 8; i++)
	{
		cout << arr[i] <<" ";
	}
	cout << endl;
	system("pause");
	return 0;
}

3.方法二完整代码

//4、写出一个函数,使从键盘上输入的一个字符串反序存放,并在主函数中输入和输出该字符串。
#include 
#define N 8
using namespace std;

int main()
{
	int arr[N] = { 1,2,3,4,5,6,7,8 };
	cout << "逆序前:" << endl;
	for (int i = 0; i < 8; i++)
	{
		cout << arr[i] << " ";
	}
	cout << endl;

	int start = 0;
	int end = sizeof(arr) / sizeof(arr[0]) - 1;
	while (start < end)
	{
		int temp = arr[start];
		arr[start] = arr[end];
		arr[end] = temp;

		start++;
		end--;
	}
	cout << "逆序后:" << endl;
	for (int i = 0; i < 8; i++)
	{
		cout << arr[i] <<" ";
	}
	cout << endl;
	system("pause");
	return 0;
}

你可能感兴趣的:(C++,小白,c++,蓝桥杯,算法)