文章

9

粉丝

0

获赞

27

访问

357

头像
大整数排序 题解:卡80%原因
P1412 华中科技大学机试题
发布于2025年3月11日 00:05
阅读数 10

卡80卡了半天,没看见多组输入,多组输入数据一定要用while(cin >> n)这样的格式。

本题思路其实很简单,字符串长度一样,直接字典排序,用sort就可以解决,但长度不一样eg:333和11111111111,字典排序很明显11111111要更小,所以长度不一样就输出短的。数据类型不用说肯定是string,因为int会超限。

代码:有些繁琐,写的随心所欲了

#include<bits/stdc++.h>
using namespace std;
typedef struct{
    string score;
    int ls;
}str;
int cmp(str a,str b){
    if(a.ls == b.ls){
        return a.score < b.score;
    }
    else return a.ls < b.ls;
}
int main(){
    int n;
    while(cin >> n){
        str a[105];
    for(int i = 0;i < n;i++){
        cin >> a[i].score;
        a[i].ls = a[i].score.length();
    }    
    sort(a,a+n,cmp);
    for(int i = 0;i < n;i++){
   ...

登录查看完整内容


登录后发布评论

暂无评论,来抢沙发