您的位置:首页 > 运维架构

文字排版 (Coursera 程序设计与算法 专项课程2 C程序设计进阶 李戈;OpenJudge)

2017-08-21 20:44 316 查看
编程题#3:文字排版

来源: POJ (http://pkuic.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;

int main() {
char a[1000][41];
int n, sum = 0;

cin >> n;
for (int i = 0; i < n; i++)
cin >> a[i];

sum += strlen(a[0]);
cout << a[0] << ' ';

for (int i = 1; i < n; i++){
sum += strlen(a[i]) + 1;
if (sum > 80){
cout << endl;
sum = 0;
sum += strlen(a[i]);
}
cout << a[i] << ' ';
}
cout << endl;

return 0;
}


其它解法参考:

http://blog.csdn.net/panda711/article/details/50750620
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐