Submission
Status:
PPPPP
Score: 100
User: Spongebob
Problemset: ชั้นหนังสือ
Language: c
Time: 0.052 second
Submitted On: 2024-10-08 15:11:15
#include <stdio.h>
#include <string.h>
int main() {
// initilize valriable and recieve user input
int height, type, width = 0;
scanf("%d %d", &height, &type);
char names[type][height+1];
char temp[height+1];
int amount[type], temp_num, curr_col = 1, flip_counter = 1;
for (int i = 0; i < type; i++) {
scanf("%d %s", &amount[i], names[i]);
width += amount[i];
}
// intilize grid
char grid[height+2][width*2+2];
for (int i = 0; i < height+2; i++) {
for (int j = 0; j < width*2+1; j++) {
if (i == 0 || i == height+1) {
if (j%2 == 1) {
grid[i][j] = '-';
} else {
grid[i][j] = '+';
}
}
else if (j%2 == 0) {
grid[i][j] = '|';
} else {
grid[i][j] = '.';
}
}
grid[i][width*2+1] = '\0';
}
// sorting book
for (int i = 0; i < type-1; i++) {
for (int j = 0; j < type-1-i; j++) {
if (strcmp(names[j], names[j+1]) > 0) {
strcpy(temp, names[j]);
strcpy(names[j], names[j+1]);
strcpy(names[j+1], temp);
temp_num = amount[j];
amount[j] = amount[j+1];
amount[j+1] = temp_num;
}
}
}
// display books
for (int i = 0; i < type; i++) {
for (int j = 0; j < amount[i]; j++) {
for (int k = 0; k < strlen(names[i]); k++) {
if (flip_counter % 2 == 1) {
grid[k+1][curr_col] = names[i][k];
}
else {
grid[height - k][curr_col] = names[i][k];
}
}
flip_counter++;
curr_col += 2;
}
}
// output grid
for (int i = 0; i < height+2; i++) {
printf("%s\n", grid[i]);
}
return 0;
}