题目描述:
解题思路:
预处理:建立一个数组a[N], 初始为-1。 计算符合的石头的编号和步数,进行下一步操作,a[编号]=步数。于是输入编号就可得到对应步数。
题解:
#include<bits/stdc++.h>
using namespace std;
const int N = 1e6 + 9;//大小为编号范围+9,int范围为0~1e9
int a[N];
int main()
{
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
memset(a, -1, sizeof(a));
//vector等容器不能使用memset初始化,只有数组才能使用。且初始化的之只能是0或1
int now = 1, cnt = 0;
while(now < N)
{
a[now] = cnt++;//第一个没有走动,所以cnt初始为0
int x = now;
int sum = 0;//sum为每位数相加之和
while(x)//计算sum
{
sum += x%10;
x = x/10;
}
now += sum;
a[now] = cnt;
}
int t; cin >> t;
while(t--)
{
int x; cin >> x;
cout << a[x] << "\n";
}
return 0;
}