文章

93

粉丝

57

获赞

604

访问

168.1k

头像

字符串排序3 题解:暴力结构体(无容器)

P1261 北京大学机试题
发布于 2025年3月
阅读数 2.2k

#include <bits/stdc++.h>
using namespace std;

struct node
{
    string str;
    int cd;
}jilu[10005];

bool cmp(node a,node b)
{
    return a.cd<b.cd;
}

int main()
{
    int n;
    while(cin>>n)
    {
        memset(jilu,0,sizeof(jilu));
        int count=0;
        while(n--)
        {
            string str;
            if(count==0)cin.ignore(numeric_limits<streamsize>::max(), '\n');
            getline(cin,str);
            if(str=="stop")goto label;
            else
            {
                jilu[count].str=str;
                jilu[count].cd=str.size();
                count++;
            }
        }
        label:
        sort(jilu,jilu+count,cmp);
        for(int i=0;i<count;i++)
        {
            cout<<jilu[i].str<<endl;
        }
    }
    return 0;
    
}



登录后发布评论

暂无评论,来抢沙发