24h購物| | PChome| 登入
2013-05-29 16:31:38| 人氣1,555| 回應0 | 上一篇 | 下一篇

[UVA][Easy] 12614 - Earn For Future

推薦 0 收藏 0 轉貼0 訂閱站台


  Earn For Future! 

In a lazy afternoon the great Froogrammer came to realize that, to make his future plans successful he needs a lot of money. To make some quick cash he decided to go to the casino to play a game. The rule of the game is the following:

  • The player is given N cards. Each card has a non-negative integer printed in it.

  • The player will choose some cards from the given cards.

  • The bitwise AND value of the chosen cards will be calculated and the player will be given the same amount of money. (i.e. equal to the bitwise AND value of the chosen cards).

After getting N cards Froogrammer was in a fix as usual. He could not decide which of the cards to choose. So he called you to help him. Please tell him the maximum amount he can win from these set of cards. If you are confused about bitwise AND operation see the notes section below.

Input 

The first line of input will contain the number of test cases T (T < 101). Then there will be T test cases. Each of the test cases will start with an integer N ( 0 < N < 31) denoting the number of cards. Then the following line will contain N non-negative integers Ci ( 0$ le$Ci < 231) separated by space, denoting the numbers printed on each of the cards.

Output 

For each test case print one line of output denoting the case number and the maximum amount Froogrammer can win. See sample output for exact format.


Note:

A bitwise AND takes two binary representations of equal length and performs the logical AND operation on each pair of corresponding bits. The resulting bit of a position is 1 if the bit at that position of both numbers is 1; otherwise, that bit is 0.

For example:

        0101 (decimal 5)
    AND 0011 (decimal 3)
      = 0001 (decimal 1)

Sample Input 

1
2
0 1

Sample Output 

Case 1: 1


Problemsetter: Abu Obaida Opu
Special Thanks: Towhidul Islam Talukder


題目希望找到一個子集合,子集合內的所有數字進行 AND 之後越大越好。

這真心是個騙人的題目,AND 越多數字只會變小不會變大,因此找一個最大數字即可。


#include <stdio.h>
#include <algorithm>
using namespace std;
int main() {
    int t, cases = 0, n, x;
    scanf("%d", &t);
    while(t--) {
        scanf("%d", &n);
        int ret = 0;
        while(n--) {
            scanf("%d", &x);
            ret = max(ret, x);
        }
        printf("Case %d: %d\n", ++cases, ret);
    }
    return 0;
}


台長: Morris
人氣(1,555) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA][乘法反元素、Math] 12619 - Just Make A Wish
此分類上一篇:[UVA][Easy] 12626 - I ❤ Pizza

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文