I - MaratonIME divides fairly
Problem Description
In a country trip, the contestants decided to play a soccer match. Yan, who was a professional player once, decided not to play to keep the teams balanced. He wanted to participate in another way, so he decided to choose the two teams.
Unfortunately, unlike soccer, Yan is very bad at math and doesn't know if he divided the teams fairly. Yan considers a division fair if the absolute difference between the number of players in each team is minimum. Can you help him?
Input
The first line has a single integer T, the number of test cases. The next T lines have two integers a and b, the number of players in each team.
- 1 ≤ T ≤ 1000.
- 0 ≤ a, b ≤ 109.
Output
Print T lines, one for each test case.
If Yan was fair, output the word "Ok".
If Yan wasn't fair, output two integers x and y, x ≤ y, the sizes of the teams in a fair division.
Example
Input
2 2 2 0 2Output
Ok 1 1
代码如下:
Hint:题目要求两队人数相差必须小于等于1,符合输出ok,不符合进行划分,注意题目要求 x <= y。数据量大用longlong存储。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
int T;
long long a, b, max, min;
scanf("%d", &T);
while(T--)
{
scanf("%lld %lld", &a, &b);
if(a > b)
{
max = a;
min = b;
}
else
{
max = b;
min = a;
}
if(max - min <= 1) // 如果差距 >=0 && <= 1那么认为是公平的
printf("Ok\n");
else
{
if((a + b) % 2 == 0) // 判断奇偶数,偶数直接对半分,奇数y多分一个
printf("%lld %lld\n", (a + b) / 2, (a + b) / 2);
else
printf("%lld %lld\n", (a + b) / 2, (a + b) / 2 + 1); // 左边小于右边
}
}
return 0;
}