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;
}
没思路,参考了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;
}
相关文章推荐
- UVA 10125 - Sumsets(POJ 2549) hash
- UVA 10125 - Sumsets(POJ 2549) hash
- 【图染色】POJ 1419 & UVA 193
- uva 10125 Sumsets
- POJ 2528 & UVA 10587(线段树+离散+区间修改)
- HDU 2333 & POJ 3497 & UVA 12124 Assemble (二分答案)
- UVa 10127/POJ 2551 Ones (模运算&转换思想)
- poj 2549 Sumsets
- hdu 4071& poj 3873 & zoj 3386 & uva 12197 Trick or Treat 三分法
- UVa 439/HDU 1372/POJ 2243/ZOJ 1091 Knight Moves(BFS&纯数学方法)
- UVa 270 & POJ 1118 - Lining Up
- POJ 1903 & ZOJ 2469 & UVA 1326 Jurassic Remains (部分枚举)
- UVa 10042 && POJ 1142 - Smith Numbers
- UVA 10125 - Sumsets
- uva 10125 - Sumsets
- UVa 11054/HDU 1489/POJ 2940 Wine trading in Gergovia(贪心&双向队列)
- UVa 10125 - Sumsets
- UVa 106 && POJ 1305 - Fermat vs. Pythagoras(毕达哥拉斯三元组)
- uva 1394&poj 3517
- UVa 106/POJ 1305 Fermat vs. Pythagoras(数论&勾股数)