列表

详情


NC237151. Black Jack

描述

PaiGuDragon participated in the game of fake-blackjack.

Each card is written with a number from 1 to 10. 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 21, then PaiGuDragon lose everything!

On the premise that the sum of points is less than or equal to 21, 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 - the number of cards.
The second line contains n intergers - card points from top to bottom, each number is an interger between 1 and 10.

输出描述

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 2 cards and get 10 + 9 = 19 points. If it draws 3 cards, the sum will exceed 21.

示例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 = 21 points!

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

C 解法, 执行用时: 2ms, 内存消耗: 352K, 提交时间: 2022-06-04 12:33:47

#include <stdio.h>

int main(){
	int n,i,m,t,max;
	t=max=0;
	scanf("%d",&n);
	for(i=0;i<n;i++){
		scanf("%d",&m);
		max+=m;
		if(max<=21)
			t++;
	}
	printf("%d",t);
	return 0;
}

C++ 解法, 执行用时: 4ms, 内存消耗: 460K, 提交时间: 2022-06-04 13:30:26

#include<iostream>
using namespace std;
int main(){int n,ans;cin>>n;for(int i=1,s=0,x;i<=n;i++){cin >> x; s+=x;if(s<=21)ans=i;}cout<<ans;return 0;}

Python3 解法, 执行用时: 45ms, 内存消耗: 4728K, 提交时间: 2022-06-06 09:59:51

i=h=0
input()
for x in map(int,input().split()):
    if h+x<22:
        h+=x
        i+=1
    else:
        break
print(i)

上一题