洛谷 P1012 拼数

15 篇文章 0 订阅
15 篇文章 0 订阅

洛谷 P1012 拼数

真——手写字符串的排序比较函数

这个题目,因为之前没有把更长的字符串后面剩余的字符与另一个字符串的首字母进行比较,所以最后一个点WA了

#include <bits/stdc++.h>
using namespace std;
int n;
struct op{
    char st[100001];
};
op a[21];
bool cmp(op x,op y){
    int l1=strlen(x.st),l2=strlen(y.st);
    int tp=0;
    while(tp<min(l1,l2)){
        if(x.st[tp]>y.st[tp])
            return 1;
        else if(x.st[tp]<y.st[tp])
            return 0;
        ++tp;
    }//比较两个字符串长度重合部分的字符大小
    if(l1<l2){
        while(tp<l2){
            if(x.st[0]>y.st[tp])
                return 1;
            else if(x.st[0]<y.st[tp])
                return 0;
            ++tp;
        }
    }
    else if(l1>l2){
        while(tp<l1){
            if(y.st[0]<x.st[tp])       
                return 1;
            else if(y.st[0]>x.st[tp])
                return 0;
            ++tp;
        }
    }//分别讨论两个字符串更长的情况,并逐个比较字符串
return 1;
}//手写排序函数
int main(){
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
        scanf("%s",a[i].st);
    sort(a+1,a+1+n,cmp);
    for(int i=1;i<=n;i++)
        printf("%s",a[i].st);
return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值