OpenJudge | 文字排版

总时间限制: 1000ms 内存限制: 65536kB

描述

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

每行不超过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. string str;定义字符串
  2. int n;定义整数
  3. cin >> n;输入整数
  4. int summaryLine = 0;定义行字符数
  5. for循环接收字符串,这里用了cin >> str;只能接收一个单词,如果有标点符号则会被分开的特性。
  6. if判断是否超过80个字符,是则换行,否则输出字符串。注意:str.size()+1是字符串长度加上一个空格的长度,此时设summaryLine为字符串长度。
  7. else if判断是否为第一个字符串,是则输出字符串,否则输出空格和字符串,并更新summaryLine
  8. else输出空格和字符串,并更新summaryLine

Code

C++ STL

#include <bits/stdc++.h>
using namespace std;

int main() {
	string str;
	int n;
	cin >> n;
	int summaryLine = 0;
	for(int i = 0; i < n; i++) {
		cin >> str;
		if(str.size()+1 + summaryLine > 80) {	// str.size()+1 is the length of str and backspace
			cout << endl; cout << str; summaryLine = str.size(); 
		} else if(summaryLine == 0){cout << str; summaryLine += str.size();} 
		else {cout << " " << str; summaryLine += str.size()+1;}
	}
}

C

#include <stdio.h>
#include <string.h>

int main() {
    char str[41];
    int n;
    scanf("%d", &n);
    int summaryLine = 0;
    for(int i = 0; i < n; i++) {
        scanf("%s", str);
        if(strlen(str)+1 + summaryLine > 80) {	// strlen(str)+1 is the length of str and backspace
            printf("\n"); printf("%s", str); summaryLine = strlen(str); 
        } else if(summaryLine == 0){printf("%s", str); summaryLine += strlen(str);} 
        else {printf(" %s", str); summaryLine += strlen(str)+1;}
    }
}

C++

#include <bits/stdc++.h>
using namespace std;

int main() {
    char str[41];
    int n;
    scanf("%d", &n);
    int summaryLine = 0;
    for(int i = 0; i < n; i++) {
        scanf("%s", str);
        if(strlen(str)+1 + summaryLine > 80) {	// strlen(str)+1 is the length of str and backspace
            printf("\n"); printf("%s", str); summaryLine = strlen(str); 
        } else if(summaryLine == 0){printf("%s", str); summaryLine += strlen(str);} 
        else {printf(" %s", str); summaryLine += strlen(str)+1;}
    }
}

测试用例

测试用例1

输入
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.

测试用例2

输入
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.

测试用例3

输入
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.
  • 23
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Mryan2005

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值