温姑娘


私信TA

用户名:151210107

访问量:6794

签 名:

等  级
排  名 6460
经  验 1358
参赛次数 1
文章发表 6
年  龄 0
在职情况 学生
学  校 平顶山学院
专  业

  自我简介:

解题思路:

先输入N,在用多实例输入输入字符串,用cnt来统计当前输入了多少个字符串,如果cnt<=n,说明当前字符串应该原样输出,否则就要遍历整个字符串,用tmp变量来保存分割出的字符串,所以其最初初始化为空,没遇到一个非空字符就链接上该字符,当遇到空格,我们需要检测tmp是否为空串,因为空串可能是由两个连续空格造成的,所以输出时的检测很重要。



注意事项:
输出每个字符串时,后面要输出两个换行。




参考代码:

#include <iostream>

#include <stdio.h>


using namespace std;


int main() {

    int n,cnt;

    cin>>n;

    cnt = 0;

    string s;

    getchar();

    while(getline(cin,s)) {

        cnt++;

        if(cnt <= n) {

            cout<<s<<endl<<endl;

        }

        else {

            string tmp="";

            int j = 0;

            for(int i = 0; i < (int)s.length(); i++) {

                if(s[i]!=' ') {

                    tmp += s[i];

                }

                else {

                    if(tmp == "") {

                        continue;

                    }

                    else {

                        cout<<tmp<<endl<<endl;

                    }

                    tmp = "";

                }

            }

            if(tmp != "") {

               cout<<tmp<<endl<<endl;

            }


        }

    }

    return 0;

}


 

0.0分

4 人评分

看不懂代码?想转换其他语言的代码? 或者想问其他问题? 试试问问AI编程助手,随时响应你的问题:

编程语言转换

万能编程问答  

代码解释器

代码纠错

SQL生成与解释

  评论区