您的位置:首页 > 其它

UVa 10125 & POJ 2549 - Sumsets

2014-05-28 10:59 411 查看
传送门UVa 10125 & POJ 2549 - Sumsets

没思路,参考了Primo_的解题报告。

非常好的思路。

直接用三重循环会超时,要将等式变形为a + b = d - c,然后从大到小枚举d。遇到符合条件的,输出。

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int MAXN = 1100;

int main()
{
//freopen("input.txt", "r", stdin);
int n, num[MAXN], i, j;
while (scanf("%d", &n), n)
{
bool flag = false;
for (i = 0; i < n; i++)
scanf("%d", &num[i]);
sort(num, num + n);
for (i = n - 1; i >= 0; i--)
{
for (j = n - 1; j >= 0; j--)
{
if (i != j)
{
int temp = num[i] - num[j];
for (int l = 0, k = j - 1; l < k;)
if (temp == num[l] + num[k])
{
flag = true;
break;
}
else if (temp < num[l] + num[k])
k--;
else
l++;
}
if (flag)
break;
}
if (flag)
break;
}
if (flag)
printf("%d\n", num[i]);
else
printf("no solution\n");
}
return 0;
}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ACM UVa POJ