后知后觉


私信TA

用户名:2015416576

访问量:10943

签 名:

人总是要学习的,颓废了好久,现在又要重新上路了.....

等  级
排  名 5658
经  验 1449
参赛次数 0
文章发表 12
年  龄 21
在职情况 学生
学  校 qfnu
专  业 计算机科学与技术

  自我简介:

小白一枚,希望能遇到大神带我刷题。

解题思路:

这个题的关键还是输入上,定义一个n,然后输入字符串,对于输入的每个字符串,只要是前n个就原样输出,超过n的以空格符为界,把完整的字符串分割开。用一个for循环遍历字符串即可,为什么用C++呢,因为C++中用一个字符串的类型,可以直接接受字符串。



注意事项:


字符串的输入还有格式。输入可以用getline 格式对于每个输入,后面有两个换行。


参考代码:

#include <iostream>

#include <cstring>

#include <stdio.h>

using namespace std;


int main()

{

    string str;

    int cou = 0;

    int n;

    cin >> n;

    getchar();

    while(getline(cin,str))

    {

        if (cou>=n)

        {

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

            {

                if(str[i]==' ')

                {

                   i++;

                   cout << endl << endl;

                }


                cout << str[i];

            }

            cout << endl << endl;

        }

        else

            cout << str << endl << endl;


        cou++;

        //cout << cou<<endl;


    }

    return 0;

}


 

0.0分

0 人评分

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

编程语言转换

万能编程问答

代码解释器

  评论区