#include"iostream"
using namespace std;
class Marray
{
public:
Marray(int n)
{
if (n < 0)
{
cout << "输入数组下标不合法" << endl;
return;
}
Len = n;
pArray = new int[n]{0};
if (pArray == NULL)
{
return;
}
for (int i = 0; i < n; i++)
{
pArray[i] = i;//完成0~n的赋值
}
}
public:
void Test()
{
int *tem = new int[Len]{0};
int j = 0;
if (tem == NULL)
{
return;
}
for (int i = 0; i < Len; i++)
{
if (pArray[i] % 2 != 0)//奇数
{
tem[j] = pArray[i];
j++;
}
}
for (int i = 0; i < Len; i++)//偶数
{
if (pArray[i] % 2 == 0)
{
tem[j] = pArray[i];
++j;
}
}
for (int i = 0; i < Len; i++)
{
pArray[i] = tem[i];
}
print();
delete[] tem;
}
protected:
void print()
{
for (int i = 0; i < Len; i++)
{
cout << pArray[i] << " ";
}
cout << endl;
}
public:
~Marray()
{
delete[] pArray;
}
private:
int Len;//数组容量
int *pArray;
};
int main()
{
Marray array(100);
array.Test();
return 0;
}
很久没做这种题目了,暴力一些