AtCoder Beginner Contest 174 A Problem "Air Conditioner" Explanation (C ++, Python, Java)

AtCoder Beginner Contest 174 A I will explain the problem "Air Conditioner". Problem URL: https://atcoder.jp/contests/abc174/tasks/abc174_a </ q>

Problem Summary:

Given the current room temperature $ X $ [unit: degrees].
If the room temperature is above $ 30 $, turn on the air conditioner. Decide whether to turn on the air conditioner.

Constraints:

・ $ -40 \ leq X \ leq 40 $ ・ $ X $ is an integer

Commentary:

This is a problem of "conditional branching" </ font>, which is used quite frequently in programming.

This time it was a problem of conditional branching whether it was $ 30 \ leq X $.

Well, before tackling the problem Anyone think of code like the one below because of the small constraints? ??

A.py


X = int(input())
if (X == -40):
  print("No")
elif (X == -39):
  print("No")
elif (X == -38):
  print("No")
#After this X==Conditional branch up to 40

Due to restrictions, there is $ -40 \ leq \ X \ leq 40 $, so it is possible to AC by writing about 160 lines, but in competitive programming "The speed to write the code correctly" "</ B> is also required, so this writing style is not very good.

A2.py


X = int(input())
if (30<=X):
  print("Yes")
else:
#(X < 30)
  print("No")

If you write like this ・ Output "Yes" when it is $ 30 \ leq X $ ・ Output "No" when $ X <30 (X \ leq 29) $ By conditional branching with only 2 conditions </ b>, you can write a program that performs the same processing as code like A.py.

Below is an example of the answer in C ++ 14, Java. (The above code is an example of the answer in Python)

Example solution in C ++

{A.cpp}


#include<bits/stdc++.h>
using namespace std;
int main(){
  int x;
  cin >> x;
  if (30 <= x){
    cout << "Yes" << endl;
  }else{
    cout << "No" << endl;
  }
}

(Don't forget the line breaks !!)

Java answer example

{A.java}


import java.util.Scanner;
public class main(){
  public static void (String[] args){
    Scanner scan = new Scanner(System.in);
    int x = scan.nextInt();
    if (30 <= x){
      System.out.println("Yes");
    }else{
      System.out.println("No");
    }
  }
}

Recommended Posts