Codeforces Round #643 (Div. 2)A. Sequence with Digits(模拟)

在这里插入图片描述
原题链接
题意:
给你一个数a,对他执行k次操作,每次操作让当前数a加上组成a的每位数中最大值与最小值的积。输出最后得到的结果。
思路:
一步步模拟就行,只要中间出现了0这位数立马break掉,因为出现0之后改数字再怎么加也不会变了。
代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int inf=0x3f3f3f3f;
const int N=2e5+10;
ll cal(ll x)
{
    ll temp=x,minx=inf,maxx=-1;
    while(temp!=0)
    {
        minx=min(minx,temp%10);
        maxx=max(maxx,temp%10);
        temp/=10;
    }
    return minx*maxx;
}
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    int t;
    cin>>t;
    while(t--)
    {
        ll a,k;
        cin>>a>>k;
        for(ll i=2;i<=k;i++)
        {
            ll c=cal(a);
            if(c==0) break;
            a+=c;
        }
        cout<<a<<endl;
    }
    return 0;
}
原创文章 76 获赞 78 访问量 1万+

猜你喜欢

转载自blog.csdn.net/amazingee/article/details/106173163