首页 > 代码库 > 1.13.17

1.13.17

17:文字排版

描述

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

每行不超过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.
 1 #include <cstdio>
 2 #include <cstring>
 3 using namespace std;
 4 char m[50];
 5 int n,tot,x;
 6 int main()
 7 {
 8     scanf("%d",&n);
 9     while(++x<=n)
10     {
11         scanf("%s",m);
12         tot+=strlen(m);
13         if(tot!=80) tot++;
14         if(tot>80)
15         {
16             printf("\n");
17             tot=strlen(m)+1;
18         }
19         printf("%s",m);
20         if(tot!=80&&x!=n) printf(" ");
21         memset(m,0,sizeof(m));
22     }
23     return 0;
24 }

 

1.13.17