当前位置:网站首页>OpenJudge NOI 1.13 17:文字排版
OpenJudge NOI 1.13 17:文字排版
2022-06-11 02:04:00 【君义_noip】
【题目链接】
【题目考点】
1. 字符串
【解题思路】
解法1:保存一行已经占用的字符数
设h为当前行已经占用的字符数。
第一个单词直接加入,后面加入单词时要包括单词前面的空格,所以每次加入单词后,已经占用的字符增加:单词长度+1。
- 如果h为0,输出该单词,h只增加一个单词的长度。
- 如果h加上当前单词长度+1后,大于80个字符,则该单词应该放在下一行。换行,输出该单词,而后h变为当前单词长度。
- 其他情况,输出空格与该单词,h增加单词长度+1。
解法2:保存一行剩余的字符数
设r为当前行剩余的可以添加的字符数。
第一个单词直接加入,后面加入单词时要包括单词前面的空格,所以每次加入单词后,剩余的字符数减少:单词长度+1。
- 如果r为80,输出一个单词,r只减少一个单词的长度。
- 如果r减去当前单词长度+1后小于0,则该单词应该放在下一行。换行,输出该单词,而后r变为80减去当前单词长度。
- 其他情况,输出空格与该单词,r减少:单词长度+1。
可以一边遍历一边输出。也可以先构造字符串,最后输出。
【题解代码】
解法1:保存一行已经占用的字符数+构造字符串
#include<bits/stdc++.h>
using namespace std;
#define L 80 //一行长度
int main()
{
string s[1005], ans;
int n, h = 0;//h:已有字符数
cin >> n;
for(int i = 1; i <= n; ++i)
cin >> s[i];
for(int i = 1; i <= n; ++i)
{
if(h == 0)
{
ans = ans + s[i];
h += s[i].length();
}
else if(h+s[i].length()+1 <= L)
{
ans = ans + ' ' + s[i];
h += s[i].length()+1;
}
else//h+s.length()+1 > L
{
ans = ans + '\n' + s[i];
h = s[i].length();
}
}
cout << ans;
return 0;
}
解法2:保存一行剩余的字符数+直接输出
#include<bits/stdc++.h>
using namespace std;
#define L 80 //一行长度
int main()
{
string s;
int n, r = L;//r:剩余字符数
cin >> n;
for(int i = 1; i <= n; ++i)
{
cin >> s;
if(r == L)
{
cout << s;
r -= s.length();
}
else if(r >= s.length()+1)
{
cout << ' ' << s;
r -= s.length()+1;
}
else// r < s.length()+1
{
cout << endl << s;
r = L - s.length();
}
}
return 0;
}
边栏推荐
- Unity HTC and Pico are the same
- Flat data to tree and tree data flattening
- Kotlin apply方法
- Defense measures for common vulnerabilities
- When the interviewer opens his mouth, he comes to compose. Is this the case now?
- AOSP ~ Logcat Chatty 行过期
- One line of code solves the problem that the time to fetch datetime from MySQL database is less than eight hours
- AOSP ~ 默认开启开发者模式
- Colab reported an error: importerror: cannot import name '_ check_ savefig_ extra_ args‘ from ‘matplotlib. backend_ bases‘
- Why is the trend chart of precious metal silver strong?
猜你喜欢

完成千万元A轮融资,小象生活能否成为折扣界的“永辉”?

环糊精金属有机骨架(β-CD-MOF)装载二巯丁二酸/大黄素/槲皮素/三氯蔗糖/二氟尼柳/奥美拉唑(OME)

How to use phpMyAdmin to optimize MySQL database

Setting access to win10 shared folder without verification

One line of code solves the problem that the time to fetch datetime from MySQL database is less than eight hours

How to fix syntax errors in WordPress websites

Multilevel mesoporous organometallic framework material zif-8 loaded with lactic acid oxidase (LOD) / ferric oxide (Fe304) / doxorubicin / insulin /cas9 protein / metronidazole / emodin methyl ether

When the interviewer opens his mouth, he comes to compose. Is this the case now?

Three special data types, day3 and redis (geographic location, cardinality statistics and bitmap scene usage)
![[MySQL 45 -10] Lesson 10 how MySQL selects indexes](/img/eb/dbde2852a89ece383266da1aeea862.jpg)
[MySQL 45 -10] Lesson 10 how MySQL selects indexes
随机推荐
软件测试英语常见词汇
To view the data in redis, in addition to the command line and client, you have a third option
Fundamentals of deep learning [4] build easyocr and carry out simple character recognition from 0
[untitled]
Google Gmail mailbox marks all unread messages as read at once
【新晋开源项目】动态配置化任务编排框架 Gobrs-Async 加入Dromara开源社区
AOSP ~ Logcat Chatty 行过期
Kotlin apply方法
C language principle explanation and code implementation of scalable / reduced thread pool
年金保险理财产品可以复利吗?利率是多少?
【189. 轮转数组】
Databinding escaping with presentation symbols
Add SQL formatter to vscode to format SQL
【面试题 17.04. 消失的数字】
Kotlin let method
Istio安装与使用
Cyclodextrin metal organic framework( β- Cd-mof) loaded with dimercaptosuccinic acid / emodin / quercetin / sucralose / diflunisal / omeprazole (OME)
GCC C inline assembly
Tests logiciels vocabulaire commun anglais
List 过滤、排序、校验等处理方法