카타 8급 Rock Paper Scissors!

1 C++[ | ]

#include <string>
std::string rps(const std::string& p1, const std::string& p2)
{
    if(p1 == p2) return "Draw!";
    if( ( p1[0] == 's' && p2[0] == 'p' ) ||
        ( p1[0] == 'p' && p2[0] == 'r' ) ||
        ( p1[0] == 'r' && p2[0] == 's' ) )
        return "Player 1 won!";
    return "Player 2 won!";
}
 #include <string>
std::string rps(const std::string& p1, const std::string& p2)
{
  if(p1 == p2) return "Draw!";
  if (p1 == "scissors")  {
    if (p2 == "paper") return "Player 1 won!";
    return "Player 2 won!";
  }
  if (p1 == "paper") {
    if (p2 == "rock") return "Player 1 won!";
    return "Player 2 won!";
  }
  if (p2 == "scissors") return "Player 1 won!";
  return "Player 2 won!";
}
#include <string>
std::string rps(const std::string& p1, const std::string& p2)
{
  int a = p1.compare("rock");
  int b = p2.compare("rock");
  if( a == b ) return "Draw!";
  if( a < 0 ) {
    if( b == 0 ) return "Player 1 won!";
    return "Player 2 won!";
  }
  if( a == 0 ) {
    if( b < 0 ) return "Player 2 won!";
    return "Player 1 won!";
  }
  if( b < 0 ) return "Player 1 won!";
  return "Player 2 won!";
}

2 PHP[ | ]

function rpc ($p1, $p2) {
  if( $p1 == $p2 ) return 'Draw!';
  if( $p1 == 'rock' && $p2 == 'scissors' ) return 'Player 1 won!';
  if( $p1 == 'paper' && $p2 == 'rock' ) return 'Player 1 won!';
  if( $p1 == 'scissors' && $p2 == 'paper' ) return 'Player 1 won!';
  return 'Player 2 won!';
}
문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}