九度OJ 1041 Simple Sorting (排序,STL)

题目1041:Simple Sorting

时间限制:1 秒

内存限制:32 兆

特殊判题:

提交:3971

解决:1483

题目描述:

You are given an unsorted array of integer numbers. Your task is to sort this array and kill possible duplicated elements occurring in it.

输入:

For each case, the first line of the input contains an integer number N representing the quantity of numbers in this array(1≤N≤1000). Next N lines contain N integer numbers(one number per each line) of the original array.

输出:

For each case ,outtput file should contain at most N numbers sorted in ascending order. Every number in the output file should occur only once.

样例输入:
6
8 8 7 3 7 7
样例输出:
3 7 8

纯C程序:

#include<stdio.h>
int aux[1001];
int a[1001];
void merge(int a[],int l,int mid,int h){
    int i=l;
    int j=mid+1;
    for(int k=l;k<=h;++k)
        aux[k]=a[k];
    for(int k=l;k<=h;++k){
        if(i>mid)a[k]=aux[j++];
        else if(j>h)a[k]=aux[i++];
        else if(aux[i]<aux[j])a[k]=aux[i++];
        else a[k]=aux[j++];
    }
}
void m_sort(int a[],int l,int h){
    if(h<=l)return ;
    int mid=l+(h-l)/2;
    m_sort(a , l , mid);
    m_sort(a,mid+1,h);
    merge(a,l,mid,h);
}
int main(int argc, char *argv[])
{
  //  freopen("1041.in","r",stdin);
    int n;
    while(~scanf("%d",&n))
    {
        for(int i=0;i<n;++i)
            scanf("%d",&a[i]);
        m_sort(a,0,n-1);
        printf("%d",a[0]);
        int t=a[0];
        for(int i=1;i<n;++i)
        {
            if(a[i]!=a[i-1]){
                printf(" %d",a[i]);
                t=a[i];
            }
        }
        printf("\n");
    }
    return 0;
}
 
/**************************************************************
    Problem: 1041
    User: kirchhoff
    Language: C
    Result: Accepted
    Time:0 ms
    Memory:920 kb
****************************************************************/


c++版STL:

#include<cstdio>
#include<cstring>
#include<vector>
#include<iostream>
#include<algorithm>
using namespace std;
vector<int>a;
int main(int argc, char *argv[])
{
    int n;
    int t;
    while(cin>>n)
    {
        a.clear();
        for(int i=0;i<n;++i)
        {
            cin>>t;
            a.push_back(t);
        }
        sort(a.begin(),a.end());
        vector<int>::iterator new_end;
        new_end=unique(a.begin(),a.end());
        cout<<*a.begin();
        for(vector<int>::iterator it=a.begin()+1;it!=new_end;++it)
        {
            cout<<" "<<*it;
        }
        cout<<endl;
    }
    return 0;
}
 
/**************************************************************
    Problem: 1041
    User: kirchhoff
    Language: C++
    Result: Accepted
    Time:10 ms
    Memory:1520 kb
****************************************************************/


高人版(0内存,0ms):

#include<iostream> 
#include<set>
using namespace std;
int main()
{ 
    int n;
    set<int> st;
    set<int>::iterator it;
    int in;
    while(cin>>n)
    {
       st.clear();
       for(int i=0;i<n;i++)
       {
           cin>>in;
           st.insert(in);
       }
       for(it=st.begin();it!=st.end();it++)
       {
          cout<<*it<<" ";
       }
       cout<<endl;
    }
    return 0; 
} 
/**************************************************************
    Problem: 1041
    User: wuying
    Language: C++
    Result: Accepted
    Time:0 ms
    Memory:0 kb
****************************************************************/


真有想法~可是set为啥不计入内存呢?OJ问题?没想明白


郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。