题解:P17113 [Algo Beat 009 & MROI-R1] Number Game

· · 题解

入门题。

思路

跟着题目模拟。

讲一下最后怎么判谁赢。

S 先手,所以如果进行过的回合数是奇数,S 赢。否则 M 赢。

代码

#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N=1e5+10;
int t;
int n;
signed main(){
    ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
    cin>>t;
    while(t--){
        cin>>n;
        int cnt=0;
        string s=to_string(n); // 字符串,方便操作
        string a="";
        while(s!=a){ // 小 S 操作之后与小 M 不一样
            int sum=0;
            for(auto c:s){
                sum+=c-'0'; // 统计和
            }
            a=s; // 更新小 M
            s=to_string(sum); // 更新小 S
            cnt++; // 计算次数
        }
        if(cnt%2==0) cout<<"S"; // S win
        else cout<<"M"; // M win
        cout<<'\n';
    }
    return 0;
}