CF1221A 2048 Game题解
【问题分析】
本题中只能相加,所以大于2048的数都没用,这使得本题有用的数据最大为2048, 所以可用桶排也是理所当然啦!用桶把小于等于2048的数都装进桶中从桶1开始把可以相加的数都进上去,直到1024,因为1024是相加小于等于2048中的最大数。
【设计程序】
#include<bits/stdc++.h>
#include<iostream>
#include<stdio.h>
#include<cstdio>
#include<queue>
using namespace std;
const int N = 3000 + 5;
int a[N];
int n, t, sum;
int main()
{
int Q;
scanf ("%d", &Q);
while(Q--)//Q次询问
{
scanf ("%d", &n);
memset (a, 0, sizeof(a));//每次清空桶
for (int i = 0;i < n; i++)
{
scanf ("%d", &t); //输入t
if(t <= 2048)//如果小于等于2048
a[t]++;//装入桶中
}
for (int i = 1;i < 2048; i *= 2)//小于2048的数
{
a[i * 2] += a[i] / 2;
//i的两倍的数量加上有几组2个i
}
if(a[2048] > 0)//如果有2048
printf ("YES\n");//输出YES
else//否则
printf ("NO\n");//输出NO
}
return 0;
}
【代码调试】
-
测试样例
-
自测数据(边界值,特殊值)
自测:
2
11
1024 512 256 128 64 32 16 8 4 2 1
11
1024 512 256 128 64 32 16 8 4 2 2
完结撒花

浙公网安备 33010602011771号