Pete's Pantry

Pete isn’t feeling too well today. In an effort to try and get better, he wants to heat up a can of chicken soup. However, his pantry is a mess, and as soon as he grabs the can he wants, the stacks of cans all collapse and roll onto the kitchen floor. “If I only had a way to stack these cans to keep this from happening,” Pete thought.

The Problem:

Your job is to write a program to help Pete stack cans in his pantry. Cans come in all shapes and sizes, but for simplicity, we’ll assume that all cans are the same height, and only their width (diameter) will vary. The stacking must follow one simple rule:

No can shall be stacked atop a narrower can, or a can of equal width.

Pete doesn’t follow written directions very well, so he wants your program to show him how to stack his cans. For example,a can with the label “BEANS” should look like this:

image.png

The width of each can is determined by the can’s label. The label can have multiple lines, so the width of the can is simply the width of the longest line of the can’s label, plus two (for # delimeters). All cans are 8 units tall, counting the edges, so no can label will have more than six lines. Pete also wants a particular order to his cans, so your program should process the input cans in the order given. Try fitting a given can in the leftmost stack first, and work to the right if needed. If there is no existing stack that will hold a given can, start a new stack just to the right of the last existing stack (the first can should be placed in the leftmost spot on the shelf).

The Input:

Pete will have multiple sets of cans for you to stack. The first line of input will contain a positive integer, n, indicating the number of sets of cans (i.e., the number of data sets to be processed). Each set of cans will begin with a positive integer c (1 ≤c≤ 100), on a line by itself. On each of the next c lines will be a can label. Can labels consist of upper- and lower-case letters, digits, and spaces.The pound sign (’#’) may also appear in a can label and designates the start of a new line (i.e., the can label must be put on multiple lines in output). There will be no leading or trailing spaces on any line of the can label (in input) and the input lines will not exceed column 60. Assume that there is at least one letter(or digit) before and after each ‘#’.

The Output:

For each set of cans print “Can Stack #d:” , where d is the number of the set (starting with 1). Then, print two header lines to show column numbers. Then, print the stacks of cans using the format described above. Can labels must be centered both vertically and horizontally on the can(if an exact centering isn’t possible, favor the left half and/or top half of the can). Each stacked can should be centered above the can below it (again, favor the left side if an exact centering is impossible). There should be exactly one space between the bottommost cans in each stack. Assume that no final arrangement will be more than 60 characters wide, and no stack will be taller than 10 cans.

Leave a blank line after the output for each data set. Follow the format illustrated in Sample Output.

(PS:可能有的题目有些乱码,请大家在通知下载pdf题面)

输出时每行末尾的多余空格,不影响答案正确性

样例输入
2
2
Coaches8#Ali88
Geeks of#UCFdorms#Soup
6
Pork and#Beans
Cream of#Mushroom#Soup
Baby#Carrots
Snow#Peas
Grape#Jelly
Aunt Helens#Down Home#Country#Goodness
样例输出
         1         2         3         4         5         6
123456789012345678901234567890123456789012345678901234567890
########## ##########
#        # #        #
#        # #Geeks of#
#Coaches8# #UCFdorms#
# Ali88  # #  Soup  #
#        # #        #
#        # #        #
########## ##########

Can Stack #2:
         1         2         3         4         5         6
123456789012345678901234567890123456789012345678901234567890
 ######
 #    #
 #    #
 #Snow#
 #Peas#
 #    #
 #    #
 ######
#########   #######
#       #   #     #
#       #   #     #
# Baby  #   #Grape#
#Carrots#   #Jelly#
#       #   #     #
#       #   #     #
#########   #######
########## ########## #############
#        # #        # #           #
#        # #Cream of# #Aunt Helens#
#Pork and# #Mushroom# # Down Home #
# Beans  # #  Soup  # #  Country  #
#        # #        # # Goodness  #
#        # #        # #           #
########## ########## #############
#include <bits/stdc++.h>

using namespace std;
const int N = 105;
int T, n;

struct P {
    string label;
    string word[10];
    int width, line;
    int namestart;
    int wordstart[10];
} can[N];
P pile[N][N];
int pile_range[N], high;
int pile_start[N][N];

int main() {
    cin >> T;
    for (int t = 1; t <= T; t++) {
        cin >> n;
        getchar();
        for (int i = 1; i <= n; i++) {
            can[i].line = can[i].width = 0;
            for (int j = 0; j < 10; j++)can[i].word[j] = "";
        }
        for (int i = 1; i <= n; i++) {
            getline(cin, can[i].label);
            for (int j = 0; j < can[i].label.size(); j++) {
                if (can[i].label[j] != '#')
                    can[i].word[can[i].line].push_back(can[i].label[j]);
                else can[i].width = max(can[i].width, (int) can[i].word[can[i].line].size()), can[i].line++;
            }
            can[i].width = max(can[i].width, (int) can[i].word[can[i].line].size());
            can[i].namestart = (6 - can[i].line + 1) / 2;
            for (int j = 0; j <= can[i].line; j++)
                can[i].wordstart[j] = (can[i].width - can[i].word[j].size()) / 2;
        }
        printf("Can Stack #%d:\n", t);
        for (int i = 1; i <= 6; i++) {
            for (int j = 1; j <= 9; j++)putchar(' ');
            printf("%d", i);
        }
        puts("");
        for (int i = 1; i <= 6; i++) {
            for (int j = 1; j <= 9; j++)printf("%d", j);
            putchar('0');
        }
        puts("");
        for (int i = 1; i <= n; i++)
            for (int j = 1; j <= n; j++)
                pile[i][j].width = 0;
        for (int i = 1; i <= n; i++)pile[0][i].width = 1e9;
        high = 0;
        pile_range[0] = 100;
        for (int i = 1; i <= n; i++)pile_range[i] = 0;
        for (int i = 1; i <= n; i++)
            for (int j = high + 1; j >= 1; j--) {
                bool flag = false;
                for (int k = 1; k <= pile_range[j - 1]; k++) {
                    if (!pile[j][k].width && pile[j - 1][k].width > can[i].width) {
                        pile[j][k] = can[i];
                        if (k > pile_range[j])pile_range[j] = k;
                        flag = true;
                        if (j == high + 1)high++;
                        break;
                    }
                }
                if (flag)break;
            }
        for (int i = 1; i <= pile_range[1]; i++)pile[0][i].width = pile[1][i].width;
        for (int i = 1; i <= high; i++)
            for (int j = 1; j <= pile_range[i]; j++)
                if (pile[i][j].width)
                    pile_start[i][j] = (pile[i - 1][j].width - pile[i][j].width) / 2 + pile_start[i - 1][j];
        for (int i = high; i >= 1; i--) {
            for (int j = 1; j <= pile_range[i]; j++) {
                if (!pile[i][j].width)for (int k = 1; k <= pile[1][j].width + 2; k++)putchar(' ');
                else {
                    int k = 1;
                    for (; k <= (pile[i - 1][j].width - pile[i][j].width) / 2 + pile_start[i - 1][j]; k++)
                        putchar(' ');
                    for (int l = 1; l <= pile[i][j].width + 2; l++)putchar('#');
                    k += pile[i][j].width + 2;
                    for (; k <= pile[1][j].width + 2; k++)putchar(' ');
                }
                if (j != pile_range[i])
                    putchar(' ');
            }
            puts("");
            for (int j = 1; j <= 6; j++) {
                for (int k = 1; k <= pile_range[i]; k++) {
                    if (!pile[i][k].width)for (int l = 1; l <= pile[1][k].width + 3; l++)putchar(' ');
                    else {
                        int e = 1;
                        for (; e <= (pile[i - 1][k].width - pile[i][k].width) / 2 + pile_start[i - 1][k]; e++)
                            putchar(' ');
                        e += pile[i][k].width + 2;
                        putchar('#');
                        if (j < pile[i][k].namestart || j > pile[i][k].namestart + pile[i][k].line)
                            for (int l = 1; l <= pile[i][k].width; l++)putchar(' ');
                        else {
                            int l = 1;
                            for (; l <= pile[i][k].wordstart[j - pile[i][k].namestart]; l++)putchar(' ');
                            cout << pile[i][k].word[j - pile[i][k].namestart];
                            l += pile[i][k].word[j - pile[i][k].namestart].size();
                            for (; l <= pile[i][k].width; l++)putchar(' ');
                        }
                        printf("# ");
                        if (k != pile_range[i])
                            for (; e <= pile[1][k].width + 2; e++)putchar(' ');
                    }
                }
                puts("");
            }
            for (int j = 1; j <= pile_range[i]; j++) {
                if (!pile[i][j].width)for (int k = 1; k <= pile[1][j].width + 2; k++)putchar(' ');
                else {
                    int k = 1;
                    for (; k <= (pile[i - 1][j].width - pile[i][j].width) / 2 + pile_start[i - 1][j]; k++)putchar(' ');
                    for (int l = 1; l <= pile[i][j].width + 2; l++)putchar('#');
                    k += pile[i][j].width + 2;
                    for (; k <= pile[1][j].width + 2; k++)putchar(' ');
                }
                if (j != pile_range[i])
                    putchar(' ');
            }
            puts("");
        }
        puts("");
    }
    return 0;
}
发布了329 篇原创文章 · 获赞 28 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_45323960/article/details/104671268
s
a's