乒乓球代码

#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
    char c;
    string s;
    int h=0,sb=0;
    while(cin>>c){
        s+=c;
    }
    for(int i=0;i<s.size();i++){
        if(s[i]=='W'){
            h+=1;
        }
        else if(s[i]=='L'){
            sb++;
        }
        if(s[i]=='E'){
            cout<<h<<':'<<sb<<endl;
            break;
        }
        if(h>=11&&h-sb>=2){
            cout<<h<<':'<<sb<<endl;
            h=0;
            sb=0;
        }
        if(sb>=11&&sb-h>=2){
            cout<<h<<':'<<sb<<endl;
            h=0;
            sb=0;
        }
        if(s[i]=='E'){
            cout<<h<<':'<<sb<<endl;
            h=0;
            sb=0;
        }
    }
    cout<<endl;
    h=0;
    sb=0;
    for(int i=0;i<s.size();i++){
        if(s[i]=='W'){
            h+=1;
        }
        else if(s[i]=='L'){
            sb++;
        }
        if(s[i]=='E'){
            cout<<h<<':'<<sb<<endl;
            break;
        }
        if(h>=21&&h-sb>=2){
            cout<<h<<':'<<sb<<endl;
            h=0;
            sb=0;
        }
        if(sb>=21&&sb-h>=2){
            cout<<h<<':'<<sb<<endl;
            h=0;
            sb=0;
        }
        if(s[i]=='E'){
            cout<<h<<':'<<sb<<endl;
        }
    }
    return 0;
}
 

  • 8
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个简单的强化学习打乒乓球的示例代码: 首先,我们需要安装一些必要的库: ``` pip install gym pip install keras pip install tensorflow ``` 然后,我们可以使用 OpenAI 的 `gym` 库来创建一个打乒乓球的环境并进行学习。 ```python import gym import numpy as np from keras.models import Sequential from keras.layers import Dense, Activation, Flatten from keras.optimizers import Adam # 创建打乒乓球的环境 env = gym.make('Pong-v0') # 定义动作数量 action_size = env.action_space.n # 定义状态数量,这里我们将图像转换为灰度图并缩小到 80x80 state_size = (80, 80, 1) # 构建神经网络模型 def build_model(state_size, action_size): model = Sequential() model.add(Flatten(input_shape=state_size)) model.add(Dense(128)) model.add(Activation('relu')) model.add(Dense(256)) model.add(Activation('relu')) model.add(Dense(action_size)) model.add(Activation('softmax')) return model # 定义一个 agent 类来进行强化学习 class Agent: def __init__(self, state_size, action_size): self.state_size = state_size self.action_size = action_size self.model = build_model(state_size, action_size) self.gamma = 0.99 # 折扣因子 self.learning_rate = 0.001 self.states = [] self.actions = [] self.rewards = [] self.model.compile(loss='categorical_crossentropy', optimizer=Adam(lr=self.learning_rate)) # 选择动作 def act(self, state): state = state.reshape([1, *state.shape]) prob = self.model.predict(state).flatten() action = np.random.choice(self.action_size, 1, p=prob)[0] return action # 记录状态、动作和奖励 def remember(self, state, action, reward): self.states.append(state) self.actions.append(action) self.rewards.append(reward) # 训练模型 def train(self): # 计算折扣奖励 discounted_rewards = np.zeros_like(self.rewards) running_reward = 0 for t in reversed(range(len(self.rewards))): running_reward = running_reward * self.gamma + self.rewards[t] discounted_rewards[t] = running_reward # 归一化折扣奖励 discounted_rewards -= np.mean(discounted_rewards) discounted_rewards /= np.std(discounted_rewards) # 计算每个动作的概率 states = np.vstack(self.states) actions = np.array(self.actions) discounted_rewards = np.array(discounted_rewards) discounted_rewards = np.reshape(discounted_rewards, (discounted_rewards.shape[0],)) actions = np.eye(self.action_size)[actions] # 训练模型 self.model.fit(states, actions * discounted_rewards[:, None], epochs=1, verbose=0) # 清空状态、动作和奖励列表 self.states = [] self.actions = [] self.rewards = [] # 创建 agent agent = Agent(state_size, action_size) # 进行强化学习 for episode in range(1000): state = env.reset() score = 0 done = False while not done: action = agent.act(state) next_state, reward, done, _ = env.step(action) agent.remember(state, action, reward) state = next_state score += reward agent.train() print("Episode: {}, score: {}".format(episode, score)) ``` 这个示例代码可能需要一段时间才能训练出有效的模型,但是你可以根据需要增加神经网络的大小或修改学习率等参数来提高训练效果。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值