题目来自:http://218.5.5.242:9018/JudgeOnline/problem.php?id=1278

题目描述

给你一个整数的集合S(里面所有的整数均不相同),请你找出最大的 d,使得 a + b + c = d。在这里a,b,c,d均为S中的数且a,b,c,d是不同的数。

输入

每组测试资料的第一行有1个整数 n(1 <= n <= 1000),代表S中元素的个数。接下来的n行,每行有一个整数xi,代表S中的各元素。-536870912 <= xi <= +536870911.n=0时代表输入结束,请参考Sample Input。

输出

对每一组测试资料,输出d。如果找不到则输出no solution。

样例输入

5
2
3
5
7
12
5
2
16
64
256
1024
0

样例输出

12
no solution

作者分析:这道题我们可以先排序,算出最大的和,然后暴力求解。
#include <bits/stdc++.h>
using namespace std; int main(){
int ans[100001],t = 0;
while (true){
int n;
cin >> n;
if (n == 0){
break;
}
t++;
int a[n+1];
for (int i = 1;i <= n;i++){
cin >> a[i];
}
sort(a,a+n+1);
int max = -1;
for (int i = n;i >= 1;i--){
for (int j = 1;j <= i;j++){
for (int k = j+1;k <= i;k++){
for (int l = k + 1;l <= i;l++){
if (a[j] + a[k] + a[l] == a[i]){
ans[t] = a[i];
max = 0;
break;
}
}
if (max == 0){
break;
}
}
if (max == 0){
break;
}
}
if (max == 0){
break;
}
}
if (max == -1){
ans[t] = -1;
}
}
for (int i = 1;i <= t;i++){
if (ans[i] == -1){
cout << "no solution" << endl;
continue;
}
cout << ans[i] << endl;
}
}
 

最新文章

  1. 说说Makefile那些事儿
  2. Command and Query Responsibility Segregation (CQRS) Pattern 命令和查询职责分离(CQRS)模式
  3. 李洪强iOS经典面试题130
  4. Objective-C:模拟按钮点击事件理解代理模式
  5. 请实现一个函数用来找出字符流中第一个只出现一次的字符。例如,当从字符流中只读出前两个字符&quot;go&quot;时,第一个只出现一次的字符是&quot;g&quot;。当从该字符流中读出前六个字符“google&quot;时,第一个只出现一次的字符是&quot;l&quot;。
  6. 如果在安装32位oracle 客户端组件时的情况下以64位模式运行,将出现问题
  7. Java finally语句到底是在return之前还是之后执行(JVM字节码分析及内部体系结构)?
  8. linux group
  9. curl 返回响应头
  10. Spring知识点回顾(02)AOP
  11. MFC程序设计小结
  12. css3混合模式
  13. 弹窗查看内容时 内容滚动区域设置为body区
  14. (转)Sphinx中文分词安装配置及API调用
  15. Ubuntu 安装google chrome
  16. Tutorials on Inverse Reinforcement Learning
  17. Linux配置多个Tomcat同时运行
  18. NOIP 2000 计算器的改良
  19. Netty源码分析第8章(高性能工具类FastThreadLocal和Recycler)----&gt;第1节: FastThreadLocal的使用和创建
  20. HDOJ 5019 Revenge of GCD

热门文章

  1. Spring Boot 系列总结
  2. chm打开看不到内容时好时坏
  3. rand()函数 不同区间 整数和浮点数
  4. Linux-yum安装和相关命令
  5. EF6.2加载速度慢的解决方案
  6. 1153 Decode Registration Card of PAT
  7. 2. Linear Regression with One Variable
  8. Codeforces Global Round 9 D. Replace by MEX
  9. hdu5497 Inversion
  10. poj2362 Square