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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章