数组元素的逆序

n次逆序和n/2次逆序

#include<iostream>

using namespace std;

int main()
{
	char a[] = { 'a','b','c','d','e','f','\0' };
	int n = sizeof(a) / sizeof(*a);
	cout << n << endl;
	//正序输出数组元素
	for (int i = 0; i < n; ++i)
	{
		cout << a[i] << endl;
	}
	//n次逆序输出数组元素
	for (int i = n; i >=0; i--)
	{
		cout << a[i] << endl;
	}
	
	//n/2次逆序输出数组元素
	for (int i = 0; i < n / 2; ++i) {
		char test = a[n-1-i];
		a[n-1-i] = a[i];
		a[i] = test;
	}
	for (int i = 0; i < n; ++i)
	{
		cout << a[i] << endl;
	}
     system("pause");
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41848006/article/details/81698825