题目1185:特殊排序

3/3/2017来源:C/C++教程人气:949

PRoblem:

Solution:

比较简单的一道排序题,不知道这类题可不可以用sort()来做,不过我写的程序无法通过,会提示Wrong Answer,但我测试发现好像又没错,所以还是老老实实的写个冒泡吧
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
using namespace std;

int main()
{
    int n;
    while(cin >> n)
    {
        int i,m,max;
        vector<int> ss;
        max = 0;
        for (i = 0;i < n;i++)
        {
            cin >> m;
            ss.push_back(m);
            if (m > max) max = m;
        }
        cout << max << endl;
        if (n == 1)
        {
            cout << -1 << endl;
            break;
        }
        sort(ss.begin(),ss.end());
        for(i = 0;i < n-1;i++)
        {
            cout<< ss[i] << ' ';
        }
    }
    return 0;
}
排序部分改为冒泡:
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
using namespace std;

int main()
{
    int n;
    while(cin >> n)
    {
        int i,m,max;
        int  ss[n];
        max = 0;
        for (i = 0;i < n;i++)
        {
            cin >> m;
            ss[i] = m;
            if (m > max) max = m;
        }
        cout << max << endl;
        if (n == 1)
        {
            cout << -1 << endl;
            break;
        }
        for(i = 1;i < n;i++)
        {
            int temp;
            for(int j = 0;j < n-i;j++)
            {
                if (ss[j] > ss[j+1])
                {
                    temp = ss[j];
                    ss[j] = ss[j+1];
                    ss[j+1] = temp;
                }
            }
        }
        for(i = 0;i < n-1;i++)
        {
            cout<<ss[i]<<' ';
        }
    }
    return 0;
}