AtCoder Beginner Contest 166 A Explanation of Problem "A? C" (Python3, C ++, Java)

This is Rute. AtCoder Beginner Contest 166 A I will explain the problem "A? C".

Problem URL: https://atcoder.jp/contests/abc166/tasks/abc166_a

Problem summary

The rules for holding the contest are as follows. (In this issue) ・ ARC will be held the week after ABC is held ・ ABC will be held next week after ARC

You will be given the string $ S $, which represents the contest held last week. Output a string that represents this week's contest.

Constraint

· $ S $ is 'ABC' or 'ARC'

Commentary

・ ARC will be held the week after ABC is held ・ ABC will be held next week after ARC

From the rule ・ If $ S $ is 'ABC', ARC will be held this week ・ If $ S $ is 'ARC', ABC will be held this week You can see that. Therefore, you can read the string $ S $ and output according to the above conditional branch. </ b> It is possible to AC by reading the second character of $ S $ and performing conditional branching </ b>, but the method described above is easier to implement.

Below are examples of solutions in Python3, C ++, and Java.

Example of answer for each language

Example solution in Python3

{ABC166.py}


S = input()
if S == "ABC":
  print("ARC")
else:
  print("ABC")
Example solution in C ++

{ABC166A.cpp}


#include<bits/stdc++.h>
using namespace std;
int main(){
  string S;
  cin >> S;
  if (S == "ABC"){
    cout << "ARC" << endl;
  }else{
    cout << "ABC" << endl;
  }
}
Java answer example

{ABC166A.java}


import java.util.Scanner;
public class Main{
  public static void main(String[] args)
  {
    Scanner scan =new Scanner (System.in);
    String S =  scan.nextLine();
    if (S.equals("ABC")){
      System.out.println("ARC");
    }else{
      System.out.println("ABC");
    }
  }
}
  • In Java, code " string A ".equals (" string B ") instead of == when comparing strings. A compile error may occur if the conditional expression is ==. In the latter case, it can be correctly determined whether the character string A is the same as the character string B.

Recommended Posts