热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

下一个数字不同的数字

下一个数字不同的数字原文:https://www.geesf

下一个数字不同的数字

原文:https://www . geesforgeks . org/next-number-with-distinct-digits/

给定一个整数 N ,任务是找到下一个数字,其中有不同的数字。

示例:

输入: N = 20
输出:21
20 后所有不同数字的下一个整数是 21。

输入:N = 2019
T3】输出: 2031

进场:


  1. 使用本文中讨论的方法计算数字 N 中的总位数。

  2. 计算 N 中不同数字的总数。

  3. 如果总位数和 N 中不同位数的计数相等,则返回该数,否则将该数增加 1,并重复前面的步骤。

下面是上述方法的实现:

C++

// C++ program to find next consecutive
// Number with all distinct digits
#include
using namespace std;
// Function to count distinct
// digits in a number
int countDistinct(int n)
{
    // To count the occurrence of digits
    // in number from 0 to 9
    int arr[10] = { 0 };
    int count = 0;
    // Iterate over the digits of the number
    // Flag those digits as found in the array
    while (n) {
        int r = n % 10;
        arr[r] = 1;
        n /= 10;
    }
    // Traverse the array arr and count the
    // distinct digits in the array
    for (int i = 0; i <10; i++) {
        if (arr[i])
            count++;
    }
    return count;
}
// Function to return the total number
// of digits in the number
int countDigit(int n)
{
    int c = 0;
    // Iterate over the digits of the number
    while (n) {
        int r = n % 10;
        c++;
        n /= 10;
    }
    return c;
}
// Function to return the next
// number with distinct digits
int nextNumberDistinctDigit(int n)
{
    while (n         // Count the distinct digits in N + 1
        int distinct_digits = countDistinct(n + 1);
        // Count the total number of digits in N + 1
        int total_digits = countDigit(n + 1);
        if (distinct_digits == total_digits) {
            // Return the next consecutive number
            return n + 1;
        }
        else
            // Increment Number by 1
            n++;
    }
    return -1;
}
// Driver code
int main()
{
    int n = 2019;
    cout <    return 0;
}

Java 语言(一种计算机语言,尤用于创建网站)

// Java program to find next consecutive
// Number with all distinct digits
class GFG
{
    final static int INT_MAX = Integer.MAX_VALUE ;
    // Function to count distinct
    // digits in a number
    static int countDistinct(int n)
    {
        // To count the occurrence of digits
        // in number from 0 to 9
        int arr[] = new int[10];
        int count = 0;
        // Iterate over the digits of the number
        // Flag those digits as found in the array
        while (n != 0)
        {
            int r = n % 10;
            arr[r] = 1;
            n /= 10;
        }
        // Traverse the array arr and count the
        // distinct digits in the array
        for (int i = 0; i <10; i++)
        {
            if (arr[i] != 0)
                count++;
        }
        return count;
    }
    // Function to return the total number
    // of digits in the number
    static int countDigit(int n)
    {
        int c = 0;
        // Iterate over the digits of the number
        while (n != 0)
        {
            int r = n % 10;
            c++;
            n /= 10;
        }
        return c;
    }
    // Function to return the next
    // number with distinct digits
    static int nextNumberDistinctDigit(int n)
    {
        while (n         {
            // Count the distinct digits in N + 1
            int distinct_digits = countDistinct(n + 1);
            // Count the total number of digits in N + 1
            int total_digits = countDigit(n + 1);
            if (distinct_digits == total_digits)
            {
                // Return the next consecutive number
                return n + 1;
            }
            else
                // Increment Number by 1
                n++;
        }
        return -1;
    }
    // Driver code
    public static void main (String[] args)
    {
        int n = 2019;
        System.out.println(nextNumberDistinctDigit(n));
    }
}
// This code is contributed by AnkitRai01

Python 3

# Python3 program to find next consecutive
# Number with all distinct digits
import sys
INT_MAX = sys.maxsize;
# Function to count distinct
# digits in a number
def countDistinct(n):
    # To count the occurrence of digits
    # in number from 0 to 9
    arr = [0] * 10;
    count = 0;
    # Iterate over the digits of the number
    # Flag those digits as found in the array
    while (n != 0):
        r = int(n % 10);
        arr[r] = 1;
        n //= 10;
    # Traverse the array arr and count the
    # distinct digits in the array
    for i in range(10):
        if (arr[i] != 0):
            count += 1;
    return count;
# Function to return the total number
# of digits in the number
def countDigit(n):
    c = 0;
    # Iterate over the digits of the number
    while (n != 0):
        r = n % 10;
        c+=1;
        n //= 10;
    return c;
# Function to return the next
# number with distinct digits
def nextNumberDistinctDigit(n):
    while (n         # Count the distinct digits in N + 1
        distinct_digits = countDistinct(n + 1);
        # Count the total number of digits in N + 1
        total_digits = countDigit(n + 1);
        if (distinct_digits == total_digits):
            # Return the next consecutive number
            return n + 1;
        else:
            # Increment Number by 1
            n += 1;
    return -1;
# Driver code
if __name__ == '__main__':
    n = 2019;
    print(nextNumberDistinctDigit(n));
# This code is contributed by PrinciRaj1992

C

// C# program to find next consecutive
// Number with all distinct digits
using System;
class GFG
{
    readonly static int INT_MAX = int.MaxValue ;
    // Function to count distinct
    // digits in a number
    static int countDistinct(int n)
    {
        // To count the occurrence of digits
        // in number from 0 to 9
        int []arr = new int[10];
        int count = 0;
        // Iterate over the digits of the number
        // Flag those digits as found in the array
        while (n != 0)
        {
            int r = n % 10;
            arr[r] = 1;
            n /= 10;
        }
        // Traverse the array arr and count the
        // distinct digits in the array
        for (int i = 0; i <10; i++)
        {
            if (arr[i] != 0)
                count++;
        }
        return count;
    }
    // Function to return the total number
    // of digits in the number
    static int countDigit(int n)
    {
        int c = 0;
        // Iterate over the digits of the number
        while (n != 0)
        {
            int r = n % 10;
            c++;
            n /= 10;
        }
        return c;
    }
    // Function to return the next
    // number with distinct digits
    static int nextNumberDistinctDigit(int n)
    {
        while (n         {
            // Count the distinct digits in N + 1
            int distinct_digits = countDistinct(n + 1);
            // Count the total number of digits in N + 1
            int total_digits = countDigit(n + 1);
            if (distinct_digits == total_digits)
            {
                // Return the next consecutive number
                return n + 1;
            }
            else
                // Increment Number by 1
                n++;
        }
        return -1;
    }
    // Driver code
    public static void Main(String[] args)
    {
        int n = 2019;
        Console.WriteLine(nextNumberDistinctDigit(n));
    }
}
// This code is contributed by PrinciRaj1992

java 描述语言


Output

2031

另一种方法:

我们可以使用 set STL 来检查一个数字是否只有唯一的数字,而不是每次都计算数字的数量。

然后我们可以比较由给定数字和新创建的集合形成的字符串的大小。

例如,让我们考虑数字 1987,然后我们可以将该数字转换为字符串,

C++

int n;
cin>>n;
string s = to_string(n);

之后,用字符串 s 的内容初始化一个集合。

C++

set uniDigits(s.begin(), s.end());

然后我们可以比较字符串 s 的大小和新创建的一组单音数字。

这是总代码

C++

// CPP program for the above program
#include
using namespace std;
// Function to find next number
// with digit distinct
void nextNumberDistinctDigit(int n)
{
    // Iterate from n + 1 to inf
    for (int i = n + 1;; i++) {
        // Convert the no. to
        // string
        string s = to_string(i);
        // Convert string to set using stl
        set uniDigits(s.begin(), s.end());
        // Output if condition satisfies
        if (s.size() == uniDigits.size()) {
            cout <            break;
        }
    }
}
// Driver Code
int main()
{
    int n = 2019; // input the no.
    // Function Call
    nextNumberDistinctDigit(n);
    return 0;
}

Output

2031

推荐阅读
  • 目录实现效果:实现环境实现方法一:基本思路主要代码JavaScript代码总结方法二主要代码总结方法三基本思路主要代码JavaScriptHTML总结实 ... [详细]
  • 本文主要解析了Open judge C16H问题中涉及到的Magical Balls的快速幂和逆元算法,并给出了问题的解析和解决方法。详细介绍了问题的背景和规则,并给出了相应的算法解析和实现步骤。通过本文的解析,读者可以更好地理解和解决Open judge C16H问题中的Magical Balls部分。 ... [详细]
  • 本文介绍了P1651题目的描述和要求,以及计算能搭建的塔的最大高度的方法。通过动态规划和状压技术,将问题转化为求解差值的问题,并定义了相应的状态。最终得出了计算最大高度的解法。 ... [详细]
  • Google Play推出全新的应用内评价API,帮助开发者获取更多优质用户反馈。用户每天在Google Play上发表数百万条评论,这有助于开发者了解用户喜好和改进需求。开发者可以选择在适当的时间请求用户撰写评论,以获得全面而有用的反馈。全新应用内评价功能让用户无需返回应用详情页面即可发表评论,提升用户体验。 ... [详细]
  • 先看官方文档TheJavaTutorialshavebeenwrittenforJDK8.Examplesandpracticesdescribedinthispagedontta ... [详细]
  • Java容器中的compareto方法排序原理解析
    本文从源码解析Java容器中的compareto方法的排序原理,讲解了在使用数组存储数据时的限制以及存储效率的问题。同时提到了Redis的五大数据结构和list、set等知识点,回忆了作者大学时代的Java学习经历。文章以作者做的思维导图作为目录,展示了整个讲解过程。 ... [详细]
  • 本文讨论了如何优化解决hdu 1003 java题目的动态规划方法,通过分析加法规则和最大和的性质,提出了一种优化的思路。具体方法是,当从1加到n为负时,即sum(1,n)sum(n,s),可以继续加法计算。同时,还考虑了两种特殊情况:都是负数的情况和有0的情况。最后,通过使用Scanner类来获取输入数据。 ... [详细]
  • 本文介绍了在Java中gt、gtgt、gtgtgt和lt之间的区别。通过解释符号的含义和使用例子,帮助读者理解这些符号在二进制表示和移位操作中的作用。同时,文章还提到了负数的补码表示和移位操作的限制。 ... [详细]
  • 本文介绍了如何在给定的有序字符序列中插入新字符,并保持序列的有序性。通过示例代码演示了插入过程,以及插入后的字符序列。 ... [详细]
  • baresip android编译、运行教程1语音通话
    本文介绍了如何在安卓平台上编译和运行baresip android,包括下载相关的sdk和ndk,修改ndk路径和输出目录,以及创建一个c++的安卓工程并将目录考到cpp下。详细步骤可参考给出的链接和文档。 ... [详细]
  • 如何用UE4制作2D游戏文档——计算篇
    篇首语:本文由编程笔记#小编为大家整理,主要介绍了如何用UE4制作2D游戏文档——计算篇相关的知识,希望对你有一定的参考价值。 ... [详细]
  • 使用在线工具jsonschema2pojo根据json生成java对象
    本文介绍了使用在线工具jsonschema2pojo根据json生成java对象的方法。通过该工具,用户只需将json字符串复制到输入框中,即可自动将其转换成java对象。该工具还能解析列表式的json数据,并将嵌套在内层的对象也解析出来。本文以请求github的api为例,展示了使用该工具的步骤和效果。 ... [详细]
  • XML介绍与使用的概述及标签规则
    本文介绍了XML的基本概念和用途,包括XML的可扩展性和标签的自定义特性。同时还详细解释了XML标签的规则,包括标签的尖括号和合法标识符的组成,标签必须成对出现的原则以及特殊标签的使用方法。通过本文的阅读,读者可以对XML的基本知识有一个全面的了解。 ... [详细]
  • 本文详细介绍了Java中vector的使用方法和相关知识,包括vector类的功能、构造方法和使用注意事项。通过使用vector类,可以方便地实现动态数组的功能,并且可以随意插入不同类型的对象,进行查找、插入和删除操作。这篇文章对于需要频繁进行查找、插入和删除操作的情况下,使用vector类是一个很好的选择。 ... [详细]
  • 本文介绍了深入浅出Linux设备驱动编程的重要性,以及两种加载和删除Linux内核模块的方法。通过一个内核模块的例子,展示了模块的编译和加载过程,并讨论了模块对内核大小的控制。深入理解Linux设备驱动编程对于开发者来说非常重要。 ... [详细]
author-avatar
elgin2010
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有