全新题目


【编程题目 |100分】翻牌求最大分【2022 Q2考试题】


时间限制:C/C++ 1秒,其他语言 2秒

空间限制:C/C++262144K,其他语言524288K

64bit IO Format:%lld


本题可使用本地IDE编码,不能使用本地已有代码,无跳出限制,

编码后请点击”保存并调试“按钮进行代码提交。


题目描述

给出n个牌数,在-100到100之间,求最大得分。

规则如下:连续翻牌,如果选当前牌,则总得分等于上一次翻牌总得分加上当前牌的数字,

如果当前总得分小于它前三次的总得分的话,那此次不翻牌,并且总得分就等于它前三次的得分。

1到3次翻牌数如果小于0的话就取0。

例子:1,-5,-6,4,7,2,-2


(1)1大于零 翻牌
(2)-5 加上1 小于0 不翻 结果为0
(3)-6 加上0 小于0 不翻 结果为0
(4)4 加上0 大于0(1)翻牌 结果为4
(5)7 加上4 大于0(2) 翻牌 结果为11
(6)2 加上11 大于0(3) 翻牌 结果为13
(7)-2 加上14 大于4(4)翻牌 结果为11


C++解法一


Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
#include<bits/stdc++.h>
using namespace std;
class Solution {
public:
int Poker(string &s)
{
string str(s);
vector<int> array;
SplitInt(str, array, ',');
#include<bits/stdc++.h> using namespace std; class Solution { public: int Poker(string &s) { string str(s); vector<int> array; SplitInt(str, array, ',');
#include<bits/stdc++.h>

using namespace std;

class Solution {
public:
    int Poker(string &s)
    {
        string str(s);
        vector<int> array;
        SplitInt(str, array, ',');

剩余50%内容,订阅会员后查看


隐藏内容

此处内容需要权限查看

  • 普通用户特权:11金币
  • 会员用户特权:免费
  • 永久会员用户特权:免费推荐
会员免费查看

C++解法二


Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
#include<bits/stdc++.h>
using namespace std;
#include<bits/stdc++.h> using namespace std;
#include<bits/stdc++.h> 

using namespace std;

剩余50%内容,订阅会员后查看


隐藏内容

此处内容需要权限查看

  • 普通用户特权:11金币
  • 会员用户特权:免费
  • 永久会员用户特权:免费推荐
会员免费查看

JAVA


Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
String[] split = s.split(",");
int[] arr = new int[split.length];
for (int i = 0; i < split.length; i++) {
arr[i] = Integer.parseInt(split[i]);
}
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String s = sc.nextLine(); String[] split = s.split(","); int[] arr = new int[split.length]; for (int i = 0; i < split.length; i++) { arr[i] = Integer.parseInt(split[i]); }
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String s = sc.nextLine();
        String[] split = s.split(",");
        int[] arr = new int[split.length];
        for (int i = 0; i < split.length; i++) {
            arr[i] = Integer.parseInt(split[i]);
        }

剩余50%内容,订阅会员后查看


隐藏内容

此处内容需要权限查看

  • 普通用户特权:11金币
  • 会员用户特权:免费
  • 永久会员用户特权:免费推荐
会员免费查看