当前位置:网站首页>Openjudge noi 1.13 17: text layout
Openjudge noi 1.13 17: text layout
2022-06-11 02:49:00 【Junyi_ noip】
【 Topic link 】
OpenJudge NOI 1.13 17: Typesetting
【 Topic test site 】
1. character string
【 Their thinking 】
solution 1: Save the number of characters that a line has occupied
set up h Is the number of characters occupied by the current line .
Add the first word directly , Include the space in front of the word when adding the word , So every time you add a word , The occupied characters are increased : Word length +1.
- If h by 0, Output the word ,h Just increase the length of one word .
- If h Add the current word length +1 after , Greater than 80 Characters , Then the word should be placed on the next line . Line break , Output the word , Then h Change to the current word length .
- Other situations , Output the space and the word ,h Increase word length +1.
solution 2: Save the number of characters remaining in a line
set up r The number of characters remaining for the current line that can be added .
Add the first word directly , Include the space in front of the word when adding the word , So every time you add a word , The number of remaining characters is reduced : Word length +1.
- If r by 80, Output a word ,r Reduce the length of only one word .
- If r Subtract the current word length +1 After less than 0, Then the word should be placed on the next line . Line break , Output the word , Then r Turn into 80 Subtract the current word length .
- Other situations , Output the space and the word ,r Reduce : Word length +1.
You can output while traversing . You can also construct a string first , The final output .
【 Solution code 】
solution 1: Save the number of characters that a line has occupied + Construct string
#include<bits/stdc++.h>
using namespace std;
#define L 80 // One line length
int main()
{
string s[1005], ans;
int n, h = 0;//h: Number of existing characters
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;
}
solution 2: Save the number of characters remaining in a line + Direct output
#include<bits/stdc++.h>
using namespace std;
#define L 80 // One line length
int main()
{
string s;
int n, r = L;//r: Number of characters remaining
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;
}
边栏推荐
- Google Gmail mailbox marks all unread messages as read at once
- Introduction to the functions of today's headline search webmaster platform (portal)
- How to state clearly and concisely the product requirements?
- Technology sharing | quick intercom, global intercom
- CPT 102_LEC 13-14
- Cyclodextrin metal organic framework( β- Cd-mof) loaded with dimercaptosuccinic acid / emodin / quercetin / sucralose / diflunisal / omeprazole (OME)
- cmake常用命令
- If you understand the logic of mining and carbon neutrality, you will understand the 100 billion market of driverless mining areas
- [MySQL 45 -10] Lesson 10 how MySQL selects indexes
- Project - redis message queue + worker thread fetches user operation logs and stores them (2)
猜你喜欢

Google Gmail mailbox marks all unread messages as read at once

WordPress article directory plug-in luckywp table of contents setup tutorial

AOSP ~ 修改WebView默认实现

APP测试_测试点总结

Uni app - one click access to user information

Common vocabulary of software testing English

银行选择电子招标采购的必要性

AOSP ~ modify WebView default implementation

How to guarantee the data quality of data warehouse?

When a logical deletion encounters a unique index, what are the problems and solutions?
随机推荐
20220610 星期五
[AI weekly] AI and freeze electron microscopy reveal the structure of "atomic level" NPC; Tsinghua and Shangtang proposed the "SIM" method, which takes into account semantic alignment and spatial reso
App test_ Summary of test points
[C language classic]: inverted string
Kotlin let method
Flat data to tree and tree data flattening
Uni app - one click access to user information
Baidu submits sitemap to prompt the solution of "index type is not handled"
Unity HTC and Pico are the same
How can Delma's own brand "take off" when Philips is listed on the market?
Multilevel mesoporous organometallic framework material zif-8 loaded with lactic acid oxidase (LOD) / ferric oxide (Fe304) / doxorubicin / insulin /cas9 protein / metronidazole / emodin methyl ether
WordPress article directory plug-in luckywp table of contents setup tutorial
Jetpack compose scaffold and topappbar (top navigation)
剑指 Offer II 079. 所有子集
Metal organic framework MOF Al (Diba), MOF Zr (Diba), MOF Fe (Diba) loaded with curcumin / carboxybenzylpenicillin /mtx methotrexate / paclitaxel ptx/ DOX / cisplatin cddp/cpt camptothecin and other d
Project - redis message queue + worker thread fetches user operation logs and stores them (2)
Cmake common commands
年金保險理財產品可以複利嗎?利率是多少?
码农的进阶之路 | 每日趣闻
MySQL backup and recovery