首页 > 其他分享 >OpenJudge | 文字排版

OpenJudge | 文字排版

时间:2024-07-27 21:57:26浏览次数:8  
标签:文字 me his cones OpenJudge str 排版 summaryLine gave

总时间限制: 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.

标签:文字,me,his,cones,OpenJudge,str,排版,summaryLine,gave
From: https://blog.csdn.net/qq_21739599/article/details/140741999

相关文章

  • ValueError:在带有 GATT 的 Python 中,以 16 为基数的 int() 的文字无效:b'0f 18 '
    我正在使用Python和GATT库pxexpect来处理一些数据,但在尝试将十六进制值转换为整数时遇到问题。这是我看到的具体错误:print(int(gatt.before,16)),^^^^^^^^^^^^^^^^^^^^ValueError:invalidliteralforint()withbase16:b'0f18'这是产生错误的代......
  • 如何注释用文字初始化的“OrderedDict”的类型?
    假设我有以下内容:fromcollectionsimportOrderedDictfromdataclassesimportdataclass@dataclassclassHelloWorld:x:OrderedDict[str,int]a=OrderedDict([("a",0),("c",2),("b",1)])HelloWorld(a)<---#typeerro......
  • imgui中文字体库导入乱码问题
    1.中文字体库导入乱码问题1.修改clion中的文件编码配置2.按住Ctrl+Shift+Alt+/,选中Registry…,然后取消run.processes.with.pty3.cmakelist.txt导入字体文件,注意不要加u8了,不然会乱码#字体文件include_directories(${PROJECT_SOURCE_DIR}/include/fonts)然后mai......
  • Python从零开始制做文字游戏(荒岛求生)
    文章目录前言开发游戏《荒岛求生》游戏大纲背景内容通关条件游戏过程探索荒岛购买物资休息总结代码开发定义变量当前代码引入背景故事当前代码循环问题解决:函数当前代码制作延时当前代码制作a函数(探索荒岛阶段)展示数......
  • 如何使用Python实现语音转文字/字幕
    文章目录......
  • 使用SpringAI框架实现文字生成图片壁纸:深入探索与实战
    使用SpringAI框架实现文字生成图片壁纸:深入探索与实战在当今的技术世界中,人工智能(AI)已经成为了一个热门话题。无论是自然语言处理、图像识别还是生成对抗网络(GAN),AI的应用场景无处不在。今天,我们将深入探讨如何使用SpringAI框架来实现一个有趣的功能:根据文字生成图片壁纸。什么是......
  • 在 Python 中动态定义文字字符串排列的并集
    我有一个字符串列表:strings=['a','b','c']我想声明列表中所有可能的有序对的Union类型。硬编码,这看起来像:Literal我如何动态定义CustomType=Literal['ab','ac','aa','ba','bb','bc�......
  • uin-app中文字转语音(可实现支付成功语音播报)
    1.插件介绍MT-TTS离线语音合成MT-TTS离线语音合成https://ext.dcloud.net.cn/plugin?id=36821.1下载直接下载离线打包,导入项目中2.使用  2.1下载,解压,打开hbuiderx中的项目,项目目录下创建nativeplugins文件夹,把解压的文件MT-TTS放到nativeplugins文件夹下。 ......
  • OpenJudge | 扩号匹配问题
    总时间限制:1000ms内存限制:65536kB描述在某个字符串(长度不超过100)中有左括号、右括号和大小写字母;规定(与常见的算数式子一样)任何一个左括号都从内到外与在它右边且距离最近的右括号匹配。写一个程序,找到无法匹配的左括号和右括号,输出原来字符串,并在下一行标出不能匹配......
  • WPS入门(一、文件,文字,段落、样式)
    一、总述在打开WPS后我们将看到工如下的图。这次主要从文件,文字,段落入手,讲述如何运用WPS二、文件打开文件后:这里主要有两个比较重要的功能,即上述画上了红方框的功能。1、输出为PDF点击输出为PDF后,如下页面:此处再讲解一下PDF文档设置情况。当点击设置后,如图:这里要......