公式

A - Traffic Light 解説 by en_translator 印刷ページ


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” (https://atcoder.jp/contests/abs).


The possible colors of the traffic light are B, Y, and R. One can use an if statement to branch into three cases.


In C++, string and char are distinguished. A string is surrounded by ", and a char by '. One may use either for input and comparison, but make sure to use the same type.

Sample code (C++)

#include<bits/stdc++.h>
using namespace std;

int main(){
  char c; cin >> c;
  if (c == 'B'){
      cout << 'Y' << endl;
  }else if (c == 'Y'){
      cout << 'R' << endl;
  }else{
      cout << 'B' << endl;
  }
}
#include<bits/stdc++.h>
using namespace std;

int main(){
  string c; cin >> c;
  if (c == "B"){
      cout << 'Y' << endl;
  }else if (c == "Y"){
      cout << 'R' << endl;
  }else{
      cout << 'B' << endl;
  }
}

Sample code (PyPy)

c = input()

if c == "B":
    print("Y")
elif c == "Y":
    print("R")
else:
    print("B")

投稿日時:
最終更新: