1. 程式人生 > >文字排版

文字排版

iat 大於 單詞 col section 英文單詞 多行文本 imm amp

文字排版

發布時間: 2017年10月27日 10:15 最後更新: 2017年10月29日 20:40 時間限制: 1000ms 內存限制: 128M

給一段英文短文,單詞之間以空格分隔(每個單詞包括其前後緊鄰的標點符號)。請將短文重新排版,要求如下:

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

輸入樣例以空格分割,未換行
一定註意換行要寫在前面
要不就空格了
還有一定是else if 不是 if
if會導致每行開頭多出來個空格


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
int n;
scanf("%d",&n);
char a[41]={0};
int sum=0,i;
for(i=0;i<n;i++)
{
scanf("%s",&a);
if(sum+1+strlen(a)>80)
{
printf("\n");
sum=0;
}
if(i>0)
{
printf(" ");
sum++;
}
printf("%s",a);
sum=sum+strlen(a);
}
return 0;
}

文字排版