hihoCode #1338 : A Game (区间dp)

#1338 : A Game

Time Limit: 10000ms
Case Time Limit: 1000ms
Memory Limit: 256MB

Description

Little Hi and Little Ho are playing a game. There is an integer array in front of them. They take turns (Little Ho goes first) to select a number from either the beginning or the end of the array. The number will be added to the selecter's score and then be removed from the array.

Given the array what is the maximum score Little Ho can get? Note that Little Hi is smart and he always uses the optimal strategy. 

Input

The first line contains an integer N denoting the length of the array. (1 ≤ N ≤ 1000)

The second line contains N integers A1A2, ... AN, denoting the array. (-1000 ≤ Ai ≤ 1000) 

Output

Output the maximum score Little Ho can get.

Sample Input
4
-1 0 100 2
Sample Output
99

#include<bits/stdc++.h>
using namespace std;
const int maxn=1003;
int s[maxn],dp[maxn][maxn],a[maxn];
int main()
{
int n;
scanf("%d",&n);
memset(dp,0,sizeof(dp));
a[0]=-10000;
s[0]=0;
for(int i=1;i<=n;i++)
{
    scanf("%d",&a[i]);
    s[i]=s[i-1]+a[i];
    dp[i-1][i]=max(a[i],a[i-1]);
    dp[i][i]=a[i];
}
for(int i=n;i>=1;i--)
    for(int j=i+2;j<=n;j++)
dp[i][j]=s[j] - s[i - 1]+(dp[i+1][j]>dp[i][j-1]?-dp[i][j - 1]:-dp[i + 1][j]);
printf("%d\n",dp[1][n]);
    return 0;
}



好的,我会按照你的要求使用面向对象的方式来完成这个任务。下面是我给出的代码实现: ```python import random class Game: def __init__(self): self.player_score = 0 self.computer_score = 0 def player_move(self): move = input("请输入你的拳:石头、剪刀或布:") while move not in ['石头', '剪刀', '布']: move = input("输入有误,请重新输入:石头、剪刀或布:") return move def computer_move(self): moves = ['石头', '剪刀', '布'] return random.choice(moves) def start(self): print("游戏开始!") for _ in range(3): player_move = self.player_move() computer_move = self.computer_move() print("你出拳:%s,电脑出拳:%s" % (player_move, computer_move)) if player_move == computer_move: print("平局!") elif (player_move == '石头' and computer_move == '剪刀') or \ (player_move == '剪刀' and computer_move == '布') or \ (player_move == '布' and computer_move == '石头'): self.player_score += 10 print("你赢了!得分+10") else: self.computer_score += 10 print("电脑赢了!得分+10") print("游戏结束!") if self.player_score > self.computer_score: print("你赢得了比赛!") elif self.player_score < self.computer_score: print("电脑赢得了比赛!") else: print("比赛结束,平局!") print("你的得分:%d,电脑得分:%d" % (self.player_score, self.computer_score)) # 实例化游戏类并开始游戏 game = Game() game.start() ``` 运行上述代码,你可以与电脑进行三局积分的石头剪刀布游戏。玩家需要输入石头、剪刀或布,电脑会随机出拳。胜利一方会得到10分,三局后会出现最终结果。希望这个代码对你有帮助!如果有任何问题,请随时告诉我。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值