文字排版 (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.
其它解法参考:
http://blog.csdn.net/panda711/article/details/50750620
来源: 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
相关文章推荐
- 分配病房 (Coursera 程序设计与算法 专项课程2 C程序设计进阶 李戈;OpenJudge)
- 递归 单词翻转 (Coursera 程序设计与算法 专项课程2 C程序设计进阶 李戈;OpenJudge)
- 递归 排队游戏 (Coursera 程序设计与算法 专项课程2 C程序设计进阶 李戈;OpenJudge)
- 配对碱基链 (Coursera 程序设计与算法 专项课程2 C程序设计进阶 李戈;OpenJudge)
- 计算矩阵边缘元素之和 (Coursera 程序设计与算法 专项课程2 C程序设计进阶 李戈;OpenJudge)
- 二维数组右上左下遍历 (Coursera 程序设计与算法 专项课程2 C程序设计进阶 李戈;OpenJudge)
- 期末考试 编程题#6:MyString(Coursera 程序设计与算法 专项课程3 C++程序设计 郭炜、刘家瑛;OpenJudge)
- 动态规划(1) 编程题#1:集合加法(Coursera 程序设计与算法 专项课程4 算法基础 郭炜、刘家瑛;map容器插入元素)
- 标准模板库 STL-1 编程题#1(Coursera 程序设计与算法 专项课程3;用法:迭代器 ostream_iterator,容器 set)
- ** 动态规划(2) 编程题#2: Charm Bracelet(Coursera 程序设计与算法 专项课程4;#define maxN 3402 此处没有 ; 号!)
- * 继承与派生 编程题#1(Coursera 程序设计与算法 专项课程3 C++程序设计 郭炜、刘家瑛;OpenJudge)
- 标准模板库 STL-1 编程题#2 List(Coursera 程序设计与算法 专项课程3 C++程序设计 郭炜、刘家瑛;OpenJudge)
- 期末考试 编程题#3:计算数列平方和(Coursera 程序设计与算法 专项课程3 C++程序设计 郭炜、刘家瑛;函数对象作参数)
- 文件操作与模板 编程题#2: 实数的输出格式(Coursera 程序设计与算法 专项课程3 C++程序设计 郭炜、刘家瑛;OpenJudge)
- * 标准模板库 STL-2 编程题#2(Coursera 程序设计与算法 专项课程3 C++程序设计;自定义迭代器 CMyistream_iterator)
- 运算符重载 编程题#1(Coursera 程序设计与算法 专项课程3 C++程序设计;重载:& operator =, operator =)
- 类和对象 编程题#2(Coursera 程序设计与算法 专项课程3 C++程序设计 郭炜、刘家瑛;OpenJudge)
- ? 枚举 编程题#2:拨钟问题(Coursera 程序设计与算法 专项课程4;函数memcpy的用法,switch case break 语句!)
- ** 标准模板库 STL-2 编程题#4: 字符串操作(Coursera 程序设计与算法 专项课程3 C++程序设计 郭炜、刘家瑛;OpenJudge)
- * 期末考试 编程题#4:计算整数平方和(Coursera 程序设计与算法 专项课程3 C++程序设计 郭炜、刘家瑛;OpenJudge)