Openjudge NOI题库1.13编程基础之综合应用17:文字排版
来源:互联网 发布:java.io.eofexception 编辑:程序博客网 时间:2024/06/10 15:18
- 总时间限制:
- 1000ms
- 内存限制:
- 65536kB
- 描述
给一段英文短文,单词之间以空格分隔(每个单词包括其前后紧邻的标点符号)。请将短文重新排版,要求如下:
每行不超过80个字符;每个单词居于同一行上;在同一行的单词之间以一个空格分隔;行首和行尾都没有空格。
- 输入
- 第一行是一个整数n,表示英文短文中单词的数目. 其后是n个以空格分隔的英文单词(单词包括其前后紧邻的标点符号,且每个单词长度都不大于40个字母)。
- 输出
- 排版后的多行文本,每行文本字符数最多80个字符,单词之间以一个空格分隔,每行文本首尾都没有空格。
- 样例输入
84One 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 fourchildren they could "buy" a cone from me for a hug. Almost immediately, the kidslined 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 endof the line finally got his turn to "buy" his ice cream, he gave me two hugs."Keep the changes," he said with a smile.
思路:因为string能够存一串,长度好计算,所以就不用char类型了,哪几个求长度的函数我还老记不住,干脆就用string,输入的时候不需要考虑回车还是空格,定义string类型的a【1000】,然后k记录数组a里面有多少个单词,开始循环,因为一行最多八十个字符嘛,然后用sum来记录当前已经有多少字符了,每次都要a【i】.length+sum,当然因为有空格,每次打印空格的时候sum还要+1,如果sum+=a[i].length()>80;说明不能在这一行打印,要转到下一行,于是打印回车,然后打印该单词,此时sum要变成该单词的长度,又因为此时我代码中打印了空格,所以要+1,这次这题让我终于记住了size和length求长度的区别,length是字符,size就是你输入了几个。可以自己写个程序试试。
#include<iostream>#include<cstdlib>#include<algorithm>#include<iomanip>#include<math.h>#include<cstdio>using namespace std;int main(){int i,k=0,n,sum=0;string a[1000],dc;cin>>n;while (cin>>dc) a[k++]=dc;for(i=0;i<k;i++) { sum+=a[i].length(); if(sum<=80) { cout <<a[i]<<' '; sum++; } else if(sum>80) {printf("\n");cout<<a[i]<<' ';sum=a[i].length()+1; }}}
0 0
- Openjudge NOI题库1.13编程基础之综合应用17:文字排版
- Openjudge NOI题库1.13编程基础之综合应用12:分数求和
- Openjudge NOI题库1.11编程基础之二分查找04 网线管理
- Openjudge NOI题库1.11编程基础之二分查找07 和为给定数
- Openjudge NOI题库1.7编程基础之字符串 34:回文子串
- NOI题库1.11编程基础之二分查找 矩形分割
- OpenJudge NOI题库 6266:取石子游戏
- 11:回文素数( 1.13编程基础之综合应用)
- Openjudge NOI题库2.4基本算法之分治 7620:区间合并
- NOI题库之二分 派
- NOI.OPENJUDGE 1.13.02 感想
- NOI.OPENJUDGE 1.13.20感想
- NOI.OPENJUDGE 1.13.26 感想
- 41:判断元素是否存在(编程基础之综合应用)
- NOI题解(1.1编程基础之输入输出)
- 10:判决素数个数(1.13编程基础之综合应用)
- 05:素数回文数的个数(1.13编程基础之综合应用)
- css基础-文字排版
- 自动增量_自动升级系统_nsAutoUpdate_NSIS_插件发布
- 关于贪心算法
- 自定义 mvc
- 基础拾遗------特性详解【含常用过滤器实例】
- spark sql和DataFrame本质
- Openjudge NOI题库1.13编程基础之综合应用17:文字排版
- bootstrap 模态框modal 传值问题
- maven javadoc plugin build fails with Java 8 when Javadoc tags are incomplete
- 安装YouCompleteMe
- spark sql on hive配置及其使用
- Android View的scrollTo(),scrollBy(),getScrollX(), getScrollY()
- 归并排序算法(php)
- How to get port in FTP protocol from passive mode?
- IEueditor无法使用-window.localstorage报错