17: 文字排版最近的提交

来源:互联网 发布:ip域名查询电信 编辑:程序博客网 时间:2024/05/14 02:26
描述

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

每行不超过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.

代码

 
 1 #include<cstdio> 2 #include<string> 3 #include<cstring> 4 #include<iostream> 5 //#include<algorithm>#include<cstdlib>#include<cmath> 6 using namespace std; 7 char a[40],t[40]; 8 int main() 9 {10     //freopen("in.txt","r",stdin);11     //freopen("out.txt","w",stdout);12     int tlen=0,n;13     bool w=1;14     cin>>n;15     while(~scanf("%s",a))16     {17         tlen+=strlen(a);18         if(tlen<=80)19         {20             tlen++;21         }22         else23         {24             cout<<endl;25             w=1;26             tlen=strlen(a)+1;27         }28         if(w)29         {30             printf("%s",a);31             w=0;    32         }33         else printf(" %s",a);34     }35     return 0;    36 }

 

 
0 0