第十二届国防科技大学程序设计竞赛(同步赛)-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<bits/stdc++.h>
using namespace std;
int a[21];
int main(){
    int sum=0,cnt=0;
    int n;
    cin>>n;
    for(int i=0;i<n;i++){
        int x;
        cin>>x;
        a[i]=x;
    }
    for(int i=0;i<n;i++){
        if(a[i]+sum>21){
            break;
        }else{
            sum+=a[i];
            cnt++;
        }
    }
    cout<<cnt;
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

易江南20

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值