UVA10812 Beat the Spread! 题解

发布时间 2023-08-17 22:59:23作者: Code_Fish_Hp

题目链接

思路

大家应该都知道绝对值是什么吧?

那么,我们不妨直接设 \(a\gt b\),这样就省去了一次分类讨论的麻烦,大大降低了程序的复杂度。即可得到此二元一次含参方程组:

\[\begin{cases} a+b=s\\ a-b=t \end{cases} \]

运用二元一次方程的消元法,解得:

\[\begin{cases} a=\frac{s+t}{2}\\ b=\frac{s-t}{2} \end{cases} \]

题目要求 \(a\)\(b\) 必须为正整数,故当 \(s+t\) 为奇数或 \(s-t<0\) 时一定不满足要求,输出 impossible。

参考代码(请勿抄袭):

#include<bits/stdc++.h>//万能头
using namespace std;
int s,t,n;

int main(){
	scanf("%d",&n);//有n组数据
	for(int i=1;i<=n;i++){//每次数据的处理
		scanf("%d%d",&s,&t);//输入s,t
		((s+t)%2!=0||(s-t)<0)?puts("impossible"):printf("%d %d\n",(s+t)/2,(s-t)/2);//判断符不符合之前的推导,若不符合,输出impossible,否则直接用推出的公式
	}
	return 0;
}