CSU-暑假集训题 Reachable Numbers

题目链接:http://codeforces.com/problemset/problem/1157/A

题目

Let's denote a function f(x) in such a way: we add 1 to x

, then, while there is at least one trailing zero in the resulting number, we remove that zero. For example,

  • f(599)=6

: 599+1=600606

  • ;
  • f(7)=8

: 7+1=8

  • ;
  • f(9)=1

: 9+1=101

  • ;
  • f(10099)=101

: 10099+1=101001010101

  • .

We say that some number y

is reachable from x if we can apply function f to x some (possibly zero) times so that we get y as a result. For example, 102 is reachable from 10098 because f(f(f(10098)))=f(f(10099))=f(101)=102

; and any number is reachable from itself.

You are given a number n

; your task is to count how many different numbers are reachable from n.

Input

The first line contains one integer n (1n109).

 

Output

Print one integer: the number of different numbers that are reachable from n.

Example
    Input
1098
Output
20
Input
10
Output
19


思路

就是根据题意模拟这个过程就行。我用的set集合,可以去重。

AC代码

#include<iostream>
#include<set>
using namespace std;
int getNum(int n);
int main()
{
    int n,num;
    cin>>n;
    set<int>aa;
    aa.insert(n);
    num=n;
    while(1){
        num+=1;
        num=getNum(num);
        if(aa.find(num)==aa.end())aa.insert(num);
        else break;
    }
    cout<<aa.size()<<endl;
    return 0;
}
int getNum(int n)
{
    while(!(n%10))     //之前写!n%10 这样是错误的 
    {
        n=n/10;
    }
    return n;
} 

 

posted @ 2019-07-26 19:06  小小笼包包  Views(240)  Comments(0)    收藏  举报