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.
The problem is here.
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.
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;
}
}
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