文章

34

粉丝

0

获赞

263

访问

4.6k

头像
字符串排序3 题解:
P1261 北京大学机试题
发布于2026年3月9日 14:36
阅读数 69

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

int main(){
    int n;
    char s[100][100];
    while(scanf("%d",&n)!=EOF){
        getchar();
        int cou=0;
        for(int i=0;i<n;i++){
            gets(s[i]);
            if(strcmp(s[i],"stop")==0) break;
            cou++;
        }
        for(int i=0;i<cou-1;i++){
            for(int j=0;j<cou-1-i;j++){
                if(strlen(s[j])>strlen(s[j+1])){
                    char ch[100];
                    strcpy(ch,s[j]);
                    strcpy(s[j],s[j+1]);
                    strcpy(s[j+1],ch);
                }
            }
        }
        for(int i=0;i<cou;i++){
            printf("%s\n",s[i]);
        }
    }
    return 0;
}

 

登录查看完整内容


登录后发布评论

暂无评论,来抢沙发