公式

A - CBC 解説 by MtSaka


初心者の方へ

この問題は文字列の扱い、その中でも特に英大文字と英小文字の区別が問われている問題です。

C++ の場合は isupper 関数を使うことである文字が英大文字であるかそうでないかを判定できます。 Python などの他の言語でも同様の機能が標準ライブラリにある場合が多いです。

他にも文字コードを利用した判定方法があります。

ord(x) を文字 \(x\) の対応する文字コードであるとしたとき、ある文字 \(c\) が英大文字であることは ord('A') \(\leq \) ord(c) \(\leq\) ord('Z') を見たすことと同値です。なぜならば、ASCIIコード表上で英大文字が連続しているからです。よって、これを利用して判定することができます。文字コードを用いるこの方法も多くのプログラミング言語で使うことができます。

実装例1(C++):

#include <bits/stdc++.h>
using namespace std;
int main() {
    string s;
    cin >> s;
    for (char c : s) {
        if (isupper(c)) cout << c;
    }
    cout << endl;
}

実装例2(C++):

#include <bits/stdc++.h>
using namespace std;
int main() {
    string s;
    cin >> s;
    for (char c : s) {
        if ('A' <= c && c <= 'Z') cout << c;
    }
    cout << endl;
}

実装例1(Python):

s = input()
ans = ""
for c in s:
    if c.isupper():
        ans += c

print(ans)

実装例2(Python):

s = input()
ans = ""
for c in s:
    if ord('A') <= ord(c) and ord(c) <= ord('Z'):
        ans += c

print(ans)

投稿日時:
最終更新: