首页 > 试题广场 >

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
#include<stdio.h>
#include<stdlib.h>
//利用计数排序的思想
int main(){
	int n;
	while(scanf("%d",&n)!=EOF){
		int max=0;
		int *temp=(int *)malloc(sizeof(int)*n);
		for(int i=0;i<n;i++){ //输入n个数,并记录最大的那个值
			int n2;
			scanf("%d",&n2);
			temp[i]=n2;
			if(n2>max)max=n2;
		}
		int *num=(int *)malloc(sizeof(int)*(max+1));
		for(int j=0;j<=max;j++)// 初始化该HashTable
			num[j]=0;
		for(int k=0;k<n;k++)
			if(num[temp[k]]==0)
				num[temp[k]]+=1;
		for(int x=0;x<=max;x++)// 
			if(num[x])
				printf("%d ",x);
		printf("\n");
	}
}

发表于 2022-01-24 20:20:43 回复(0)

问题信息

难度:
1条回答 5599浏览

热门推荐

通过挑战的用户

查看代码