uva 10420 - List of Conquests

本文介绍了一个使用C++编写的程序,该程序用于处理国家和对应数量的输入,并通过自定义排序函数按国家名称升序排列。文章详细解释了程序结构、变量定义及排序逻辑,适用于希望了解C++字符串处理和排序算法实现细节的读者。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include<cstdio>
#include<cstring>
#include<cctype>
#include<algorithm>
using namespace std;

int T = 0;
struct Conquest
{
    int num;
    char s[80];
}str[2005];

int cmp(struct Conquest a,struct Conquest b)//自己写一个排序参数
{
    int a_len = strlen(a.s);
    int b_len = strlen(b.s);
    for(int i = 0; a.s[i]!='\0'||b.s[i]!='\0'; i++)
    {
        if(a.s[i]<b.s[i])return 1;
        else if(a.s[i]>b.s[i]) return 0;
    }
    if(a_len <= b_len)return 1;
    else return 0;
}

int main()
{
    #ifdef LOCAL
    freopen("in.txt","r",stdin);
    #endif // LOCAL
    int n,i;
    scanf("%d",&n);
    while(n--)
    {
        char contry[80],name[80];
        scanf("%s",contry);
        getchar();
        gets(name);
        if(T == 0)
        {
            strcpy(str[T].s,contry);
            str[0].num++;
            T++;
        }
        else
        for(i = 0; i < T ; i++)
        {
            if(strcmp(str[i].s,contry)==0)
            {str[i].num++;break;}
        }
        if(i == T)
        {
            strcpy(str[T].s,contry);
            str[T].num++;
            T++;
        }
    }
    if(T == 1)printf("%s %d\n",str[0].s,str[0].num);
    else
    {
        sort(str,str+T,cmp);
        for(i = 0; i < T; i++)
        {
            printf("%s %d\n",str[i].s,str[i].num);
        }
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值