文字排版

来源:互联网 发布:linux dd命令 编辑:程序博客网 时间:2024/05/16 05:33


描述

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

每行不超过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 upto make their purchases. The three youngest each gave me a quick hug, grabbedtheir cones and raced back outside. But when my teenage son at the end of theline finally got his turn to "buy" his ice cream, he gave me twohugs. "Keep the changes," he said with a smile.

输出示例】

Onesweltering 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 maketheir purchases. The three youngest each gave me a quick hug,

grabbed their conesand raced back outside. But when my teenage son at the end

of the line finallygot his turn to "buy" his ice cream, he gave me two hugs.

"Keep thechanges," he said with a smile.

C代码】

---------------

#include<stdio.h>

#include<string.h>

#defineWORD_SIZE 40

#defineLINE_SIZE 80

intmain(void) {

char word[WORD_SIZE + 1];

int length = 0;

int n, t;

scanf("%d", &n);

t = n;

while(n > 0) {

scanf("%s", word);

if(length + strlen(word) + 1 <= LINE_SIZE && n != t) {

printf(" ");

++length;

}

else if(length + strlen(word) + 1 > LINE_SIZE) {

printf("\n");

length = 0;

}

length += strlen(word);

printf("%s",word);        

--n;

}

return 0;

}

0 0