首页 > 学院 > 开发设计 > 正文

题目1185:特殊排序

2019-11-06 08:24:11
字体:
来源:转载
供稿:网友

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;}
上一篇:AJAX学习

下一篇:uva10256 The Great Divide

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表