[GO] I tried to solve AOJ's Small, Large, or Equal

Hello This is skanehira from Global Sense Co., Ltd..

The AOJ introduced earlier in this article I tried to solve the title problem.

It ’s a simple algorithm, I tried to make it easy to read and efficient.

problem

The problem is here. image.png

With a simple problem All you have to do is compare the magnitude of the entered numbers and output.

Because the positions of a and b do not change It's an image that only the symbols change depending on the conditions.

Source

I will post it because it is a correct answer in the judgment. I wonder if there is a better way to write ...

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        String[] line = scan.nextLine().split(" ");

        int[] inputs = new int[2];
        int input;

        // -1,000 ≤ a, b ≤ 1,000
        for (int i = 0; i < line.length; i++) {
            input = Integer.parseInt(line[i]);
            if (input < -1000 || 1000 < input ) System.exit(-1);
            inputs[i] = input;
        }

        int a = inputs[0];
        int b = inputs[1];

        System.out.println("a " + (a < b ? "<": a > b ? ">" : "==") + " b" );

        scan.close();
    }

}

It may be a little confusing because it uses the ternary operator twice, I think this has shortened it considerably.

If the judgment process is a method, it will be as follows.

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        String[] line = scan.nextLine().split(" ");

        int[] inputs = new int[2];
        int input;

        // -1,000 ≤ a, b ≤ 1,000
        for (int i = 0; i < line.length; i++) {
            input = Integer.parseInt(line[i]);
            if (input < -1000 || 1000 < input ) System.exit(-1);
            inputs[i] = input;
        }

        System.out.println("a " + getSymbol(inputs[0], inputs[1]) + " b" );

        scan.close();
    }

    public static String getSymbol(int a, int b) {
        String symbol = "==";

        if (a < b) {
            symbol = "<";
        } else if (a > b){
            symbol = ">";
        }

        return symbol;
    }
}

Finally

I feel like I can omit the input part a little more. I can't think of it for now!

This way Can be shortened more Memory usage is reduced Execution time is reduced I would be grateful if you could give me advice such as m (_ _) m

Recommended Posts

I tried to solve AOJ's Small, Large, or Equal
I tried to solve AOJ's Binary Search
[Java] I tried to solve Paiza's B rank problem
I tried to verify yum-cron
I tried to collect and solve Ruby's "class" related problems.
I tried to solve the problem of "multi-stage selection" with Ruby
I tried to chew C # (indexer)
I tried to summarize iOS 14 support
I tried to implement flexible OR mapping with MyBatis Dynamic SQL
I tried to interact with Java
I tried to explain the method
I tried to summarize Java learning (1)
I tried to understand nil guard
I tried to solve the paiza campaign problem "Challenge from Kaito 813"
I tried to summarize Java 8 now
I tried to chew C # (polymorphism: polymorphism)
I tried to explain Active Hash
I tried to solve the problem of Google Tech Dev Guide
I tried to solve the tribonacci sequence problem in Ruby, with recursion.
I tried to summarize the methods used
I tried to introduce CircleCI 2.0 to Rails app
I tried migrating Processing to VS Code
I tried to implement the Iterator pattern
I tried to summarize the Stream API
I tried to build AdoptOpenjdk 11 on CentOS 7
What is Docker? I tried to summarize
I tried to build Ruby 3.0.0 from source
I tried to use Selenium like JQuery
I tried to touch JavaScript Part.2 Object-oriented
I tried to implement ModanShogi with Kinx
I tried to solve the tribonatch sequence problem in Ruby (time limit 10 minutes)
[Small story] I tried to make the java ArrayList a little more convenient
I tried to summarize about JVM / garbage collection
I tried to verify AdoptOpenJDK 11 (11.0.2) with Docker image
I tried to make Basic authentication with Java
I tried to implement polymorphic related in Nogizaka.
I tried to manage struts configuration with Coggle
[Rails] I tried to raise the Rails version from 5.0 to 5.2
I tried to manage login information with JMX
I tried to organize the session in Rails
java I tried to break a simple block
I tried to link grafana and postgres [docker-compose]
I tried to develop a man-hour management tool
I tried to develop a DUO3.0 study website.
I tried to chew C # (basic of encapsulation)
I tried to implement deep learning in Java
[Must see !!!] I tried to summarize object orientation!
I tried to create a LINE clone app
I tried to build AdoptOpenJDK 8 (Addition: Amazon Corretto 8)
[Ruby basics] I tried to learn modules (Chapter 1)
I tried to output multiplication table in Java
I tried to link JavaFX and Spring Framework.
I tried to set tomcat to run the Servlet.
I tried to build Micra mackerel in 1 hour!
I tried to develop an application in 2 languages
I tried to create Alexa skill in Java
I tried to develop a website to record expenses.
I tried to implement a server using Netty
I tried to break a block with java (1)