题解:P9354 「SiR-1」Popsicle

· · 题解

解题思路

对于猫猫,每次只能将一个雪糕棒的数字减 1。在没有小老鼠影响的情况下,操作次数为 n 的各位数字之和。

对于小老鼠,只能改变一个数字,要使操作次数尽量多:

参考代码

#include <bits/stdc++.h>
using namespace std;

using ll=long long;
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int T;
    cin>>T;
    while(T--)
    {
        ll n;
        cin>>n;
        int sum=0;
        bool k=0;
        while(n)
        {
            if(n%10==0)k=1;
            sum+=n%10;
            n/=10;
        }
        cout<<sum+(k?9:8)<<'\n';
    }
    return 0;
}