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

来源:互联网 发布:淘宝买家服务热线 编辑:程序博客网 时间:2024/06/14 09:43

首先先讲一讲这个函数的头文件是#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;}