练习codeforces1741A. Compare T-Shirt Sizes

题目如下
A. Compare T-Shirt Sizes
time limit per test1 second
memory limit per test256 megabytes
Two T-shirt sizes are given: 𝑎 and 𝑏. The T-shirt size is either a string M or a string consisting of several (possibly zero) characters X and one of the characters S or L.

For example, strings M, XXL, S, XXXXXXXS could be the size of some T-shirts. And the strings XM, LL, SX are not sizes.

The letter M stands for medium, S for small, L for large. The letter X refers to the degree of size (from eXtra). For example, XXL is extra-extra-large (bigger than XL, and smaller than XXXL).

You need to compare two given sizes of T-shirts 𝑎 and 𝑏.

The T-shirts are compared as follows:

any small size (no matter how many letters X) is smaller than the medium size and any large size;
any large size (regardless of the number of letters X) is larger than the medium size and any small size;
the more letters X before S, the smaller the size;
the more letters X in front of L, the larger the size.
For example:

XXXS < XS
XXXL > XL
XL > M
XXL = XXL
XXXXXS < M
XL > XXXS
Input
The first line of the input contains a single integer 𝑡 (1≤𝑡≤104) — the number of test cases.

Each test case consists of one line, in which 𝑎 and 𝑏 T-shirt sizes are written. The lengths of the strings corresponding to the T-shirt sizes do not exceed 50. It is guaranteed that all sizes are correct.

Output
For each test case, print on a separate line the result of comparing 𝑎 and 𝑏 T-shirt sizes (lines "<", ">" or "=" without quotes).
题目大意
本题是一个判断尺码大小的题目,判断尺码的规则如下
无论带多少个“X”,保持S < M < L的原则不变,在此基础上,S前缀越多X表明尺码越小,L前缀越多X表明尺码越大,M则不带任何X

题目分析
根据上述判断原则,我们可以通过每个字符串的结尾来判断他属于哪个大类,再进行细分判断
我们先要通过每个字符串的结尾字符来判断两个字符串是否是同种,再通过strlen求出长度,若结尾字符相同则分出若结尾为S,则越长说明带的X越多,尺码越小;若结尾为L,那么越长说明X越多,尺码越大。结尾字符不同则直接按照“S < M < L”原则判断

点击查看代码
#include <stdio.h>
#include <string.h>

int main(){
    int t;
    scanf("%d",&t);
    while(t--){
        char a[55], b[55];
        scanf("%s %s", a, b);
        int len1 = strlen(a) - 1;
        int len2 = strlen(b) - 1;
        if(a[len1] != b[len2]){
            if(a[len1] < b[len2])
                printf(">\n");
            else
                printf("<\n");
        }else{
            if(a[len1] == 'S'){
                if(len1 > len2)
                    printf("<\n");
                else if(len1 < len2)
                    printf(">\n");
                else
                    printf("=\n");
            }else if(a[len1] == 'L'){
                if(len1 > len2)
                    printf(">\n");
                else if(len1 < len2)
                    printf("<\n");
                else
                    printf("=\n");
            }else{  // 'M'
                printf("=\n");
            }
        }
    }
    return 0;
}
posted @ 2025-07-04 20:53  sirro1uta  阅读(11)  评论(0)    收藏  举报