跳至正文

1 条题解

  • 0
    @ 2026-10-8 17:34:58

    【本题解由站长编写】

    用枚举表示三种手势:Rock=0(石头)、Paper=1(布)、Scissors=2(剪刀)。胜负关系用差值判断最简洁:胜负是循环的,所以 (x - y + 3) % 3 == 1 表示前者赢、== 2 表示后者赢、相等则平局。验证:0(石头) vs 2(剪刀),(0-2+3)%3=1 前者赢;1(布) vs 0(石头),(1-0+3)%3=1 前者赢;2(剪刀) vs 1(布),(2-1+3)%3=1 前者赢——正好是「布胜石头、石头胜剪刀、剪刀胜布」。

    代码(C++17)

    #include <bits/stdc++.h>
    using namespace std;
    
    enum Hand { Rock = 0, Paper = 1, Scissors = 2 };
    
    int main() {
        int x, y;
        cin >> x >> y;
        int d = (x - y + 3) % 3;
        if (d == 0) cout << 0 << endl;        // 平局
        else if (d == 1) cout << 1 << endl;   // 前者赢
        else cout << -1 << endl;              // 后者赢
        return 0;
    }
    
    • 1

    信息

    ID
    4136
    时间
    1000ms
    内存
    512MiB
    难度
    6
    标签
    递交数
    0
    已通过
    0
    上传者