c++left right 和 setw() 函数的用法Alignment of Code

2024-02-11 10:48

本文主要是介绍c++left right 和 setw() 函数的用法Alignment of Code,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

首先先讲一讲这个函数的头文件是#include<iomanip>。

setw(5) -- 设打印可用宽度为5
left -- 打印不足5个字时,输出靠左放,右边填空白,凑足宽度5
例如:
357 打印 出: 357空格空格
如果用 << right <<setw(5)<<357;
357 打印 出:空格空格357, right--靠右放

用left讲道题目吧



You are working in a team that writes Incredibly Customizable Programming Codewriter (ICPC) which
is basically a text editor with bells and whistles. You are working on a module that takes a piece of
code containing some definitions or other tabular information and aligns each column on a fixed vertical
position, while keeping the resulting code as short as possible, making sure that only whitespaces that
are absolutely required stay in the code. So, that the first words on each line are printed at position
p1 = 1; the second words on each line are printed at the minimal possible position p2, such that all first
words end at or before position p2 − 2; the third words on each line are printed at the minimal possible
position p3, such that all second words end at or before position p3 − 2, etc.
For the purpose of this problem, the code consists of multiple lines. Each line consists of one or
more words separated by spaces. Each word can contain uppercase and lowercase Latin letters, all
ASCII punctuation marks, separators, and other non-whitespace ASCII characters (ASCII codes 33 to
126 inclusive). Whitespace consists of space characters (ASCII code 32).
Input
The input file contains one or more lines of the code up to the end of file. All lines (including the last
one) are terminated by a standard end-of-line sequence in the file. Each line contains at least one word,
each word is 1 to 80 characters long (inclusive). Words are separated by one or more spaces. Lines of
the code can have both leading and trailing spaces. Each line in the input file is at most 180 characters
long. There are at most 1000 lines in the input file.
Output
Write to the output file the reformatted, aligned code that consists of the same number of lines, with
the same words in the same order, without trailing and leading spaces, separated by one or more spaces
such that i-th word on each line starts at the same position pi
.
Note for the Sample:
The ‘⊔’ character in the example below denotes a space character in the actual files (ASCII code
32).
Sample Input
start: integer; // begins here
stop: integer; // ends here
s: string;
c: char; // temp



Sample Output
start: integer; // begins here
stop: integer; // ends here
s: string;
c: char; // temp


思路:利用string字符串中不能空格,所以统计每行分成了多少节并进行编号。如第一行有五节,第二行有5节,第三行有两节,第四行有三节,并编号。 之后统计每节中最长的并记录。最后输出的时候每行输出宽度为每节最长的长度+1;因为每行的最后一节后面没有空格,所以可以单独输出。

代码如下:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<sstream>
#include<iomanip>using namespace std;
int main()
{int maxline[1000];int num[1000];int r,c,i,j;string a[1010][100];string line;memset(maxline,0,sizeof(maxline));memset(num,0,sizeof(num));r=0,c=0;while(getline(cin,line)){stringstream ss(line);while(ss>>a[r][c]){if(a[r][c].length()>maxline[c]){maxline[c]=a[r][c].length();}c++;}num[r++]=c;c=0;}for(i=0;i<r;i++){for(j=0;j<num[i]-1;j++){cout<<left<<setw(maxline[j]+1)<<a[i][j];}cout<<a[i][num[i]-1];printf("\n");}return 0;
}


这篇关于c++left right 和 setw() 函数的用法Alignment of Code的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/699590

相关文章

C++中RAII资源获取即初始化

《C++中RAII资源获取即初始化》RAII通过构造/析构自动管理资源生命周期,确保安全释放,本文就来介绍一下C++中的RAII技术及其应用,具有一定的参考价值,感兴趣的可以了解一下... 目录一、核心原理与机制二、标准库中的RAII实现三、自定义RAII类设计原则四、常见应用场景1. 内存管理2. 文件操

C++中零拷贝的多种实现方式

《C++中零拷贝的多种实现方式》本文主要介绍了C++中零拷贝的实现示例,旨在在减少数据在内存中的不必要复制,从而提高程序性能、降低内存使用并减少CPU消耗,零拷贝技术通过多种方式实现,下面就来了解一下... 目录一、C++中零拷贝技术的核心概念二、std::string_view 简介三、std::stri

C++高效内存池实现减少动态分配开销的解决方案

《C++高效内存池实现减少动态分配开销的解决方案》C++动态内存分配存在系统调用开销、碎片化和锁竞争等性能问题,内存池通过预分配、分块管理和缓存复用解决这些问题,下面就来了解一下... 目录一、C++内存分配的性能挑战二、内存池技术的核心原理三、主流内存池实现:TCMalloc与Jemalloc1. TCM

Python中help()和dir()函数的使用

《Python中help()和dir()函数的使用》我们经常需要查看某个对象(如模块、类、函数等)的属性和方法,Python提供了两个内置函数help()和dir(),它们可以帮助我们快速了解代... 目录1. 引言2. help() 函数2.1 作用2.2 使用方法2.3 示例(1) 查看内置函数的帮助(

mapstruct中的@Mapper注解的基本用法

《mapstruct中的@Mapper注解的基本用法》在MapStruct中,@Mapper注解是核心注解之一,用于标记一个接口或抽象类为MapStruct的映射器(Mapper),本文给大家介绍ma... 目录1. 基本用法2. 常用属性3. 高级用法4. 注意事项5. 总结6. 编译异常处理在MapSt

C++ 函数 strftime 和时间格式示例详解

《C++函数strftime和时间格式示例详解》strftime是C/C++标准库中用于格式化日期和时间的函数,定义在ctime头文件中,它将tm结构体中的时间信息转换为指定格式的字符串,是处理... 目录C++ 函数 strftipythonme 详解一、函数原型二、功能描述三、格式字符串说明四、返回值五

java中long的一些常见用法

《java中long的一些常见用法》在Java中,long是一种基本数据类型,用于表示长整型数值,接下来通过本文给大家介绍java中long的一些常见用法,感兴趣的朋友一起看看吧... 在Java中,long是一种基本数据类型,用于表示长整型数值。它的取值范围比int更大,从-922337203685477

MyBatis ResultMap 的基本用法示例详解

《MyBatisResultMap的基本用法示例详解》在MyBatis中,resultMap用于定义数据库查询结果到Java对象属性的映射关系,本文给大家介绍MyBatisResultMap的基本... 目录MyBATis 中的 resultMap1. resultMap 的基本语法2. 简单的 resul

Python主动抛出异常的各种用法和场景分析

《Python主动抛出异常的各种用法和场景分析》在Python中,我们不仅可以捕获和处理异常,还可以主动抛出异常,也就是以类的方式自定义错误的类型和提示信息,这在编程中非常有用,下面我将详细解释主动抛... 目录一、为什么要主动抛出异常?二、基本语法:raise关键字基本示例三、raise的多种用法1. 抛

C++作用域和标识符查找规则详解

《C++作用域和标识符查找规则详解》在C++中,作用域(Scope)和标识符查找(IdentifierLookup)是理解代码行为的重要概念,本文将详细介绍这些规则,并通过实例来说明它们的工作原理,需... 目录作用域标识符查找规则1. 普通查找(Ordinary Lookup)2. 限定查找(Qualif