当前位置:网站首页>ZCMU--1098: 查找元素
ZCMU--1098: 查找元素
2022-07-15 12:55:00 【小小小Why】
Description
给定一个整数集合s,对于s里的不同元素a,b,c,d,找出一个最大的d,满足a+b+c=d.
元素不同定义为元素大小不同。
Input
多组测试数据,每组第一行为一个正整数n(1≤n≤1000)代表集合中元素的个数,第二行为n个整数代表集合里的元素.(每个整数的绝对值小于等于108)
Output
对于每组数据如果d存在输出最大的d,否则输出”no solution”.
Sample Input
5
2 3 5 7 12
5
2 16 64 256 1024
Sample Output
12
no solution
解析:这题暴力可以过,这题要求四个不同元素,所以我们可以先用set来提取出来不同元素,然后导入普通数组a[ ]中,而且set自动排序,遍历导出时候也不用重新sort排序,十分方便,然后进行四重for循环,找到满足式子break即可。
注意:元素可以是负数,四重for循环,a[ m-1 ]不一定是最大,所以x要从m-1开始遍历,然后再加上 i!=x && i !=y && i !=z
#include <stdio.h>
#include <set>
using namespace std;
int a[1005];
int main()
{
int n,i,m,l,s,x,y,z;
while(~scanf("%d",&n)){
set<int> st;
set<int>::iterator it;
m=0,s=0; //s用来判断是否存在满足式
for(i=0;i<n;i++){
scanf("%d",&l);
st.insert(l); //存入set,保存不同元素
}
for(it=st.begin();it!=st.end();it++) a[m++]=*it;//将set中元素存入a[],即已经没有重复元素
for(i=m-1;i>=0;i--){
for(x=m-1;x>=0;x--){//x要从m-1开始,因为元素可以为负数,a[i]不一定是最大
for(y=x-1;y>=0;y--){
for(z=y-1;z>=0;z--){
if((a[x]+a[y]+a[z]==a[i])&&i!=x&&i!=y&&i!=z){
s=1; //找到
printf("%d\n",a[i]);
break;
}
}
if(s==1) break;
}
if(s==1) break;
}
if(s==1) break;
}
if(s==0) printf("no solution\n");
}
return 0;
}边栏推荐
- 10分钟带你进入Swagger的世界,快来看一看吧
- Polardb for PostgreSQL is based on shared storage, multi computing and single storage
- Unp learning notes - Chapter 2 transport layer
- Fuxin software appeared at the 2022 national chemical enterprise digital intelligence transformation and Development Forum
- vulnhub Funbox: Easy (funbox3)
- Is it true that double non undergraduate students cannot enter the big factory? Ali technology four sides + cross face +hr face, successfully got the offer!!
- Is there a version of polardb for oracle?
- DEVKIT-mpc5744p配置rtos
- 授人以渔-在 SAP MM 物料显示界面上看到一个字段,如何查找哪张数据库表的哪个字段进行的存储
- 盖茨再捐200亿美元,谷歌云转投ARM,推特员工因马斯克遭CEO警告,今日更多大新闻在此...
猜你喜欢

论文学习(一)——MWP-BERT: Numeracy-Augmented Pre-training for Math WordProblem Solving

Quickly deploy mqtt clusters on Alibaba cloud using terraform

Two years of crud, two ordinary graduates, three months of challenge interview Ali, successfully won the offer grading P7! Annual salary 50W

vulnhub Funbox: Easy (funbox3)

Explore NTP clock synchronization (PTP time synchronization) of intelligent driving interval speed measurement

Once, I sprayed AI customer service for two minutes, and it only replied to my first sentence

The colleague next to me was suddenly promoted to meituan P7 because he secretly learned this JVM note?

光速募资超70亿美元 宓群:加大绿色科技与硬科技投资比重

曾经,我对着AI客服喷了两分钟,它只回复了我的第一句话

Where is the IBD file of MySQL in win11 virtual machine
随机推荐
Google Earth engine (GEE) - random forest classification has mapped the detailed code of mangrove app in Guyana in 2000, 2010 and 2020
Utonmos: how social metauniverse constructs digital world
Google Earth engine (GEE) -- abnormal value of S2 image
A concise course for designers of soft exam system architecture | enterprise informatization and e-commerce
After 00, he graduated from Nanjing University and planned to be a special associate researcher. Netizens quarreled after picking out their papers
AI简报-模型集成 SAM 和SWA
vulnhub Funbox: 1
[Voforia] 通过识自己设定图片,显示特定AR模型
Change buffer introduction
Explore NTP clock synchronization (PTP time synchronization) of intelligent driving interval speed measurement
Offstage in shutter
360预计上半年扣非净亏4.5亿至6.3亿 广告投放预算不及预期
A concise course for the architect of soft examination system | government informatization and e-government
AI briefing - model integration Sam and SWA
真的牛b!京东T3-2都还在学的微服务+MySQL+Kafka+boot2.x+虚拟机PDF
Do you know the architecture and engine of MySQL?
Is poalrdb for PostgreSQL 100% compatible with PostgreSQL itself?
Four sides Ali offer grading p82022 latest and most practical Ali 68 advanced interview questions to help you succeed in your interview!!
Is polardb for PostgreSQL completely open source?
leetcode:300. 最长递增子序列【LIS板子 + 贪心二分 + nlogn】