首页 > 试题广场 >

Simple Sorting

[编程题]Simple Sorting
  • 热度指数:5223 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 64M,其他语言128M
  • 算法知识视频讲解
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.
示例1

输入

6
8 8 7 3 7 7

输出

3 7 8

python3 solution:

while True:
    try:
        a,b=input(),map(int,input().split())
        print(" ".join(map(str,sorted(set(b)))))
    except:
        break
发表于 2017-10-06 15:58:41 回复(0)
排序法:2ms 384k 先排序,再互异输出
#include <stdio.h>
#include <algorithm>
using namespace std;
int main()
{
int n,a[1000];
while(scanf("%d",&n)!=EOF)
{
for(int i=0;i<n;++i)
scanf("%d",&a[i]);
sort(a,a+n); //升序排序
printf("%d",a[0]);
for(int i=1;i<n;++i){
if(a[i]>a[i-1]){  //后一项必须严格大于前一项
printf(" %d",a[i]);
}
}
printf("\n");
}
return 0;
}
hash函数法 此方法有局限性 要求每个元素都在一定的确定范围内--不过题目没有说清楚  经过我大量提交发现测试用例中每个元素都是在0到100之间  所以取N为101就可以了 说明测试用例有局限 题目说的是integer numbers 测试用例居然只有0-100 连负数都没有 好吧
效率 2ms 380k  也并没有比排序法更快??
#include <stdio.h>
#define N 101// 当然要求排序的数目有限 可以相应的自己设置 
int main()
{
    int n,k;
    while(scanf("%d",&n)!=EOF)
    {
        int hash[N]={0};
        for(int i=0;i<n;++i){
            scanf("%d",&k);
            ++hash[k];
        }
        int flag=0; //是用来检测第一个不为空的哈希值 ,此举是为了解决空格的格式问题
        for(int i=0;i<N;++i){
            if(hash[i]!=0&&flag==0){  //第一个数不需要空格
                printf("%d",i);
                flag=1;
            }else if(hash[i]!=0&&flag==1){ //后续的每个数前面空一格
                printf(" %d",i);
            }
        }
        printf("\n");  //别忘了换行
    }
    return 0;
}

编辑于 2018-01-13 19:58:53 回复(0)
#include<iostream>
#include<cstdio>
#include<string.h>
#include<vector>
#include<cmath>
#include<algorithm>
#define MAXSIZE 1000001
#define PAI
using namespace std;
#define N 31
int main()
{
    int n;
    while(cin>>n)
    {
        int a[n];
        for(int i=0;i<n;i++)
            cin>>a[i];
        sort(a,a+n);
        cout<<a[0]<<" ";
        for(int i=1;i<n;i++)
        {
            if(a[i]!=a[i-1])
                cout<<a[i]<<" ";
        }
    }
    return 0;
}
sort 排序,不同的直接输出
发表于 2022-02-14 11:43:57 回复(0)
#include<stdio.h>
int main()
{
    int n,a[1000],i,j,temp;
    scanf("%d",&n);//输入
    for(i=0;i<n;i++)
        scanf("%d",&a[i]);
    for(i=0;i<n-1;i++)//冒泡
        for(j=0;j<n-1-i;j++)
            if(a[j]>a[j+1])
            {//交换
                temp=a[j];a[j]=a[j+1];a[j+1]=temp;
            }
    for(i=0;i<n-1;i++)//去重 
    {//判断从0-n-2个数,最后一个数n-1不判断是因为后面没有能与他相同的了
        if(a[i]==a[i+1])//本位置与下一个位置的数相等则顺序前移
        {
            for(j=i;j<n;j++)//从本位置顺序前移
                a[j]=a[j+1];
            i--;//还从当前位置判断
            n--;//去掉一个数数组容量减一
        }
    }
    //输出
    for(i=0;i<n;i++)
        printf("%d ",a[i]);
}

发表于 2020-03-23 15:01:32 回复(0)
日常感慨没有sort函数我可怎么活hhh
#include<iostream>
#include<algorithm>
using namespace std;
int main(){
    int n;
    while(cin>>n){
        int* a=new int[n];
        for(int i=0;i<n;i++)
            cin>>a[i];
        sort(a,a+n);
        for(int i=0;i<n;i++){
            if((a[i]!=a[i-1]&&i>0&&i<n-1)||i==0) cout<<a[i]<<" ";
            if(i==n-1&&a[i]!=a[i-1]) cout<<a[i];
        }
        cout<<endl;
    }
}

发表于 2019-02-09 22:18:50 回复(3)
#include<iostream>
#include<stdio.h>
#include<set>
using namespace std;
int main(){
    
    int count=0;int temp=0;
    set<int> st;
    cin>>count;
    while((scanf("%d",&temp))!=EOF){
        st.insert(temp);
        
    }
     
    for(set<int>::iterator it=st.begin ();it!=st.end ();it++)
    {
        printf("%d ",*it); 
    }
    
    
    return 0;
}
发表于 2019-01-05 15:42:49 回复(0)
while True:
    try:
        num,digits = int(input()),list(map(int,list(set(input().split()))))
        print(' '.join(map(str,sorted(digits))))
    except Exception:
        break
编辑于 2018-10-14 13:26:34 回复(0)
#include<stdio.h>
#include<stdlib.h>
#include<iostream>
#include<algorithm>
using namespace std;
int compare(int a,int b)
{
    return a<b;
}
int main()
{
    int n,a[2000],b[2000],k=1;
    cin>>n;
    for(int i=0; i<n; i++)
        cin>>a[i];
    sort(a,a+n);
    b[0]=a[0];
    for(int i=1; i<n; i++)
    {
        if((a[0]!=a[i])&&(b[k-1]!=a[i]))
        {
            b[k++]=a[i];
        }
    }
    for(int i=0; i<k; i++)
    {
        if(i<k-1)
            cout<<b[i]<<" ";
        else
            cout<<b[i]<<endl;
    }
    return 0;
}
 
发表于 2018-04-19 20:49:12 回复(0)
__author__ = 'Yaicky'
import copy
while True:
    try:
        n = input()
        numList = map(int, raw_input().strip().split())
        numList.sort()
        rlt = copy.deepcopy(numList)

        last = 9999999
        for i in numList:
            if i == last:
                rlt.remove(i)
            else:
                last = i
        rlt = map(str, rlt)
        print ' '.join(rlt)
    except:
        break

发表于 2016-06-24 16:52:16 回复(0)
#include <stdio.h>
(737)#include <stdlib.h>
#include <algorithm>                     //这个C语言没有的
using namespace std;

int main()
{
    int n;
    while(~scanf("%d", &n))
    {
        int array[1000];
        for(int i=0; i<n; i++)
        {
            scanf("%d", &array[i]);
        }
        
        sort(array, array+n);            //C语言不给用sort的话可自定义冒泡排序函数
        
        printf("%d ", array[0]);         //先输入排序好的第一个数
        
        for(int i=1; i<n; i++)
        {
            if(array[i] != array[i-1])     //去重操作
                printf("%d ", array[i]);
        }
    }
}
各位python大神别秀了,这些高校复试题怎么可能给用python呢,C++给不给用都是看心情的🤣
发表于 2020-03-15 15:25:41 回复(0)
# 人生苦短,我用Python
input()
print(" ".join(sorted(list(set(input().split())), key=lambda x : int(x))))

编辑于 2018-07-01 11:40:58 回复(0)
确实是比较简单,也就写这样的题还能写一写
#include <iostream>
#include<vector>
#include<algorithm>
using namespace std;

int main() {
//这里选择先排序,再将遍历后的元素遍历一遍来去重
    int n;
    cin >> n;
    vector<int>tmp(n);
    for (int i = 0; i < n; i++) {
        int x;
        cin >> x;
        tmp[i] = x;
    }
    sort(tmp.begin(), tmp.end());
    int res[n];
    res[0] = tmp[0];
    int j = 1;
    for (int k = 1; k < n; k++) {
        if (tmp[k] == tmp[k - 1])continue;
        res[j++] = tmp[k];
    }
    for (int s = 0; s < j; s++) {
        cout << res[s];
        cout << " ";
    }
    cout << endl;
}


发表于 2024-01-17 16:40:50 回复(0)
#include "bits/stdc++.h"
using namespace std;
inline int read() {
    int x = 0, f = 1;
    char c = getchar();
    while (!(c <= '9' && c >= '0')) {
        if (c == '-') {
            f = -1;
        }
        c = getchar();
    }
    while (c <= '9' && c >= '0') {
        x = (x << 3) + (x << 1) + (c ^ 48);
        c = getchar();
    }
    return x * f;
}

set<int>s;

int main() {
    int n=read();
    for(int i=1;i<=n;i++){
        s.insert(read());
    }
    for(auto t:s){
        cout<<t<<" ";
    }


    return 0;
}

发表于 2023-08-12 19:15:57 回复(0)
'''归并排序实现'''

def merge(a, b):
    i = 0
    j = 0
    c = []
    while i < len(a) and j < len(b):
        if a[i] < b[j]:
            c.append(a[i])
            i+=1
        elif a[i] > b[j]:
            c.append(b[j])
            j+=1
    c.extend(a[i:])
    c.extend(b[j:])
    return c

def merge_sort(a, l, r):
    if r - l <= 1:
        return a[l:r]
    
    m = (l + r) // 2
    left = merge_sort(a, l, m)
    right = merge_sort(a, m, r)
    res = merge(left, right)
    return res

def sim(a):
    return merge_sort(a, 0, len(a))

while True:
    try:
        a = []
        n = int(input())
            
        e = list(map(int, input().split()))
        e = set(e)
        #print(set(e))
        e = list(e)
        #print(e)
        e = sim(e)
        e = map(str, e)
        e = ' '.join(e)
        print(e)
    except:
        break

发表于 2024-03-23 12:21:05 回复(0)
#include <iostream>
#include <set>
using namespace std;

int main() {
    int n;
    while (cin >> n) {
        //运用C++内置泛型模板类set存储数据,set内部用红黑树实现,
        //元素自动有序,且自动排除重复元素
        set<int>mySet;
        while (n--) {
            int number;
            cin >> number;
            mySet.insert(number);
        }
        for (const auto& e : mySet) {   //顺序输出set中的元素
            cout << e << " ";
        }
        cout << endl;
    }
    return 0;
}

编辑于 2024-03-02 15:14:12 回复(0)
用一下set就好了
编辑于 2024-02-17 14:44:06 回复(0)
STL之set,自动去重
#include <cstdio>
#include <iostream>
#include <set>
using namespace std;

int main(){
    int n;
    while(scanf("%d",&n) != EOF){
        int a;
        set<int> num;
        for(int i = 0; i < n; ++i){
            scanf("%d",&a);
            num.insert(a);
        }
        set<int>::iterator it;
        for(it = num.begin();it != num.end();++it){
            cout<<*it<<" ";
        }
        printf("\n");
    }
    return 0;
}


发表于 2023-03-14 20:35:31 回复(0)
//考408的友友应该对这道题很眼熟,有年数据结构的大题就是这个,先快排在筛出重复的13分,暴力10分
//用空间换时间得15分。不过理论归理论,这道题用空间换时间由于需要对Judge数组遍历,有多个for循环
//所以时间也不咋快。
#include "stdio.h"
#include "climits"
using namespace std;
int n;int array[1010];
bool Judge[100000];

int main(){
    int max = INT_MIN;
    while (scanf("%d",&n)!=EOF){
        for (int i = 1; i <= n; ++i) {
            scanf("%d",array+i);
            if(array[i] > max)
                max = array[i];
        }
        for (int i = 1; i <= max; ++i) {
            Judge[i] = false;
        }
        for (int i = 1; i <= n; ++i) {
            Judge[array[i]] = true;
        }
        for (int i = 1; i <= max; ++i) {
            if(Judge[i] == true)
                printf("%d ",i);
        }
        printf("\n");
    }
}

发表于 2023-03-13 19:47:20 回复(0)
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
import java.util.stream.Stream;
public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		while(sc.hasNext()) {
			int n = sc.nextInt();
			List<Integer> list = new ArrayList<>();
			for(int i=0;i<n;++i) {
				list.add(sc.nextInt());
			}
			Stream stream = list.stream();
			stream.distinct().sorted().forEach(i->System.out.print(i+" "));
		}
	}
}
发表于 2023-03-05 14:49:00 回复(0)
import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNextInt()) {
            int n = in.nextInt();//数据个数
            int a[] = new int[n];
            for (int i = 0; i < n; i++) {
                a[i] = in.nextInt();
            }
            Arrays.sort(a);//对数组a排序
            ArrayList<Integer> b = new ArrayList();
            for (int i = 0; i < n; i++) {
                if (!b.contains(a[i]))//保证不出现重复的数据
                    b.add(a[i]);
            }
            for (Integer i : b)
                System.out.print(i + " ");
        }
    }
}

发表于 2023-03-03 23:09:06 回复(0)