首页 > 代码库 > 九度OJ 1041 Simple Sorting (排序,STL)
九度OJ 1041 Simple Sorting (排序,STL)
时间限制: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 ****************************************************************/
#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问题?没想明白
九度OJ 1041 Simple Sorting (排序,STL)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。