VmatrixOJ--排序和去重

本文介绍了一种处理重复数据并进行排序的算法,通过读取多组测试数据,去除重复项并按升序排列,最后输出每组处理后的数据。示例输入包括三组数据,每组数据数量不等,输出为每组数据的排序且无重复项的结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Description

In this exercise you need to process a set of data, which has duplicate data and is unsorted. You need to sort them and just print one of the same data.

Input

the first line is a number n(0<n<=50), which stands for the number of test data.

the next 2*n lines contain n group of test data

In each group of test data , the first is a number m(0<m<=50) stands for the size of the test data, and the next line is the test datas.

Output

print sorted data without duplicate data.

Every output item is followed by a space , including the last output item. And each group of output is split by "\n", including the last row.

Sample Input

3
8
5 5 4 4 3 3 2 2
5
-2 -2 -3 -3 0
10
9 4 6 5 7 8 0 1 3 2

Sample Output

2 3 4 5 
-3 -2 0 
0 1 2 3 4 5 6 7 8 9 

 Code:

#include<stdio.h>
int main(){
	int n,i,j,k,x,temp,record=1,mid;
	scanf("%d",&n);
	int lim[50],num[50]={0},arr[50][50]={0},dis[50][50]={0};
	for(i=0;i<n;i++)
	{
		scanf("%d",&lim[i]);
		for(j=0;j<lim[i];j++){
			scanf("%d",&arr[i][j]);
			for(k=num[i]-1,record=1;k>=0;k--)
			{
				if(arr[i][j]==dis[i][k])
				{
					record=0;
					k=-1;
				}
			 } 
			 if(record){
			 	dis[i][num[i]]=arr[i][j];
			 	mid=num[i];
			 	num[i]++;		 	
			 	for(x=num[i]-1;x>=0;x--)
			 	{
			 		if(dis[i][mid]<dis[i][x])
			 		{
			 		temp=dis[i][mid];
			 		dis[i][mid]=dis[i][x];
			 		dis[i][x]=temp;
			 		mid=x;
					 }
				 }		 			 	
			 }
		}
		for(j=0;j<num[i];j++)
		printf((j==num[i]-1)?"%d \n":"%d ",dis[i][j]);	
	}
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值