VC++~~数组元素颠倒排列

来源:百度知道 编辑:UC知道 时间:2024/05/30 15:47:48
用new运算符产生一个一维数组,输入并输出数组中的各个元素的值,将数组元素颠倒排列后,再次输出各元素的值。最好加注释~~

CString.reverse(...)

记得好像有这个函数

#include <iostream>
using namespace std;

int main()
{
//首先声明两个要用到的变量,然后提示输入数字个数
int nCount;
int* array;
cout<<"Please input the count of the number"<<endl;
cin>>nCount;

//根据输入的数字个数动态创建数组,并输入数字初始化数组
array = new int[nCount];
for(int i = 0; i < nCount; i++)
cin>>array[i];

//输出反转前的数组
cout<<"Before reversing the array is:"<<endl;
for(i = 0; i < nCount; i++)
cout<<array[i]<<" ";
cout<<endl;

//对数组进行反转
for(i = 0; i < nCount / 2; i++)
{
int temp = array[i];
array[i] = array[nCount - i - 1];
array[nCount - i - 1] = temp;
}

//输入反转后的数组
cout<<"After reversing the array is:"<<endl;
for(i = 0; i < nCount; i++)
cout<<array[i]&