C/C++ 排序专题

本文详细探讨了多种排序算法的应用,包括按非重复字符数排序、随机排序、行数据排序、按1的位数排序、极坐标排序、字符频率排序、排序去重、成绩排序以及数组特殊排序等,涵盖了不同的排序场景和策略。
摘要由CSDN通过智能技术生成

排序专题

3238.字串非重复字符数排序

对 n(1<=n<=100)个由大写字母组成的长度为1~20 的字符串,按字符串中不同字符个数从多到少的顺序进行排序。不同字符个数相同的字符串按字符串的字典序排序。

例如:ABCCBA 和 CDFE 按照 CDFE ,ABCCBA 顺序排序,因为 ABCCBA 的不同字符个数是3个,CDFE是4 个。ABCCBAX 和 CDFE 按照 ABCCBAX, CDFE 顺序排序,因为这两个字符串的不同字符个数都是4个,而 ABCCBAX 的字典序小于 CDFE。

#include <iostream>
#include <cstring>
using namespace std;

struct Word
{
   
    char id[30];
    int diff;
    Word()
    {
   
        id[0] = '\0';
        diff = 0;
    }
};

int getDiff(Word x)
{
   
    int flag[26] = {
    0 };
    int cnt = 0;
    for (int i = 0; x.id[i] != '\0'; i++)
    {
   
        if (flag[x.id[i] - 65] == 0) cnt++;
        flag[x.id[i] - 65]++;
    }
    return cnt;
}

int cmp(const void* a, const void* b)
{
   
    Word* p1 = (Word*)a,* p2 = (Word*)b;
    int x;
    return (x= p2->diff - p1->diff)?x:strcmp(p1->id, p2->id);
}

int main()
{
   
    int t; cin >> t;
    for (int i = 0; i < t; i++)
    {
   
        Word a[1000];
        int n; cin >> n;
        for(int j=0;j<n;j++)
        {
   
            cin >> a[j].id;
            a[j].diff = getDiff(a[j]);
        }
        qsort(a, n, sizeof(a[0]), cmp);
        cout << "case #" << i << ":" << endl;
        for (int j = 0; j < n; j++)
        {
   
            cout << a[j].id << endl;
        }
    }
}

2896.随机排序

给定一组以一个空格分隔的只含大小写字母的字符串。与普通字典序不同,按照给定的字母顺序对这组字符串排序。

设两个字符串的字母不会完全相同。如:Hat、hat、HAt 等不会同时出现。

例如:
字母顺序为 QWERTYUIOPASDFGHJKLZXCVBNM 时:

一组字符串 hat cat bat book bookworm Dallas Austin Houston fire firefox fumble

排序结果为:Austin Dallas fumble fire firefox Houston hat cat book bookworm bat

#include <iostream>
#include <algorithm>
#include <sstream>
using namespace std;

string arr[1000];
string order;

bool cmp(string a, string b)
{
   
	for (int i = 0; i < min(a.length(), b.length()); i++)
	{
   
		char ch1 = toupper(a[i]);
		char ch2 = toupper(b[i]);
		if (order.find(ch1) == order.find(ch2)) continue;
		return order.find(ch1) < order.find(ch2);
	}
	return a.length() < b.length();
}

int main()
{
   
	string s;
	while (cin >> order)
	{
   
		getchar();
		getline(cin, s);
		istringstream ss(s);
		int cnt = 0;
		while (ss >> arr[cnt]) cnt++;
		sort(arr, arr + cnt, cmp);
		for (int i = 0; i < cnt; i++)
		{
   
			cout << arr[i];
			if (i != cnt - 1) cout << " ";
			else cout << endl;
		}
	}
	return 0;
}

2947.行数据排序

有N组数据,每行有若干数量不等的整数组成。现在要对这N组数据排序。

排序原则为:

首先比较行中的第一个数的值,将第一个数大的行排在前面;

若第一个数相等的话,则按照第二个数的值排序(若某行没有第二个数,则该行排在后面);

若第二个数还是相等的话,则比较第三个数,依次类推。

例如:
14 38 11 89
27 34
27 12 34
27
92 2 3 1
17 2

排序的结果为:
92 2 3 1
27 34
27 12 34
27
17 2
14 38 11 89

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
using namespace std;

int cmp1(const void* a, const void* b)
{
   
    int* s1 = (int*)a, * s2 = (int*)b;
    while (*s1 != 0 && *s2 != 0 && *s1 == *s2) s1++, s2++;
    return *s2 - *s1;
}

bool cmp(vector<int> a, vector<int> b)
{
   
    return a > b;
}

int main()
{
   
    int t; cin >> t;
    for (int i = 0; i < t; i++)
    {
   
        int n; cin >> n;
   
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值