首页 > 专题系列 > Java解POJ > POJ 2301 Beat the Spread! [解题报告] Java
2013
11-11

POJ 2301 Beat the Spread! [解题报告] Java

Beat the Spread!

问题描述 :

Superbowl Sunday is nearly here. In order to pass the time waiting for the half-time commercials and wardrobe malfunctions, the local hackers have organized a betting pool on the game. Members place their bets on the sum of the two final scores, or on the absolute difference between the two scores.

Given the winning numbers for each type of bet, can you deduce the final scores?

输入:

The first line of input contains n, the number of test cases. n lines follow, each representing a test case. Each test case gives s and d, non-negative integers representing the sum and (absolute) difference between the two final scores.

输出:

For each test case, output a line giving the two final scores, largest first. If there are no such scores, output a line containing “impossible”. Recall that football scores are always non-negative integers.

样例输入:

2
40 20
20 40

样例输出:

30 10
impossible

解题代码:

import java.util.Scanner;   
  
public class Main{   
  
    public static void main(String[] args) {   
        Scanner scan = new Scanner(System.in);   
        if (scan.hasNext()) {   
            int n = scan.nextInt();   
            for (int i = 0; i < n; i++) {   
                int s = scan.nextInt();   
                int d = scan.nextInt();   
                if (s < d ||(s+d)%2==1) {   
                    System.out.println("impossible");   
                } else {   
                    int a = (s + d) / 2;   
                    int b = (s - d) / 2;   
                    System.out.println(a + " " + b);   
                }   
            }   
        }   
  
    }   
}

  1. 这道题目的核心一句话是:取还是不取。
    如果当前取,则index+1作为参数。如果当前不取,则任用index作为参数。

  2. 如果两个序列的最后字符不匹配(即X [M-1]!= Y [N-1])
    L(X [0 .. M-1],Y [0 .. N-1])= MAX(L(X [0 .. M-2],Y [0 .. N-1]),L(X [0 .. M-1],Y [0 .. N-1])
    这里写错了吧。