C++-文字排版 C++编程 调试?

发布于 2017-06-15 07:45:52 字数 2422 浏览 1305 评论 1

描述
给一段英文短文,单词之间以空格分隔(每个单词包括其前后紧邻的标点符号)。请将短文重新排版,要求如下:

每行不超过80个字符;每个单词居于同一行上;在同一行的单词之间以一个空格分隔;行首和行尾都没有空格。

输入
第一行是一个整数n,表示英文短文中单词的数目. 其后是n个以空格分隔的英文单词(单词包括其前后紧邻的标点符号,且每个单词长度都不大于40个字母)。
输出
排版后的多行文本,每行文本字符数最多80个字符,单词之间以一个空格分隔,每行文本首尾都没有空格。
样例输入:
84
One sweltering day, I was scooping ice cream into cones and told my four children they could "buy" a cone from me for a hug. Almost immediately, the kids lined up to make their purchases. The three youngest each gave me a quick hug, grabbed their cones and raced back outside. But when my teenage son at the end of the line finally got his turn to "buy" his ice cream, he gave me two hugs. "Keep the changes," he said with a smile.
样例输出:
One sweltering day, I was scooping ice cream into cones and told my four
children they could "buy" a cone from me for a hug. Almost immediately, the kids
lined up to make their purchases. The three youngest each gave me a quick hug,
grabbed their cones and raced back outside. But when my teenage son at the end
of the line finally got his turn to "buy" his ice cream, he gave me two hugs.
"Keep the changes," he said with a smile.
我的解决方案:

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

int main()
{
char words[100][40];//用于保存每一个单词
int wordsLen[100];// 记录每一个单词的长度
int n;// 需要处理的单词总数
cin>>n;
//输入单词数据,处理后得到每个单词的长度
for(int i=0;i<n;i++)
{
cin>>words[i];
wordsLen[i]=strlen(words[i]);
}

//先输出第一个单词
int length=wordsLen[0];
cout<<words[0];
for(int i=1;i<n;i++)
{
//如果该单词,连同前面的一个空格加入后不换行,则输出空格和该单词
if(length+1+wordsLen[i]<=80)
{
length=length+1+wordsLen[i];
cout<<" "<<words[i];
}
else
{
//该单词不能在本行输出了,如果length=80自动换行,否则要手动换行。
if(length<80)
{
cout<<endl;
}
//输出下一行第一个单词,重新对下一行的输出字符长度进行统计。
length=wordsLen[i];
cout<<words[i];
}
}
cout<<endl;
return 0;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜无邪 2017-07-11 12:50:58

你写的程序如果不考虑算法的话基本没有问题,有两个错误点:
1.char words[100][40];//用于保存每一个单词
int wordsLen[100];// 记录每一个单词的长度
题目中没有说明最大单词数目为100,不要想当然。

if(length<80)
{
cout<<endl;
}
判断条件画蛇添足,此处没有手动换行和自动换行直说,直接输出就行。

我的Accept代码:

/**
* http://ica.openjudge.cn/zz/3
* 文字排版
* 总时间限制: 1000ms 内存限制: 65536kB
* 描述给一段英文短文,单词之间以空格分隔(每个单词包括其前后紧邻的标点符号)。
* 请将短文重新排版,要求如下:
* 每行不超过80个字符;每个单词居于同一行上;在同一行的单词之间以一个空格分隔;行首和行尾都没有空格。
* 输入
* 第一行是一个整数n,表示英文短文中单词的数目. 其后是n个以空格分隔的英文单词(单词包括其前后紧邻的标点符号,且每个单词长度都不大于40个字母)。
* 输出
* 排版后的多行文本,每行文本字符数最多80个字符,单词之间以一个空格分隔,每行文本首尾都没有空格。
* 样例输入
* 84
* One sweltering day, I was scooping ice cream into cones and told my four children they could "buy" a cone from me for a hug. Almost immediately, the kids lined up to make their purchases. The three youngest each gave me a quick hug, grabbed their cones and raced back outside. But when my teenage son at the end of the line finally got his turn to "buy" his ice cream, he gave me two hugs. "Keep the changes," he said with a smile.
* 样例输出
* One sweltering day, I was scooping ice cream into cones and told my four
* children they could "buy" a cone from me for a hug. Almost immediately, the kids
* lined up to make their purchases. The three youngest each gave me a quick hug,
* grabbed their cones and raced back outside. But when my teenage son at the end
* of the line finally got his turn to "buy" his ice cream, he gave me two hugs.
* "Keep the changes," he said with a smile.
*
*/
#include <iostream>
#include <cstring>

using namespace std;

#define MAX_WORD_NUMBER 1000
#define MAX_WORD_LENGHT 40
#define MAX_CHAR_PER_LINE 80

int main()
{
char words[MAX_WORD_NUMBER][MAX_WORD_LENGHT]; //用于保存每一个单词
int wordsLen[MAX_WORD_NUMBER]; // 记录每一个单词的长度
int n; // 需要处理的单词总数
cin >> n;
for (int i = 0; i < n; i++) //输入单词数据,处理后得到每个单词的长度
{
cin >> words[i];
wordsLen[i] = strlen(words[i]);
}

//先输出第一个单词
int length = wordsLen[0];
cout << words[0];
for (int i = 1; i < n; i++)
{
//如果该单词,连同前面的一个空格加入后不换行,则输出空格和该单词
if (length + 1 + wordsLen[i] <= MAX_CHAR_PER_LINE)
{
length = length + 1 + wordsLen[i];
cout << " " << words[i];
}
else
{
cout << endl;
length = wordsLen[i];
cout << words[i];
}
}
cout << endl;
return 0;
}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文