石头剪刀布

题目描述:R-石头 S-剪刀 P-布,每个人都按周期来出,问n把后双方各输了多少次

#include <deque>
#include <unordered_map>
#include <stack>
#include <algorithm>
#include <string>
#include <vector>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <queue>
#include <unordered_set>
#include <utility>
#include <limits.h>
using namespace std;
int gcd(int x, int y)
{
    while(x!=y){
        if(x>y) x=x-y;
        else y=y-x;
    }
    return x;
}
int main()
{
    int n, x, y,one_len, i, j;
    string s1, s2, str1, str2;
    cin >> n >> s1 >> s2;
    x = s1.length();
    y = s2.length();
    one_len = x * y / gcd(x, y);
    str1 = s1;
    str2 = s2;
    while (str1.length() < one_len)
    {
        str1 += s1;
    }
    while (str2.length() < one_len)
    {
        str2 += s2;
    }
    int repeat_time = n / one_len;
    int remain_len = n % one_len;
    int t1=0,t2=0;
    for (i=0; i < remain_len; i++)
    {
        if(str1[i]=='R'){
            if(str2[i]=='S')
                t1++;
            else if(str2[i]=='P')
                t2++;
        }
        else if(str1[i]=='S'){
            if(str2[i]=='P')
                t1++;
            else if(str2[i]=='R')
                t2++;
        }
        else if(str1[i]=='P'){
            if(str2[i]=='R')
                t1++;
            else if(str2[i]=='S')
                t2++;
        }
    }
    int ans1 = t1, ans2 = t2;
    if(repeat_time)
    for(;i<one_len;i++){
        if(str1[i]=='R'){
            if(str2[i]=='S')
                ans1++;
            else if(str2[i]=='P')
                ans2++;
        }
        else if(str1[i]=='S'){
            if(str2[i]=='P')
                ans1++;
            else if(str2[i]=='R')
                ans2++;
        }
        else if(str1[i]=='P'){
            if(str2[i]=='R')
                ans1++;
            else if(str2[i]=='S')
                ans2++;
        }
    }
    ans1=ans1*repeat_time+t1;
    ans2=ans2*repeat_time+t2;
    cout<<ans2<<" "<<ans1<<endl;
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Jihye Lee

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值