2048 Game

柯苗宣
2023-12-01

You are playing a variation of game 2048. Initially you have a multiset of 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 , remove them from and insert the number equal to their sum into .

For example, if and you choose integers and , then the multiset becomes .

You win if the number belongs to your multiset. For example, if you can win as follows: choose and , your multiset turns into . Then choose and , your multiset turns into and you win.

You have to determine if you can win this game.

You have to answer independent queries.

Input
The first line contains one integer () – the number of queries.

The first line of each query contains one integer () — the number of elements in multiset.

The second line of each query contains integers () — 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 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
Input
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
Output
YES
YES
NO
NO
YES
YES
Note
In the first query you can win as follows: choose and , and turns into . Then choose and , and turns into and you win.

In the second query contains initially.

题意:在数列中找两个相同的数相加,然后更新数列,继续找两个相同的数相加,最后看数列里有没有2048。

#include<cstdio>
#include<cmath>
#include<cstring>
using namespace std;

int v[15];
bool judge(int x)
{
 //for(int i=0;i<15;i++)
//v[i]=0;
int s=log2(x);//求出所给的数是2
              //的几次幂
if(s<=13)
v[s]++;//因为到2048(11次幂),就设的s<=13时,看v[s]有几个
//int a1=v[s];
for(int i=0;i<=10;i++)//因为2048是2的11次幂,i就到10就可以了
{
	//int d=v[i];
    while(v[i]>=2)//当v[i]可以被更新的时候,就一直更新
    {
    	//int d1=v[i+1];
        v[i+1]=v[i+1]+v[i]/2;//更新v[i]+1的值
//int d2=v[i+1];
        v[i]=(v[i]-(v[i]/2)*2);//更新v[i]的值(如v[i]=5,更新时:v[i]=5-(5/2)*2,5/2在int类型中=2,
                               //所以为5-4)
    }
}
if(v[11]>0)//如果2048存在
return true;
else
return false;
}
int main()
{
int T;
scanf("%d",&T);
int n,m;

while(T--)
{
    bool flag=false;//一开始位置加在了外面,就一直跑不出来
   for(int i=0;i<15;i++)
   v[i]=0;//把v[i]的值更新为0
    scanf("%d",&n);
    while(n--)
    {
        scanf("%d",&m);
        if(judge(m))//判断有没有2048
        {
            flag=true;
        }
    }
    if(flag)
    printf("YES\n");
    else
    {
        printf("NO\n");
    }
}
return 0;

}
可以的话,给个赞?
欢迎指教orz

 类似资料:

相关阅读

相关文章

相关问答