我可以在C ++中获得此问题的代码吗? [英] Can I have the code for this problem in C++?

查看:69
本文介绍了我可以在C ++中获得此问题的代码吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尼克参加一个装配好的石头剪刀比赛。在每场比赛中,他都知道他的对手将要打什么,以及预定的结果(他赢,输或抽)。请帮助他确定每回合应该玩什么。



输入格式

第一行输入是n,游戏数量。后跟一个n个字符串的列表,形式为C S。 C是O,[]或8 <,意味着对手将分别为该回合演奏摇滚,纸张或剪刀。 S是W,L或D,意味着该回合的预定结果是Bob分别赢,输或抽。



输出格式

输出n个字符串,O,[]或8<,用逗​​号分隔,Bob应该在该回合中播放。



样本输入

n = 5

games = [8< W,[] W,OD,8< ; L,OW]

样本输出

O,8<,O,[],[]

说明
对于第一场比赛,对手打算剪刀,而鲍勃应该赢,所以他打摇滚。

对于第三场比赛,对手打算打摇滚鲍勃应该和他的对手一起画,所以他也会玩摇滚。

对于第四场比赛,对手打算剪刀,但鲍勃应该输,所以他打纸。



我尝试了什么:



我在C ++中尝试过,但它不起作用。

Nick join a rigged rock paper scissors tournament. In every game, he knows what his opponent is going to play, and the predetermined outcome (he wins, loses, or draws). Please help him determine what should he play for each turn.

Input Format
The first line of input is n, the number of games. Followed by a list of n strings, in the form of "C S". C is either "O", "[]", or "8<", meaning the opponent is going to play rock, paper, or scissors respectively for that turn. S is either "W", "L", or "D", meaning the predetermined outcome for that turn is Bob wins, loses, or draws respectively.

Output Format
Output n strings, either "O", "[]", or "8<", separated by commas, the move Bob should be playing for that turn.

Sample Input
n = 5
games = ["8< W", "[] W", "O D", "8< L", "O W"]
Sample Output
O,8<,O,[],[]
Explanation
For the first game, the opponent is going to play scissor, and Bob is supposed to win, so he plays rock.
For the third game, the opponent is going to play rock, and Bob is supposed to draw with his opponent, so he plays rock as well.
For the fourth game, the opponent is going to play scissors, but Bob is supposed to lose, so he plays paper.

What I have tried:

I tried in C++,but it didn't work.

int main()
{
const char* ROCK = "O";
const char* PAPER = "[]";
const char* SCISSORS = "8<";
 
const char* CurrentMove (char C, char S)
{
   const char* Result = 0;
 
   switch (S)     // Outer decision, based upon the desired result
   {
   case 'W':      // Win
      switch (C)  // Inner decision, based upon the opponent's move
      {
      case 'O':   Result = PAPER;      break;   // ROCK[0]
      case '[':   Result = SCISSORS;   break;   // PAPER[0]
      default:    Result = ROCK;                // Must be SCISSORS[0]
      }
   case 'L':      // Lose
      switch (C)  // Inner decision, based upon the opponent's move
      {
      case 'O':   Result = SCISSORS;   break;   // ROCK[0]
      case '[':   Result = ROCK;       break;   // PAPER[0]
      default:    Result = PAPER;               // Must be SCISSORS[0]
      }
   default:       // Must be Draw
      switch (C)  // Inner decision, based upon the opponent's move
      {
      case 'O':   Result = ROCK;       break;   // ROCK[0]
      case '[':   Result = PAPER;      break;   // PAPER[0]
      default:    Result = SCISSORS;            // Must be SCISSORS[0]
      }
   }
   return Result;
}
}

推荐答案

int main() {
   play(paper);
   play(something);
   play(scissors);
   play(paper);
   return 0;
   }



希望这有助于您入门< / look-in-cheek>或者它可能会让你疯狂到自己尝试,并向我展示一个***洞(对我而言)成功的一个混蛋。或者至少对解决方案进行了很好的尝试。


Hope this helps get you started < /tongue-in-cheek > Or maybe it will make you mad enough to try on your own, and show this a***hole (being me) what a jerk I am by succeeding. Or at least taking a good stab at the solution.


没有。我们不做你的功课:这是有原因的。它就是为了让你思考你被告知的事情,并试着理解它。它也在那里,以便您的导师可以识别您身体虚弱的区域,并将更多的注意力集中在补救措施上。



亲自尝试,你可能会发现它不是和你想的一样困难!



如果遇到具体问题,请询问相关问题,我们会尽力提供帮助。但是我们不会为你做这一切!
No. We do not do your homework: it is set for a reason. It is there so that you think about what you have been told, and try to understand it. It is also there so that your tutor can identify areas where you are weak, and focus more attention on remedial action.

Try it yourself, you may find it is not as difficult as you think!

If you meet a specific problem, then please ask about that and we will do our best to help. But we aren't going to do it all for you!


Quote:

我可以拥有用C ++解决这个问题的代码?

...

我在C ++中尝试过,但它没有用。

Can I have the code for this problem in C++?
...
I tried in C++,but it didn't work.



这个网站是供程序员使用的,所以我们帮你修复你的代码,但我们不做你的HomeWork。

所以显示你的代码并解释问题所在。

家庭工作问题是你在现实生活中必须解决的问题的简化版本,他们的目的是学习和练习



我们不做你的HomeWork。

HomeWork不会测试你乞求别人做你的工作的技巧,它是让您思考并帮助您的老师检查您对所学课程的理解以及您应用这些课程时遇到的问题。

你的任何失败都会帮助你的老师发现你的弱点并设定补救措施。

你的任何失败都会帮助你了解什么有效,什么无效,被称为'试错'学习。

所以,试一试,重读课程并开始工作。如果您遇到特定问题,请显示您的代码并解释这个问题,我们可能会提供帮助。



作为程序员,您的工作是创建算法解决特定问题,你不能依赖别人永远为你做,所以有一段时间你必须学会​​如何。而且越快越好。

当你要求解决方案时,就像试图通过培训其他人来学习开车一样。

创建算法基本上是找到数学并做出必要的调整以适应你的实际问题。



[更新]

- 此代码不起作用,因为它不是C ++,你不能在另一个函数中定义一个函数。

- 这段代码不起作用,因为它不处理任何用户输入或显示结果。

- 此代码不起作用,因为 CurrentMove 已嵌套开关,您还需要 break for outer switch


This site is for programmers, so we help you to fix your code, but we don't do your HomeWork.
So show your code and explain what is the problem.
HomeWork problems are simplified versions of the kind of problems you will have to solve in real life, their purpose is learning and practicing.

We do not do your HomeWork.
HomeWork is not set to test your skills at begging other people to do your work, it is set to make you think and to help your teacher to check your understanding of the courses you have taken and also the problems you have at applying them.
Any failure of you will help your teacher spot your weaknesses and set remedial actions.
Any failure of you will help you to learn what works and what don't, it is called 'trial and error' learning.
So, give it a try, reread your lessons and start working. If you are stuck on a specific problem, show your code and explain this exact problem, we might help.

As programmer, your job is to create algorithms that solve specific problems and you can't rely on someone else to eternally do it for you, so there is a time where you will have to learn how to. And the sooner, the better.
When you just ask for the solution, it is like trying to learn to drive a car by having someone else training.
Creating an algorithm is basically finding the maths and make necessary adaptation to fit your actual problem.

[Update]
- This code don't work because it is not C++, you can't define a function inside another function.
- This code don't work because it do not handle any user input or display a result.
- This code don't work because CurrentMove have nested switch and you also need to break for outer switch.


这篇关于我可以在C ++中获得此问题的代码吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆