NC237151. Black Jack
描述
输入描述
The first line contains an interger - the number of cards.
The second line contains intergers - card points from top to bottom, each number is an interger between and .
输出描述
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 cards and get points. If it draws cards, the sum will exceed .示例2
输入:
6 1 2 3 4 5 6
输出:
6
说明:
In the second test case, PaiGuDragon will draw all cards, and get 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)