I immediately stumbled on the standard input method with AtCoder.

Introduction

I've been working as an engineer until now, but I started doing competition pregramming because I thought it was difficult to implement because it was mainly in the upstream process. I managed to solve one question, but I will post a memorandum of what I stumbled upon.

This time I will write about the data entry Scanner.

Challenged problem

Atcode's past score of 100 problem ... In other words, it's a simple problem.

https://atcoder.jp/contests/abc042/tasks/abc042_a

First submission

I managed to code while looking at various sites, and I was able to execute it from the vscode terminal and get the expected result. The following is the code content (probably a dirty code from an expert's point of view, but please forgive me ...)

public class Main {
    public static void main(String args[]) {
        String answer = "YES";
        String[] haiku = args;
        int sum = 0;
        for(int i = 0; i < 3; i++){
          if(haiku[i].equals("7") || haiku[i].equals("5")){
              sum = sum + Integer.valueOf(haiku[i]);
              System.out.println(sum);
          }
          else{
              answer = "NO";
          }
          System.out.println(haiku[i]);
        } 
        if(sum != 17){
            answer = "NO";
        }
        System.out.println(answer);
    }
}

result

Run-time error ... I couldn't even check the contents ... \ (^ o ^) /

スクリーンショット 2020-01-25 19.45.10.png

What went wrong

It seems that the input method was simply useless.

When I type Java Main X X X in the terminal, it's stored in the array ʻargs` separated by spaces, so I thought it was okay, but I was completely wrong.

What should i do

It is recommended to use Scanner to get the input contents. Get the input at the next (); part and use it again to get the next input value.

Scanner sc = new Scanner(System.in);
int i = Integer.parseInt(sc.next());

5th submission

I refactored the code many times (I'm sorry I want to use the term ...) and managed to clear it lol

import java.util.Scanner;
public class Main {
    public static void main(String args[]) {
        String answer = "YES";
        Scanner s = new Scanner(System.in);
        int sum = 0;
        for(int i = 0; i < 3; i++){
            int haiku = Integer.parseInt(s.next());
          if(haiku == 7 || haiku == 5 ){
              sum = sum + haiku;
          }
          else{
              answer = "NO";
          }
        } 
        if(sum != 17){
            answer = "NO";
        }
        System.out.println(answer);
    }
}

Reference site

https://qiita.com/p_shiki37/items/a0f6aac33bf60f5f65e4 https://uxmilk.jp/48686

If you have any suggestions, please do not hesitate to tell me-m (_ _) m

Recommended Posts

I immediately stumbled on the standard input method with AtCoder.
Come out with a suffix on the method
Come out with a suffix on the method 2
[Java] I want to test standard input & standard output with JUnit
How to display 0 on the left side of the standard input value
Learning Ruby with AtCoder 11 How to receive frequently used standard input
Simulate the simplex method with GUI
Programming with ruby (on the way)
I tried to explain the method
Recorded because I was addicted to the standard input of the Scanner class
My thoughts on the equals method (Java)
I wanted to add @VisibleForTesting to the method
I was addicted to the roll method
[JDBC ③] I tried to input from the main method using placeholders and arguments.
I stumbled on the Java version in Android Studio, so I will summarize it