There is more than one correct coding answer

Introduction I am a liberal arts student learning the java language. Recently, I'm interested in content that can compete with coding skills called AtCoder, and I'm in the process of solving past questions.

Can this code be made smarter? I made a trial and error to see if I could make the code I used as a reference smart. ..

The problem is Atcoder's B-Crane and Turtle's turtle calculation problem. https://atcoder.jp/contests/abc170/tasks/abc170_b

qiita.turukame


import java.util.Scanner;
public class Main{
  public static void main(String[] args){
    Scanner scan = new Scanner(System.in);
    int x = scan.nextInt();
    int y = scan.nextInt();
    int flag = 0;
    for (int i = 0; i <= x; i++){
      if ((y-2*i)%4 == 0 && (y - 2 * i)/4 == x-i){
        flag++;
        System.out.println("Yes");
        break;
      }
    }
    if  (flag == 0){
      System.out.println("No");
    }
  }
}

this,,,, (Code review by saka1029!)

qiita.turukamekai


public class main {
static boolean tsurukame(int x, int y) {
    return y % 2 == 0 && y >= 2 * x && 4 * x >= y;
}

public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    int x = scan.nextInt();//Animal
    int y = scan.nextInt();//Number
    System.out.println(tsurukame(x, y) ? "yes" : "no");
}
}


Ingenuity In conclusion, I packed it in the for statement. By inserting (Y / 4 <= X && Y% 2 == 0), if you happen to get an even number of yes, you can enter Y% 2 == 0 to prove that it is an even number. Then, it is possible to prevent processing with an even number. Is it correct? .. ..

Conclusion If you want to do coding, you can code compactly! Interesting! !! !! !!

Recommended Posts

There is more than one correct coding answer
Pointcut Expression I want to specify more than one