Official
A - OS Versions Editorial
by
A - OS Versions Editorial
by
physics0523
初心者の方へ
- プログラミングの学習を始めたばかりで何から手をつけるべきかわからない方は、まずは practice contest の問題A「Welcome to AtCoder」をお試しください。言語ごとに解答例が掲載されています。
- また、プログラミングコンテストの問題に慣れていない方は、 AtCoder Beginners Selection の問題をいくつか試すことをおすすめします。
- C++入門 AtCoder Programming Guide for beginners (APG4b) は、競技プログラミングのための C++ 入門用コンテンツです。
- Python入門 AtCoder Programming Guide for beginners (APG4bPython) は、競技プログラミングのための Python 入門用コンテンツです。
この問題にはいくつかの方針があります。
方針1. 場合分けを活用する
\(9\) 通り場合分けをするとこの問題に正解できます。
答えが No となるのは \(3\) 通りであることを利用すると、場合分けの数が減ります。実装例はその方針です。
実装例 (C++):
#include<bits/stdc++.h>
using namespace std;
int main(){
string x,y;
cin >> x >> y;
if(x=="Ocelot" && y=="Serval"){cout << "No\n";}
else if(x=="Ocelot" && y=="Lynx"){cout << "No\n";}
else if(x=="Serval" && y=="Lynx"){cout << "No\n";}
else{cout << "Yes\n";}
return 0;
}
方針2. バージョンに数を割り当てる
バージョンに古い順で数字を与えることで、本問題を大小比較に帰着できます。
実装例 (C++):
#include<bits/stdc++.h>
using namespace std;
int main(){
map<string,int> mp;
mp["Ocelot"]=1;
mp["Serval"]=2;
mp["Lynx"]=3;
string x,y;
cin >> x >> y;
if(mp[x]>=mp[y]){cout << "Yes\n";}
else{cout << "No\n";}
return 0;
}
方針3. 方針2 の応用
Lynx だけ先頭の文字を書き換えることにすれば、先頭の文字の辞書順比較に帰着させることもできます。この方針ではコード量を少なくできます。
実装例 (C++):
#include<bits/stdc++.h>
using namespace std;
int main(){
string x,y;
cin >> x >> y;
if(x[0]=='L'){x[0]='Z';}
if(y[0]=='L'){y[0]='Z';}
if(x[0]>=y[0]){cout << "Yes\n";}
else{cout << "No\n";}
return 0;
}
posted:
last update:
