题目描述
有一个函数f(x)f(x),效果是将x+1x+1后,去掉末尾所有的00,例如:
f(599)=6f(599)=6,因为599+1=600→60→6599+1=600→60→6
f(7)=8f(7)=8,因为7+1=87+1=8
f(9)=1f(9)=1,因为9+1=10→19+1=10→1
f(10099)=101f(10099)=101,因为10099+1=10100→1010→10110099+1=10100→1010→101
我们可以多次进行函数f(x)f(x)的运算,从而让一个数xx转换为另一个数,例如1009810098可以转换为102102,因为f(f(f(10098)))=f(f(10099))=f(101)=102f(f(f(10098)))=f(f(10099))=f(101)=102。
你需要做的是给你一个数nn,求出nn经过多次函数f(x)f(x)的计算,能转换为几个不同的数(包括自身)?
输入输出格式
输入格式
一个整数n \space (n \le 10^9)n (n≤109)。
输出格式
一个整数,代表nn能够转换为的不同数字的个数。
题目描述
Let's denote a function f(x)f(x) in such a way: we add 11 to xx , then, while there is at least one trailing zero in the resulting number, we remove that zero. For example,
- f(599) = 6f(599)=6 : 599 + 1 = 600 \rightarrow 60 \rightarrow 6599+1=600→60→6 ;
- f(7) = 8f(7)=8 : 7 + 1 = 87+1=8 ;
- f(9) = 1f(9)=1 : 9 + 1 = 10 \rightarrow 19+1=10→1 ;
- f(10099) = 101f(10099)=101 : 10099 + 1 = 10100 \rightarrow 1010 \rightarrow 10110099+1=10100→1010→101 .
We say that some number yy is reachable from xx if we can apply function ff to xx some (possibly zero) times so that we get yy as a result. For example, 102102 is reachable from 1009810098 because f(f(f(10098))) = f(f(10099)) = f(101) = 102f(f(f(10098)))=f(f(10099))=f(101)=102 ; and any number is reachable from itself.
You are given a number nn ; your task is to count how many different numbers are reachable from nn .
输入输出格式
输入格式:
The first line contains one integer nn ( 1 \le n \le 10^91≤n≤109 ).
输出格式:
Print one integer: the number of different numbers that are reachable from nn .
输入输出样例
输入样例#1
1098
输出样例#1
20
输入样例#2
10
输出样例#2
19
思路
不要被娜高端的f(x)吓到了,我们知道函数是个动态的过程,因此不用管它,直接按题意模拟即可。
这道题的本质:给一个数n,每一次n+1后,再去掉末尾的0,能得到多少个数(包括自身)。
显然对于每个数至少有9个,分别是1~9。进位以后的数将比原数小,所以可以直接循环。
#include <stdio.h>
#include <iostream>
using namespace std;
int n,s;
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin>>n;
while(n>=10)//10以内的数都可以取到,1~9都能取到9个数,所以从10开始
{
n++;
while(n%10==0)//直到不能被10整除
{
n/=10;
}
s++;//算符数!=
}
cout<<s+9<<endl;
return 0;
}