模拟
没啥算法技巧,要说有的话最多就是拆数,但也不算难。只要按照题目意思一个一个找石头就行,没有技巧,就是暴力套三层while循环的事。只要注意细节,记得去更改下一个石头的值,确保每次都没有多加少加之类的就行。
#include <bits/stdc++.h>
using namespace std;
int main()
{
int t; cin >> t;
while (t--){
int n; cin >> n;
int ans = 0;
int s = 1, now = 1;
int flag = 0;
while (now <= n){
while (s != 0){
now += s % 10;
s /= 10;
}
ans++;
if (now == n){
flag = 1;
cout << ans << endl;
break;
}
s = now;
}
if (!flag) cout << -1 << endl;
}
return 0;
}