[Java] c004. Beat the Spread!

if/else來處理問題
import java.util.Scanner;
import java.io.BufferedInputStream;
 
public class C004 {
    public static void main(String[] args) {
        Scanner in = new Scanner(new BufferedInputStream(System.in));
        while (in.hasNext()) {
            int k = in.nextInt();
            for(int a = 0; a < k; a++){
                int i = in.nextInt();
                int j = in.nextInt();
                double m = (double) (i + j)/ 2;
                double n = i - m;
                boolean isInteger = true;
                if(Math.ceil(m) != Math.floor(m) || Math.ceil(n) != Math.floor(n))
                    isInteger = false;
                if(i < j || m < 0 || n < 0 || !isInteger)
                    System.out.println("impossible");
                else{
                    i = (int) m;
                    j = (int) n;
                    System.out.println(i+ " "+ j);
                } 
            }
            break;
        }
    }
}

沒有留言:

張貼留言