📜  通过加一并删除尾随的零来计算可以从N生成的唯一数字

📅  最后修改于: 2021-05-04 12:56:13             🧑  作者: Mango

给定数字N。在第一步中将数字加一个,如果数字后缀为零,则在第二步中删除所有后缀零。继续该过程以获取下一个生成的号码。任务是计算可以从这些操作中生成的唯一编号的数量。

例子:

方法:可以使用递归解决问题。使用unordered_set存储所有唯一编号。如果一个数字到达两次,我们将结束递归,因为将重复相同的序列,并且不再获得任何唯一的数字。否则将数字插入集合中,并在第一步中将数字增加1,并在下一步中删除所有尾随零(如果有的话)。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
// Function to count the unique numbers
void count_unique(unordered_set& s, int n)
{
  
    // If the number has
    // already been visited
    if (s.count(n))
        return;
  
    // Insert the number to the set
    s.insert(n);
  
    // First step
    n += 1;
  
    // Second step
    // remove trailing zeros
    while (n % 10 == 0) {
        n = n / 10;
    }
  
    // Recur again for the new number
    count_unique(s, n);
}
  
// Driver code
int main()
{
    int n = 10;
    unordered_set s;
    count_unique(s, n);
  
    cout << s.size();
  
    return 0;
}


Java
// Java implementation of the approach
import java.util.*;
  
class GFG 
{
  
// Function to count the unique numbers
static void count_unique(HashSets, int n)
{
  
    // If the number has
    // already been visited
    if (s.contains(n))
        return;
  
    // Insert the number to the set
    s.add(n);
  
    // First step
    n += 1;
  
    // Second step
    // remove trailing zeros
    while (n % 10 == 0)
    {
        n = n / 10;
    }
  
    // Recur again for the new number
    count_unique(s, n);
}
  
// Driver code
public static void main(String[] args)
{
    int n = 10;
    HashSets = new HashSet<>();
    count_unique(s, n);
    System.out.println(s.size());
}
}
  
// This code has been contributed by 29AjayKumar


Python3
# Python3 implementation of the approach 
  
# Function to count the unique numbers 
def count_unique(s, n) : 
  
    # If the number has 
    # already been visited 
    if (s.count(n)) :
        return;
  
    # Insert the number to the set 
    s.append(n); 
  
    # First step 
    n += 1; 
  
    # Second step 
    # remove trailing zeros 
    while (n % 10 == 0) : 
        n = n // 10; 
  
    # Recur again for the new number 
    count_unique(s, n);
  
  
# Driver code 
if __name__ == "__main__" :
  
    n = 10
    s = []
      
    count_unique(s, n)
      
    print(len(s))
  
# This code is contributed by Ryuga


C#
// C# implementation of the approach
using System;
using System.Collections.Generic;
  
class GFG 
{
  
// Function to count the unique numbers
static void count_unique(HashSets, int n)
{
  
    // If the number has
    // already been visited
    if (s.Contains(n))
        return;
  
    // Insert the number to the set
    s.Add(n);
  
    // First step
    n += 1;
  
    // Second step
    // remove trailing zeros
    while (n % 10 == 0)
    {
        n = n / 10;
    }
  
    // Recur again for the new number
    count_unique(s, n);
}
  
// Driver code
public static void Main(String[] args)
{
    int n = 10;
    HashSets = new HashSet();
    count_unique(s, n);
    Console.WriteLine(s.Count);
}
}
  
// This code contributed by Rajput-Ji


输出:
19