问题如下(附链接):无尽的石头
解题思路:本题的实质就是看1之后的各个位数相加是否等于目标数值,是则输出步数,若不等直接输出-1
题解代码如下:
#include <bits/stdc++.h>
using namespace std;
int main()
{
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int t;
cin >> t;
while (t--)
{
int n;
cin >> n;
int ret = 0, ans = 1, count = 1, num = 0;
while (ans < n)
{
while (count)
{
ret += count % 10;
count /= 10;
}
ans += ret;
count = ans;
num++;
ret = 0;
}
if (ans == n)
cout << num << '\n';
else
cout << -1 << '\n';
}
}