AOJ 0003 Is it a Right Triangle?

リンク AOJ 0003 Is it a Right Triangle?

方針

辺a, b, c について三平方の定理 a^2 = b^2 + c^2 が成立するかを調べます。
調べるといいましても、たかが3通りなのでif文で書いちゃいました。

注意

複数のデータセットが与えられます・・・といった場合、入力を処理するときに、「これ以上入力があるか」を判定する必要があります。
そんなときに便利なのがScannerクラスのhasNextメソッドです。入力を処理する部分で(僕の場合はread関数)こいつを使ってやると、複数の入力を最後まで処理することができます。

  if(!sc.hasNext())return false;

例:read関数内で上記のように書くと、入力がなくなったとき、falseを返す(=read関数から抜けることができる)

ソース

import java.util.*;
public class Main {
    static Scanner sc = new Scanner(System.in);
    static int n, a, b, c;
    static int A,B,C;
    public static void main(String[] args) {
        while(read()){

        }
    }

    static boolean read(){
        if(!sc.hasNext())return false;
        n = sc.nextInt();
        for(int i = 0; i < n; i++){
            a = sc.nextInt();
            b = sc.nextInt();
            c = sc.nextInt();
            solve();
        }
        return true;
    }

    static void solve(){
        if(a != 0 && b != 0 && c!= 0){
            A = a * a;
            B = b * b;
            C = c * c;
            if(A == B + C){
                System.out.println("YES");
            }else if(B == A + C){
                System.out.println("YES");
            }else if(C == A + B){
                System.out.println("YES");
            }else{
                System.out.println("NO");
            }
        }
    }

}