C++實現元素逆置


#include <iostream>
#include<ctime>
using namespace std;
int main()
{
	int arr[5] = { 1,2,3,4,5 };
	cout << "元素逆置前" << endl;
	for (int i=0;  i < 5; i++)
	{
		cout  << arr[i] << endl;
	}
	int start = 0;
	int end = sizeof(arr) / sizeof(arr[0]) - 1;
	while (start<end)
	{
		int temp = arr[start];
		//arr[end] = arr[start];
		//temp = arr[end];
		arr[start] = arr[end];
		arr[end] = temp;
		start++;
		end--;
	}
	cout << "元素逆置後的結果爲:" << endl;
	for (int i = 0; i < 5; i++)
	{
		cout << arr[i] << endl;
	}
	return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章