样例:
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;
}