1060. 数组排序

在这里插入图片描述
样例:
input:
3
10
9 6 3 10 3 10 6 4 3 4
10
10 9 8 7 6 5 4 3 2 1
20
4 3 7 8 6 5 8 4 2 2 7 9 5 9 6 8 6 6 3 10

output:
case #0:
3 3 3 4 4 6 6 10 10 9
case #1:
1 2 3 4 5 6 7 8 9 10
case #2:
6 6 6 6 8 8 8 2 2 3 3 4 4 5 5 7 7 9 9 10

思路:
struct里搞个cnt记录个数,外面搞个vis[]记录放的位置就ok
参考代码:

#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn=11111;
int maxx=-0x3f3f3f3f;
int t,n,cnt;
int vis[555];
struct stu
{
    int i;
    int cntt;
	bool operator<(const stu &b)
	{
		if(cntt==b.cntt) return i<b.i;
		else return cntt>b.cntt;
	}
}a[maxn];
int main()
{
	cin>>t;
	for(int i=1;i<=t;i++)
	{
		cin>>n;
        cnt=1;
		for(int j=1;j<=n;j++)
		{
            int temp;
			cin>>temp;
			if(vis[temp]!=0)
            {
                a[vis[temp]].cntt++;
            }
            else
            {
                vis[temp]=cnt;
                a[cnt].i=temp;
                a[cnt].cntt=1;
                cnt++;
            }
            //cout<<"temp="<<temp<<" "<<"vis[temp]="<<vis[temp]<<" "<<"a[vis[temp]].cntt="<<a[vis[temp]].cntt<<endl;
		}
		sort(a+1,a+cnt+1);
        cout<<"case #"<<i-1<<":"<<endl;
		for(int j=1;j<cnt-1;j++)
	    {
            //cout<<"a[j].i="<<a[j].i<<" "<<"a[j].cntt="<<a[j].cntt<<endl;
            for(int k=1;k<=a[j].cntt;k++) cout<<a[j].i<<" ";
	    }
		if(a[cnt-1].cntt==1) cout<<a[cnt-1].i;
		else
		{
			for(int k=1;k<a[cnt-1].cntt;k++) cout<<a[cnt-1].i<<" ";
			cout<<a[cnt-1].i;
		}
        cout<<endl;
        memset(&a, 0, sizeof (stu));
        for(int j=1;j<=555;j++) vis[j]=0;
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值