CF1703A YES or YES? 题解

· · 题解

一 题意简述

本题多组测试数据,每次输入一个长度为 3 的字符串,不分大小写,判断它是不是 \textit{YES},是则输出 \textit{YES},否则输出 \textit{NO}。

二 解题思路

大体说来就是把输入的字符串全部转换为小写字母,然后和 \textit{YES} 按位比较。

大小写转换:减去大写字母的 ASCII 码,再加上小写字母的,代码如下:

s[i] = s[i] - 'A' + 'a';

三 代码

# include <bits/stdc++.h>
#define ll long long
#define re register
#define il inline
using namespace std;
int main()
{
    int t;
    scanf("%d", &t);
    while (t--)
    {
        string s;
        cin >> s;
        for (int i = 0; i < 3; ++i)
        {
            if (s[i] >= 'A' && s[i] <= 'Z')
                s[i] = s[i] - 'A' + 'a';
        }
        if (s == "yes") printf("YES\n");
        else printf("NO\n");
    }
    return 0;
}