A.fence
Description
Yura is tasked to build a closed fence in shape of an arbitrary non-degenerate simple quadrilateral. He’s already got three straight fence segments with known lengths a, b, and c. Now he needs to find out some possible integer length d of the fourth straight fence segment so that he can build the fence using these four segments. In other words, the fence should have a quadrilateral shape with side lengths equal to a, b, c, and d. Help Yura, find any possible length of the fourth side.
A non-degenerate simple quadrilateral is such a quadrilateral that no three of its corners lie on the same line, and it does not cross itself.
Input
The first line contains a single integer t — the number of test cases (1≤t≤1000). The next t lines describe the test cases.
Each line contains three integers a, b, and c — the lengths of the three fence segments (1≤a,b,c≤109).
Output
For each test case print a single integer d — the length of the fourth fence segment that is suitable for building the fence. If there are multiple answers, print any. We can show that an answer always exists.
Example
inputCopy
2
1 2 3
12 34 56
outputCopy
4
42
Note
We can build a quadrilateral with sides 1, 2, 3, 4.
We can build a quadrilateral with sides 12, 34, 56, 42.
题意: 给出三条边,要求另外一条边,使得这条边与给出的前三条边构成四边形。输出该条边。
题解: 了解一下构成四边形的条件就迎刃而解了。四边形构成条件
c++ AC 代码
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
typedef long long ll;
int main()
{
int t;
scanf("%d", &t);
while (t--)
{
ll res;
std::vector<ll> v(3);
scanf("%lld%lld%lld", v.data(), v.data() + 1, v.data() + 2);
sort(v.begin(), v.end());
res = v[2] - v[1] - v[0] + 1;
if (res < 0)
{
if (v[0] + v[1] >= v[2])
res = 1;
else
{
for (int i = 1; i < v[2]; i++)
if (i + v[0] + v[1] > v[2])
{
res = i;
break;
}
}
}
else if(!res)
res = 2;
printf("%lld\n", res);
}
return 0;
}