解题思路:
这个题的关键还是输入上,定义一个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 人评分
震宇大神的杀毒软件 (C语言代码)浏览:1241 |
C语言训练-计算t=1+1/2+1/3+...+1/n (C语言代码)浏览:869 |
分糖果 (C++代码)浏览:1447 |
C语言程序设计教程(第三版)课后习题6.7 (C语言代码)浏览:520 |
计算质因子 (C++代码)浏览:1645 |
C语言程序设计教程(第三版)课后习题6.6 (C语言代码)浏览:624 |
C语言程序设计教程(第三版)课后习题7.3 (C语言代码)浏览:1195 |
简单的a+b (C语言代码)浏览:606 |
关于float,double变量的几点说明浏览:1810 |
Hello, world! (C语言代码)浏览:826 |