Codeforces 1703A YES or YES? 對或是對?

題目說明

難易度 : 800

英文原文如下

There is a string s of length 3, consisting of uppercase and lowercase English letters. Check if it is equal to “YES” (without quotes), where each letter can be in any case. For example, “yES”, “Yes”, “yes” are all allowable.

中文翻譯

有一個長度為 3 的字串 s,由大寫以及小寫的英文字母組成。檢查它是否與 “YES”(不用判斷引號) 相等,單字可以是任何的大小寫。舉例來說, “yES”, “Yes”, “yes” 都是可以接受的。

輸入

The first line of the input contains an integer t(1≤t≤103) — the number of testcases.

The description of each test consists of one line containing one string s consisting of three characters. Each character of s is either an uppercase or lowercase English letter.

輸入的第一行包含了一個整數 t(1≤t≤103) — 測試案例的數量。

每一個測試案例包含了一行,裡面有一個字串 s 由三個字元所組成。 組成 s 的字元只會為英文大寫或小寫字母。

輸出

For each test case, output “YES” (without quotes) if s satisfies the condition, and “NO” (without quotes) otherwise.

You can output “YES” and “NO” in any case (for example, strings “yES”, “yes” and “Yes” will be recognized as a positive response).

針對每個案例,要是 s 符合條件就輸出 “YES”(省略引號),不然就輸出 “NO”(不用引號)。

你可以印出 “YES” 跟 “NO” 而不需要在意它們大小寫(舉例來說,字串 “yES”, “yes” 跟”Yes” 都會被視為正向的答覆)

範例

輸入10
YES
yES
yes
Yes
YeS
Noo
orZ
yEz
Yas
XES
輸出YES
YES
YES
YES
YES
NO
NO
NO
NO
NO

筆記

The first five test cases contain the strings “YES”, “yES”, “yes”, “Yes”, “YeS”. All of these are equal to “YES”, where each character is either uppercase or lowercase.

前五個測試案例包含了字串 “YES”, “yES”, “yes”, “Yes”, “YeS”,它們全部字母都與”YES”相等,在不判斷大小寫的情況下。


解題思路

通常來說,會有兩種方式可以解決這一題

第一種就是用內建的函式,例如 C# 中的 string.Compare 就有支援不區分大小寫的做法

另一種則是先轉型(大寫或小寫),再來判斷

之前的有一題就跟這個幾乎完全相同 ➡ Codeforces 112A Petya 以及字串們

C# 解決方案

方案1

int n = int.Parse(Console.ReadLine());

for(int i=0; i<n; i++){
    var str = Console.ReadLine();
    var res = string.Compare("YES", str , StringComparison.OrdinalIgnoreCase);
    Console.WriteLine(res==0?"YES":"NO");
}

方案2

int n = int.Parse(Console.ReadLine());

for(int i=0; i<n; i++){
    var str = Console.ReadLine().ToUpper();
    var res = "YES"==str;
    Console.WriteLine(res?"YES":"NO");
}
Klook.com

結論

前幾天的地震真的大得嚇死人.. 希望大家都安全

🧡如果文章有幫上你的忙,那是我的榮幸

🧡收藏文章或幫我點個廣告,那都是對我的支持

✅如有任何疑問,歡迎透過留言或messenger讓我知道 !

題目連結 : Problem – 1703A – Codeforces

一些其他的Codeforces文章

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *