CodeForces-1221A-2048 Game-思维题
You are playing a variation of game 2048. Initially you have a multiset ss of nn integers. Every integer in this multiset is a power of two.
You may perform any number (possibly, zero) operations with this multiset.
During each operation you choose two equal integers from ss, remove them from ss and insert the number equal to their sum into ss.
For example, if s={1,2,1,1,4,2,2}s={1,2,1,1,4,2,2} and you choose integers 22 and 22, then the multiset becomes {1,1,1,4,4,2}{1,1,1,4,4,2}.
You win if the number 20482048 belongs to your multiset. For example, if s={1024,512,512,4}s={1024,512,512,4} you can win as follows: choose 512512 and 512512, your multiset turns into {1024,1024,4}{1024,1024,4}. Then choose 10241024 and 10241024, your multiset turns into {2048,4}{2048,4} and you win.
You have to determine if you can win this game.
You have to answer qq independent queries.
Input
The first line contains one integer qq (1≤q≤1001≤q≤100) – the number of queries.
The first line of each query contains one integer nn (1≤n≤1001≤n≤100) — the number of elements in multiset.
The second line of each query contains nn integers s1,s2,…,sns1,s2,…,sn (1≤si≤2291≤si≤229) — the description of the multiset. It is guaranteed that all elements of the multiset are powers of two.
Output
For each query print YES if it is possible to obtain the number 20482048 in your multiset, and NO otherwise.
You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES will all be recognized as positive answer).
Example
6
4
1024 512 64 512
1
2048
3
64 512 2
2
4096 4
7
2048 2 2048 2048 2048 2048 2048
2
2048 4096
YES
YES
NO
NO
YES
YES
Note
In the first query you can win as follows: choose 512512 and 512512, and ss turns into {1024,64,1024}{1024,64,1024}. Then choose 10241024 and 10241024, and ss turns into {2048,64}{2048,64} and you win.
In the second query ss contains 20482048 initially.
题意:
给出一串数,所有的数全部都是2的次方。判断能否组成2048,里面的出现的数字可以由两个相同数字的和进行替换,比如2,2,8可以变成4,8。能输入YES,否则输出NO。
思维点:
首先要判断给的数据中有没有2048,要是存在2048就可以直接输出YES了,否则继续判断。
2048=2^11,1024=2^10,可以发现2的0次方一直加到2的10次方的和刚好是2047,如果说累加出来的sum大于2047,也就是说大于等于2048的话即可,
但是需要注意的是一旦遇到一个大于2048的,就不能进行累加。
1 #include<stdio.h> 2 #include<iostream> 3 #include<algorithm> 4 #include<string.h> 5 6 typedef long long ll; 7 using namespace std; 8 9 ll a[110],b[110]; 10 11 int main() 12 { 13 int t; 14 scanf("%d",&t); 15 while(t--) 16 { 17 int n; 18 scanf("%d",&n); 19 int flag=0; 20 for(int i=0;i<n;i++) 21 { 22 scanf("%lld",&a[i]); 23 if(a[i]==2048) 24 flag=1; 25 } 26 if(flag) 27 printf("YES\n"); 28 else 29 { 30 sort(a,a+n); 31 ll sum=0; 32 int flag=1; 33 for(int i=0;i<n;i++) 34 { 35 if(a[i]>=2048) 36 continue; 37 sum+=a[i]; 38 39 } 40 if(sum>=2048) 41 printf("YES\n"); 42 else 43 printf("NO\n"); 44 } 45 } 46 return 0; 47 }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· DeepSeek 开源周回顾「GitHub 热点速览」