公式
A - OS Versions 解説 by en_translator
For beginners
- If you are new to learning programming and do not know where to start, please try Problem A "Welcome to AtCoder" from practice contest. There you can find a sample code for each language.
- Also, if you are not familiar with problems in programming contests, we recommend you to try some problems in "AtCoder Beginners Selection".
- 「C++入門 AtCoder Programming Guide for beginners (APG4b)」 is a C++ tutorial for competitive programmers. Sadly, this is only in Japanese too.
- 「Python入門 AtCoder Programming Guide for beginners (APG4bPython)」 is a Python tutorial for competitive programmers. Again, this is only in Japanese.
There are several approaches to this problem.
Approach 1. Casework
The problem can be solved by considering the \(9\) possible cases.
If we notice that there are only \(3\) cases whose answer is No
, the conditional branches can be reduced. The sample code adopts this idea.
Sample code (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;
}
Approach 2. Represent the versions by numbers
By assigning numbers to the versions in chronological order, the problem can be reduced to an integer comparison.
Sample code (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;
}
Approach 3. Variant of Approach 2
By altering the first character of Lynx
, the problem can be boiled down to a lexicographical comparison of the first letter. This approach helps reducing the amount of code.
Sample code (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;
}
投稿日時:
最終更新: