Problem1833--【串与数组】4-10 数组的正负数分割排序

1833: 【串与数组】4-10 数组的正负数分割排序

Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 1  Solved: 1
[Submit] [Status] [Web Board] [Creator:]

Description

设任意n个整数存放于数组A[1..n]中,试编写算法,将所有正数排在所有负数前面(要求:正(负)数序列中数的相对顺序不变,算法时间复杂度为O(n))。

Input

多组数据,每组数据有两行,第一行为数组中存放的数的个数n,第二行为n个整数。当n=0时输入结束。

Output

对于每组数据分别输出一行,为分割排序后的数组。

Sample Input Copy

4
1 2 -1 2
5
-1 -2 1 2 3
0

Sample Output Copy

1 2 2 -1
1 2 3 -1 -2

HINT

#include <iostream>
using namespace std;
void SplitSort(int *A,int n)
{//正负数分割排序
/**************begin************/


    /**************end************/
}
void PrintA(int *A,int n)
{//依次输出数组中的数据
    for(int i=0;i<n;i++)
    {
        cout<<A[i];
        if(i!=n-1)cout<<" ";
        else cout<<endl;
    }
}
int main()
{
    int n;
    while(cin>>n)
    {
        if(n==0) break;
        int *A = new int[n];
        for(int i=0;i<n;i++) cin>>A[i];
        SplitSort(A,n);
        PrintA(A,n);
    }
    return 0;
}

Source/Category