文章

1

粉丝

163

获赞

0

访问

2.2k

头像
1261-字符串排序3 求大佬指点指点,为什么AC不了呀
我要提问
发布于2023年3月5日 15:47
阅读数 2.2k

#include<iostream>
#include<string.h>
#include<algorithm>
using namespace std;


typedef struct str {
    string s;
    int len;
} Str;

bool cmp(Str a, Str b) {
    return a.len < b.len;
}

int main() {
    int n;
    while(cin >> n) {
        Str str[n];
        string ss;
        getchar();   //用getchar()将换行符\n吃掉,否则用gerline()读字符串时会将换行符也读进去
        for(int i = 0; i < n; i++) {
            getline(cin, ss);
            if(ss == "stop") {
                break;
            }
            str[i].s = ss;
            str[i].len = ss.length();
        }
        ...

登录查看完整内容


登录后发布评论

3 条评论
admin SVIP
2023年3月5日 20:38

当中途有stop,你的代码break之后for循环还是会输出n行,就会多出很多空行

赞(0)

Lucy_59 : 回复 admin: 哦?谢谢回复。那要怎么修改呢?

2023年3月6日 10:22

Lucy_59 : 回复 admin: 我知道了,输入n行字符串,当最后输入stop时也是算入n中的,所以最后的输出应该要减一才行。然后解决办法就是用sum来统计输入的字符串(遇到stop就不统计)的个数,最后for循环只需要输出sum个就好了。

2023年3月6日 11:08