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