Beat the Spread!
垮掉的传播
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 5329 Accepted Submission(s): 2782
Problem Description
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,
当地的黑客组织打赌游戏。人们不是押了两个决赛分数的和(求s+d),
or on the absolute difference between the two scores.
就是押了两数之差的绝对值(求| s-d |)。
Given the winning numbers for each type of bet, can you deduce the final scores?
对于每一个赌注给一个赢得号码,你能推断出决赛分数吗?(用s和d求出决赛分数)
Input
The first line of input contains n, the number of test cases. n lines follow, each representing a test case.
输入的第一行是n,表示测试的个数。接下来是n行,每一行代表一个测试事件。
Each test case gives s and d, non-negative integers representing the sum and (absolute) difference between the two final scores.
对于每一个测试用例,给s和d。和是非负整数,差的绝对值。
Output
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.
在单独的一行输出"impossible。(回想一下)记住,足球成绩总是非负整数 {(s+d)/2是整数}。
Sample Input
2 40 20 s d 20 40
Sample Output
30 10 非负整数( s>d ),**不能是小数,也不能是负数,只能是0和正整数 目的:提取公因式,让公因式做判断语句,使代码看起来有很高的水平 因为答案不能为负数和小数,所以我们要排除这些答案 正真输出的只有0和正整数*** (s+d)/2 ----- (s-d)/2 ( s+d + (s-d) - (s-d) )/2 ------- (s-d)/2 d + (s-d)/2 ------ (s-d)/2 目的完成: 公因式 (s-d)/2 ------ { 改进:(s-d)&0x01 让 (s-d)与16进制1进行 异或 } 例:0011 0001 & 0000 0001 = 0000 0001 如果异或等于1 代表(s-d)是奇数,那么结果就是小数 impossible s-d<0
Source
University of Waterloo Local Contest 2005.02.05
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); while (n-- > 0) { int s = sc.nextInt(); int d = sc.nextInt(); int temp = s - d; if (temp < 0 || (temp & 0x01) == 1) { System.out.println("impossible"); } else { System.out.println((d + temp / 2) + " " + temp / 2); } } } }