题意:
解法:
观察到k最大只有2,那么这题就是一道模拟题了,
按照题目的意思计算数位,相加,然后重复操作直到x<10即可.
code:
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int maxm=2e5+5;
int n,k;
int cal(int x){
int ans=0;
while(x){
ans+=x%10;
x/=10;
}
return ans;
}
void solve(){
cin>>n>>k;
int x;
if(k==1)x=n;
else x=n*n;
while(x>=10){
x=cal(x);
}
cout<<x<<endl;
}
signed main(){
ios::sync_with_stdio(0);
int T;cin>>T;
while(T--){
solve();
}
return 0;
}