zoukankan      html  css  js  c++  java
  • 1090-Rock, Paper, Scissors

    描述

     

    Rock, Paper, Scissors is a classic hand game for two people. Each participant holds out either a fist (rock), open hand (paper), or two-finger V (scissors). If both players show the same gesture, they try again. They continue until there are two different gestures. The winner is then determined according to the table below: Rock beats Scissors    Paper beats Rock   
    Scissors beats Paper  Your task is to take a list of symbols representing the gestures of two players and determine how many games each player wins. In the following example:

    Turn     : 1 2 3 4 5

    Player 1 : R R S R S

    Player 2 : S R S P S

    Player 1 wins at Turn 1 (Rock beats Scissors), Player 2 wins at Turn 4 (Paper beats Rock), and all the other turns are ties.

    输入

    The input contains between 1 and 20 pairs of lines, the first for Player 1 and the second for Player 2. Both player lines contain the same number of symbols from the set {'R', 'P', 'S'}. The number of symbols per line is between 1 and 75, inclusive.  A pair of lines each containing the single character 'E' signifies the end of the input.

    输出

    For each pair of input lines, output a pair of output lines as shown in the sample output, indicating the number of games won by each player.

    样例输入

    RRSRS

    SRSPS

    PPP

    SSS

    SPPSRR

    PSPSRS

    E

    E

    样例输出

    P1: 1

    P2: 1

    P1: 0

    P2: 3

    P1: 2

    P2: 1

    #include<iostream>
    #include<string>
    using namespace std;
    
    void compare(char a,char b,int &count1,int &count2)
    {
        if(a=='R')
        {
            if(b=='S') count1++;
            if(b=='P') count2++;
        }
        if(a=='P')
        {
            if(b=='R') count1++;
            if(b=='S') count2++;
        }
        if(a=='S')
        {
            if(b=='P') count1++;
            if(b=='R') count2++;
        }
    }
    int main()
    {
        string s1,s2;
        while(cin>>s1>>s2&&(s1!="E"&&s2!="E"))
        {
            int len=s1.length();
            int i;
            int count1=0;
            int count2=0;
            for(i=0;i<len;i++)
            {
                compare(s1[i],s2[i],count1,count2);
            }
            cout<<"P1: "<<count1<<endl;
            cout<<"P2: "<<count2<<endl;
        }
        return 0;
    } 
    

      

  • 相关阅读:
    箭头函数和普通函数的区别是什么?
    前端如何优化网站性能?
    instanceof原理
    call、apply区别
    函数的节流和防抖
    关于this的指向性问题
    undefined 和null的区别?
    浅谈堆和栈的理解?
    关于vue中watch和computed
    简单说一下什么是回流和重绘
  • 原文地址:https://www.cnblogs.com/Rosanna/p/3436872.html
Copyright © 2011-2022 走看看