Description

還記得 CPE 一星題中的train swapping嗎? 只是這一次火車除了照編號排序火車以外,還多了顏色(只有 R, G, B)。當編號相同時,請依照R, G, B 的順序進行排序。

請讓使用者輸入要排序的火車顏色與編號後,把火車做排序並輸出。

Do you remember the train swapping problem from CPE 1 star? This time, in addition to sorting the trains by their numbers, the trains also have colors (only R, G, B). When the numbers are the same, please sort them in the order of R, G, B.

Please allow the user to input the colors and numbers of the trains to be sorted, and then sort the trains accordingly and output them.

Input

輸入包含多行,每行包含一個正整數及一個字元,代表火車的編號與顏色,當輸入"0 0"時代表輸入結束。

The input consists of multiple lines. Each line contains a positive integer followed by a character, representing the number and color of the train. Input "0 0" to indicate the end of input.

Output

請輸出火車排序後的結果,每行一組火車的編號、顏色。最後一行後面沒有換行。

Please output the sorted trains, with each line representing the number and color of a train. There is no new line after last line.

Sample Input 1

4 R
8 G
5 B
3 R
9 G
9 B
5 R
2 G
1 G
1 R
0 0

Sample Output 1

1 R
1 G
2 G
3 R
4 R
5 R
5 B
8 G
9 G
9 B
#include <stdio.h>
#include <stdlib.h>

struct train {
    int n;
    char c;
};

int main() {
    struct train t[30];
    int timer = 0;

    while (1) {
        int n;
        char c;
        scanf("%d %c", &n, &c);
        if (n == 0 && c == '0') {
            break;
        }
        t[timer].n = n;
        t[timer].c = c;
        timer++;
    }

    for (int i = 0; i < timer - 1; i++) {
        for (int j = 0; j < timer - i - 1; j++) {
            if (t[j].n > t[j + 1].n) {
                struct train temp = t[j];
                t[j] = t[j + 1];
                t[j + 1] = temp;
            }
        }
    }

    for (int i = 0; i < timer - 1; i++) {
        for (int j = 0; j < timer - i - 1; j++) {
            if (t[j].n == t[j + 1].n && t[j].c < t[j + 1].c) {
                struct train temp = t[j];
                t[j] = t[j + 1];
                t[j + 1] = temp;
            }
        }
    }

    for (int i = 0; i < timer; i++) {
        printf("%d %c\\n", t[i].n, t[i].c);
    }

    return 0;
}