给定一个数字 。通过在每个步骤中用最高有效数字(最左位数)减去该数字,将其减少为零。任务是计算减少到零所需的步骤数。
例子:
Input: 14
Output: 6
Steps:
14 - 1 = 13
13 - 1 = 12
12 - 1 = 11
11 - 1 = 10
10 - 1 = 9
9 - 9 = 0
Input: 20
Output: 12
Numbers after series of steps:
20, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 0
幼稚的方法:幼稚的方法是逐步减少位数并找到步数,但是如果提供大量数字,则时间复杂度将很大。
高效方法:高效方法的主要思想是减少幼稚方法的步骤数。我们可以跳过前导数字在连续数字中相同的步骤,并对其进行计数。跳过那些具有相同前导数字的数字的算法如下:
- 让数字为最后一个,将最后一个数字计数,然后将其减少1,因为具有相同前导数字和相同数字计数的最小数字将具有该数字零。
- 通过last / count查找姓氏数的第一位。
- 因此,具有相同前导数字的相同位数的最小数目将是[第一位*(count-1)]
- 跳过的步数可以通过[(最后一个最小数)/第一位数]来实现。
- 因此,下一个数字倒数第二个将是倒数–(第一个*跳过)
下面是上述方法的实现:
C++
// C++ program to find the count of Steps to
// reduce N to zero by subtracting its most
// significant digit at every step
#include
using namespace std;
// Function to count the number
// of digits in a number m
int countdig(int m)
{
if (m == 0)
return 0;
else
return 1 + countdig(m / 10);
}
// Function to count the number of
// steps to reach 0
int countSteps(int x)
{
// count the total number of stesp
int c = 0;
int last = x;
// iterate till we reach 0
while (last) {
// count the digits in last
int digits = countdig(last);
// decrease it by 1
digits -= 1;
// find the number on whose division,
// we get the first digit
int divisor = pow(10, digits);
// first digit in last
int first = last / divisor;
// find the first number less than
// last where the first digit changes
int lastnumber = first * divisor;
// find the number of numbers
// with same first digit that are jumped
int skipped = (last - lastnumber) / first;
skipped += 1;
// count the steps
c += skipped;
// the next number with a different
// first digit
last = last - (first * skipped);
}
return c;
}
// Driver code
int main()
{
int n = 14;
cout << countSteps(n);
return 0;
}
Java
// Java program to find the count of Steps to
// reduce N to zero by subtracting its most
// significant digit at every step
class GFG{
// Function to count the number
// of digits in a number m
static int countdig(int m)
{
if (m == 0)
return 0;
else
return 1 + countdig(m / 10);
}
// Function to count the number of
// steps to reach 0
static int countSteps(int x)
{
// count the total number of stesp
int c = 0;
int last = x;
// iterate till we reach 0
while (last>0) {
// count the digits in last
int digits = countdig(last);
// decrease it by 1
digits -= 1;
// find the number on whose division,
// we get the first digit
int divisor = (int)Math.pow(10, digits);
// first digit in last
int first = last / divisor;
// find the first number less than
// last where the first digit changes
int lastnumber = first * divisor;
// find the number of numbers
// with same first digit that are jumped
int skipped = (last - lastnumber) / first;
skipped += 1;
// count the steps
c += skipped;
// the next number with a different
// first digit
last = last - (first * skipped);
}
return c;
}
// Driver code
public static void main(String[] args)
{
int n = 14;
System.out.println(countSteps(n));
}
}
// This code is contributed by mits
Python 3
# Python 3 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
def countdig(m) :
if (m == 0) :
return 0
else :
return 1 + countdig(m // 10)
# Function to count the number
# of steps to reach 0
def countSteps(x) :
# count the total number
# of stesp
c = 0
last = x
# iterate till we reach 0
while (last) :
# count the digits in last
digits = countdig(last)
# decrease it by 1
digits -= 1
# find the number on whose
# division, we get the first digit
divisor = pow(10, digits)
# first digit in last
first = last // divisor
# find the first number less
# than last where the first
# digit changes
lastnumber = first * divisor
# find the number of numbers
# with same first digit that
# are jumped
skipped = (last - lastnumber) // first
skipped += 1
# count the steps
c += skipped
# the next number with a different
# first digit
last = last - (first * skipped)
return c
# Driver code
n = 14
print(countSteps(n))
# This code is contributed by ANKITRAI1
C#
// C# program to find the count of Steps to
// reduce N to zero by subtracting its most
// significant digit at every step
using System;
class GFG{
// Function to count the number
// of digits in a number m
static int countdig(int m)
{
if (m == 0)
return 0;
else
return 1 + countdig(m / 10);
}
// Function to count the number of
// steps to reach 0
static int countSteps(int x)
{
// count the total number of stesp
int c = 0;
int last = x;
// iterate till we reach 0
while (last>0) {
// count the digits in last
int digits = countdig(last);
// decrease it by 1
digits -= 1;
// find the number on whose division,
// we get the first digit
int divisor = (int)Math.Pow(10, digits);
// first digit in last
int first = last / divisor;
// find the first number less than
// last where the first digit changes
int lastnumber = first * divisor;
// find the number of numbers
// with same first digit that are jumped
int skipped = (last - lastnumber) / first;
skipped += 1;
// count the steps
c += skipped;
// the next number with a different
// first digit
last = last - (first * skipped);
}
return c;
}
// Driver code
static void Main()
{
int n = 14;
Console.WriteLine(countSteps(n));
}
}
// This code is contributed by mits
PHP
输出:
6