码迷,mamicode.com
首页 > 编程语言 > 详细

数字排序

时间:2015-11-23 16:22:44      阅读:164      评论:0      收藏:0      [点我收藏+]

标签:

题编号:    201503-2
 
试题名称:    数字排序
时间限制:    1.0s
内存限制:    256.0MB
问题描述:
  给定n个整数,请统计出每个整数出现的次数,按出现次数从多到少的顺序输出。
输入格式:
  输入的第一行包含一个整数n,表示给定数字的个数。
  第二行包含n个整数,相邻的整数之间用一个空格分隔,表示所给定的整数。
输出格式:
  输出多行,每行包含两个整数,分别表示一个给定的整数和它出现的次数。按出现次数递减的顺序输出。如果两个整数出现的次数一样多,则先输出值较小的,然后输出值较大的。
样例输入
12
5 2 3 3 1 3 4 2 5 2 3 5
样例输出
3 4
2 3
5 3
1 1
4 1
评测用例规模与约定
  1 ≤ n ≤ 1000,给出的数都是不超过1000的非负整数。

该题也是一道简单题,由“评测用例规模与约定”可以得出输入规模不大,在[1,1000]之间。下面给出多种方法:

1、使用C++的sort函数进行排序

#include<iostream> 
#include<algorithm> 
using namespace std;
struct num{
    int val;
    int t;
};

bool cmp(num a, num b){
    if(a.t != b.t)
        return a.t>b.t;
    return a.val<b.val;
}

num a[1001];
int main(){
    int n, i, x;
    for(i=0;i<=1001;++i){
        a[i].val = i;
    }
    cin>>n;
    for(i=1;i<=n;++i){
        cin>>x;
        ++a[x].t;
    }
    sort(a, a+n, cmp);
    for(i=0;i<=n;++i){
        if(a[i].t==0)
            break;
        cout<<a[i].val<< <<a[i].t<<endl;
    }
    return 0;
}

自定义实现sort函数

using namespace std;


typedef struct countInt{
    int number;
    int counts;
};

//countInt A[1001];
void quickSort(countInt A[], int l, int r)
{
    if (l < r)
    {
        int i = l;
        int j = r;
        countInt tmp = A[i];
        while (i < j)
        {
            while (i < j && (A[j].counts <= tmp.counts))
            {
                //如果不满足,跳出循环,将A[j]放到A[i]的位置上
                if (A[j].counts == tmp.counts && A[j].number < tmp.number)
                    break;
                j--;
            }
            if (i < j)
            {
                A[i] = A[j];
                i++;
            }

            while (i < j && (A[i].counts >= tmp.counts))
            {
                if (A[i].counts == tmp.counts && A[i].number > tmp.number)
                    break;
                i++;
            }

            if (i < j)
            {
                A[j] = A[i];
                j--;
            }
        }
        A[i] = tmp;
        quickSort(A, l, i - 1);
        quickSort(A, i + 1, r);
    }
}

2、STL模板实现

#include <utility>
#include <iostream>
#include <map>
#include <iterator>
#include <functional>

using namespace std;

int main(){
    map<int, int> numAndCounts;
    multimap<int, int, greater<int> > heapMax;
    int n;
    cin >> n;
    for (int i = 0; i<n; i++)
    {
        int a;
        cin >> a;
        numAndCounts[a]++;
    }
    for (map<int, int>::iterator it = numAndCounts.begin(); it != numAndCounts.end(); it++)
    {
        heapMax.insert(pair<int, int>(it->second, it->first));
    }
    for (map<int, int>::iterator it = heapMax.begin(); it != heapMax.end(); it++)
    {
        cout << it->second << " " << it->first << endl;
    }
    return 0;
}

第二种方法代码比较简单,但是必须对STL有深入的了解。其实看一下包含的头文件就知道了!

 

 

数字排序

标签:

原文地址:http://www.cnblogs.com/tgycoder/p/4988478.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!