首页 > 试题广场 >

Simple Sorting

[编程题]Simple Sorting
  • 热度指数:5229 时间限制: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
'''归并排序实现'''

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)

问题信息

难度:
1条回答 5604浏览

热门推荐

通过挑战的用户

查看代码