A - 代码对齐

subject   

   You are working in a team that writes Incredibly Customizable Programming Codewriter (ICPC) whichis basically a text editor with bells and whistles. You are working on a module that takes a piece ofcode containing some definitions or other tabular information and aligns each column on a fixed verticalposition, while keeping the resulting code as short as possible, making sure that only whitespaces thatare absolutely required stay in the code. So, that the first words on each line are printed at positionp1 = 1; the second words on each line are printed at the minimal possible position p2, such that all firstwords end at or before position p2 − 2; the third words on each line are printed at the minimal possibleposition 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 ormore words separated by spaces. Each word can contain uppercase and lowercase Latin letters, allASCII punctuation marks, separators, and other non-whitespace ASCII characters (ASCII codes 33 to126 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 lastone) 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 ofthe code can have both leading and trailing spaces. Each line in the input file is at most 180 characterslong. 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, withthe same words in the same order, without trailing and leading spaces, separated by one or more spacessuch 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 code32).

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


大意就是:代码对齐。

思路:

    第一我们要分别存起来,把每一个单词存起来。之后取每一行最长的单词。

   根据最长的单词的长度,去设置该位置的占用的位置长度,不够的就是用空格去设置,就好了。


    解决方法,第一:c++自带的一种对齐方式。

                        第二:自己写一个对齐的代码。

                        第三:用比较第二的方法用容器.


个人爱好什么,你们可以去选择看看,不过如果是想要知道一些细节的话,学习一些由来的话,建议你们去看看第二种,很仔细的。


第一种解决方式:

    代码来之与:http://www.bubuko.com/infodetail-971984.html

 #include <bits/stdc++.h>
    using namespace std;
    const int N = 1005, M = 200;
    string s[N][M], line;
    int cw[M], cn[N];

    int main()
    {
        int r = 0, c = 0;
        while(getline(cin, line))
        {
            stringstream ss(line);
            while(ss >> s[r][c])
            {
                if(s[r][c].length() > cw[c])
                    cw[c] = s[r][c].length();
                ++c;
            }
            cn[r++] = c;
            c = 0;
        }

        for(int i = 0; i < r; ++i)
        {
            for(int j = 0; j < cn[i] - 1; ++j)
                cout << left << setw(cw[j] + 1) << s[i][j];
            cout << s[i][cn[i] - 1] << endl;
        }
        return 0;
    }

第二种方法如下:

    

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string.h>
using namespace std;


///思路如下:
///首先一个数组存起来多少行,之后就是拆成几个部分,用一个数组去存起来,每一个单词的最长长度
///输出的话,就是用最长长度减去他的长度,最后一个不能参与进来,
///具体的代码就是,两层for循环,一层输入,一层直接输出。




///一个记录最长长的数组,
int vis[1010];


///一个去存每一行的单词


///思路一:结构体去实现
///思路二:容器去实现


struct node
{
    ///存单词
    char map[1005][182];
    ///记录多少个单词。
    int k;
}a[1010];


int main()
{
    char str[182];
    ///行数目,记录
    int hang = 0;
    ///重置
    memset(a,0,sizeof(a));
    memset(vis,0,sizeof(vis));
    while(gets(str)!=NULL)
    {
        ///标记第一个是不是空格,记录到位单词字母开始存。
        int start =0;
        ///标记一行的单词数目
        int head = 0;
        ///存入的位置,单词的数目
        int t=0;
        ///为了防止最后一个是单词
        int len = strlen(str);
        str[len] = ' ';
        str[len+1]='\0';
        for(int i=0;i<len+1;i++)
        {
            if(str[i]!=' ' && str[i]!=9)
            {
                start = 1;
                a[hang].map[head][t++]=str[i];
            }else{
                if(start==1){
                    ///单词记录结束,重置一些变量。
                    a[hang].map[head][t]='\0';
                    ///记录最长的长度单词
                    vis[head] = max(vis[head],t);
                    t=0;
                    head++;
                    ///单词数目
                    a[hang].k++;
                    start=0;
                }
            }
        }
        ///记录结束,换行
        hang++;
    }
    ///存入检验
    /*for(int i=0;i<hang;i++)
    {
        for(int j=0;j<a[i].k;j++)
        {
            cout<<a[i].map[j]<<"&&"<<endl;
        }
    }
    for(int i=0;i<max1 ;i++)
    {
       cout<<vis[i]<<endl;
    }*/
    ///直接输出
    for(int i=0;i<hang;i++)
    {
        for(int j=0;j<a[i].k-1;j++)
        {
            int k  = strlen(a[i].map[j]);
            cout<<a[i].map[j];
            for(int t=0;t<=vis[j]-k;t++)
            {
                printf(" ");
            }
        }
        cout<<a[i].map[a[i].k-1]<<endl;
    }
 	return 0;
}
第三种代码如下:

    代码来自与:梁山伯liangrx06

  

#include <iostream>
#include <sstream>
#include <cstdio>
#include <cstring>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;

const int N = 1000;
const int LEN = 180;

int n;
vector<string> vs[N];
int w[LEN];

int main(void)
{
    n = 0;
    string line, s;
    memset(w, 0, sizeof(w));
    while (getline(cin, line)) {
        stringstream stm(line);
        int i = 0;
        while (stm >> s) {
            w[i] = max(w[i], (int)(s.size()));
            i ++;
            vs[n].push_back(s);
        }
        n ++;
    }

    for (int i = 0; i < n; i ++) {
        int k = vs[i].size();
        for (int j = 0; j < k-1; j ++) {
            cout << vs[i][j];
            for (int r = 0; r <= w[j]-vs[i][j].size(); r ++)
                printf(" ");
        }
        cout << vs[i][k-1] << endl;
    }   

    return 0;
}

posted @ 2017-07-25 13:03  让你一生残梦  阅读(139)  评论(0编辑  收藏  举报