Steps to reduce N to zero by subtracting its most significant digit at every step | C ++ program to find the count of Steps to reduce N to zero by subtracting its most significant digit at every step ; Function to count the number of digits in a number m ; Function to count the number of steps to reach 0 ; count the total number of stesp ; iterate till we reach 0 ; count the digits in last ; decrease it by 1 ; find the number on whose division , we get the first digit ; first digit in last ; find the first number less than last where the first digit changes ; find the number of numbers with same first digit that are jumped ; count the steps ; the next number with a different first digit ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countdig ( int m ) { if ( m == 0 ) return 0 ; else return 1 + countdig ( m / 10 ) ; } int countSteps ( int x ) { int c = 0 ; int last = x ; while ( last ) { int digits = countdig ( last ) ; digits -= 1 ; int divisor = pow ( 10 , digits ) ; int first = last / divisor ; int lastnumber = first * divisor ; int skipped = ( last - lastnumber ) / first ; skipped += 1 ; c += skipped ; last = last - ( first * skipped ) ; } return c ; } int main ( ) { int n = 14 ; cout << countSteps ( n ) ; return 0 ; }