第十二届国防科技大学程序设计竞赛(同步赛)-E题解

链接:https://ac.nowcoder.com/acm/contest/35627/E
来源:牛客网
 

时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 262144K,其他语言524288K
64bit IO Format: %lld

题目描述

PaiGuDragon participated in the game of fake-blackjack.

Each card is written with a number from 111 to 101010. In the game, the PaiGuDragon can choose "shoot" or "stay".

If selecting "shoot",  PaiGuDragon will draw the card at the top of the stack.

 
If selecting "stay",  PaiGuDragon will end draw and count the sum of points.

If the total number of cards drawn exceeds 212121, then PaiGuDragon lose everything!

On the premise that the sum of points is less than or equal to 212121, the greater the sum of points, the stronger the card power.

As a clairvoyant, PaiGuDragon can see every card in the stack from top to bottom. So how many cards will the smart PaiGuDragon choose to draw to get the best result?

输入描述:

The first line contains an interger n(1≤n≤20)n(1 \le n \le 20)n(1≤n≤20) - the number of cards.
The second line contains nnn intergers - card points from top to bottom, each number is an interger between 111 and 101010.

输出描述:

Print a single integer, the number of cards PaiGuDragon will draw.

示例1

输入

3
10 9 3

输出

2

说明

In the first test case, PaiGuDragon will draw 222 cards and get 10+9=1910 + 9 = 1910+9=19 points. If it draws 333 cards, the sum will exceed 212121.

示例2

输入

6

1 2 3 4 5 6

输出

6

说明

In the second test case, PaiGuDragon will draw all cards, and get 1+2+3+4+5+6=211 + 2 + 3 + 4 +5 + 6 = 211+2+3+4+5+6=21 points!

题意

在n张牌中选取cnt张牌,使得cnt张牌的和小于等于21,输出cnt的最大值

#include
using namespace std;
int a[21];
int main(){
    int sum=0,cnt=0;
    int n;
    cin>>n;
    for(int i=0;i>x;
        a[i]=x;
    }
    for(int i=0;i21){
            break;
        }else{
            sum+=a[i];
            cnt++;
        }
    }
    cout<

你可能感兴趣的:(算法)