zoukankan      html  css  js  c++  java
  • 2048Game

    刚刚做了一道2048game的题目

    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

    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 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的幂次,后来想到了用数列的方法,快捷方便

     1 #include<iostream>
     2 
     3 using namespace std;
     4 typedef long long ll;
     5 
     6 int log2(ll a) {
     7     int count = 0;
     8     while (1) {
     9         if (a >>= 1)
    10             count++;
    11         else
    12             break;
    13     }
    14     return count;
    15 }
    16 
    17 int main() {
    18     int q, n,temp;//表示多少组数以及每组数的个数
    19     long long input;
    20     cin >> q;
    21     while (q--) {
    22         int a[14] = { 0 };
    23         cin >> n;
    24         while (n--) {
    25             cin >> input;
    26             temp = log2(input);
    27             if (temp > 11)
    28                 continue;
    29             a[temp]++;
    30             while (a[temp] == 2) {
    31                 if(temp!=11)
    32                 a[temp] = 0;
    33                 a[++temp]++;
    34             }
    35             
    36         }
    37         if (a[11] >0) {
    38             cout << "YES" << endl;
    39         }else
    40             cout << "NO" << endl;
    41     }
    42     return 0;
    43 }
    View Code

    大概就是介样

  • 相关阅读:
    Post和Get的区别(兼谈页面间传值的方式)
    ClickOnce部署Winform程序的方方面面
    TSQL查询进阶深入浅出视图
    一个java volatile测试揭开的陷阱
    java volatile的一个验证反例
    [Swing扩展组件分享]为JTable添加选择列(CheckBox)的包装类
    JTextField限制输入长度的完美解决方案
    swing程序的关闭机制看好你的swing.Timer,别让它成为程序不能退出的原凶
    举例理解单元测试
    打印出txt中出现频率最高的十个词——软件工程个人项目C语言
  • 原文地址:https://www.cnblogs.com/zlszls3113373723/p/11700771.html
Copyright © 2011-2022 走看看