1042. Shuffling Machine (20)

本文介绍了一个使用C++实现的卡片排序算法,通过定义结构体数组存储特定字符串与整数,并利用自定义比较函数进行排序。文章详细展示了如何初始化结构体数组、读取用户输入的数据并进行多次排序的过程。

思路: 将数据存储到结构体数组 后按照给定的顺序排序

#include<iostream>
#include<vector>
#include<cmath>
#include<string>
#include<cstring>
#include<algorithm>
#include<iomanip>
#include<map>
using namespace std;
#define Max 10000

struct node
{
    string S;
    int N;
}Hash[55];


bool cmp(node n1, node n2)
{
    return n1.N<n2.N;
}

void Fun()
{
    for(int i=1; i<=9; i++)
    {
        Hash[i].S="S";
        Hash[i].S+=i+'0';
    }
    Hash[10].S="S10";
    Hash[11].S="S11";
    Hash[12].S="S12";
    Hash[13].S="S13";
    for(int i=14, j=1; i<=22; i++, j++)
    {
        Hash[i].S="H";
        Hash[i].S+=j+'0';
    }
    Hash[23].S="H10";
    Hash[24].S="H11";
    Hash[25].S="H12";
    Hash[26].S="H13";
    for(int i=27, j=1; i<=35; i++, j++)
    {
        Hash[i].S="C";
        Hash[i].S+=j+'0';
    }
    Hash[36].S="C10";
    Hash[37].S="C11";
    Hash[38].S="C12";
    Hash[39].S="C13";
    for(int i=40, j=1; i<=48; i++, j++)
    {
        Hash[i].S="D";
        Hash[i].S+=j+'0';
    }
    Hash[49].S="D10";
    Hash[50].S="D11";
    Hash[51].S="D12";
    Hash[52].S="D13";
    Hash[53].S="J1";
    Hash[54].S="J2";
}

int main()
{
    Fun();
    int K, arr[55];
    cin>>K;
    for(int i=1; i<55; i++)
        cin>>arr[i];
    while(K--)
    {
        for(int i=1; i<55; i++)
            Hash[i].N=arr[i];
        sort(Hash+1, Hash+55, cmp);
    }
    for(int i=1; i<55; i++){
        if(i>1)
            cout<<" ";
        cout<<Hash[i].S;
    }
    cout<<endl;
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值