0

0

根据数字的优先级,翻译如下:基于数字优先级的下一个更大的数字

王林

王林

发布时间:2023-08-28 08:45:10

|

1363人浏览过

|

来源于tutorialspoint

转载

根据数字的优先级,翻译如下:基于数字优先级的下一个更大的数字

在正常的数字系统中,0是最小的数字,而9是最大的数字。在这个问题中,我们将得到一个长度为10的列表,从索引0到索引9表示一个数字,它表示该数字的优先级,列表将按照递增顺序排列,这意味着出现在最后索引的数字具有最高的优先级。我们还将得到一个数字,我们需要找到比当前数字稍大的下一个数字。

Input 1: 
Given number = “123”
Given array = { 9, 2, 3, 6, 8, 1, 0, 5, 4, 7}
Output: 132

Explanation

从给定的优先级数组中,我们可以看到1的优先级大于2和3。与2相比,3的优先级更高。因此,如果给定数字的下一个排列将通过交换2和3来实现。

Input 2:
Given number = 132
Given array = { 9, 2, 3, 6, 8, 1, 0, 5, 4, 7}
Output: 132

Explanation

From the given priority array, we can see that the priority of 1 is greater than both 2 and 3. Priority of 3 is greater as compared to of 2. So, there will be no next permutation available.

方法

In this approach, we will use the concept of the standard template library (STL) provided by the C++ programming language to get the next permutation. Let us see the steps −

  • 首先,我们将得到下一个数字的数字,以及一个按升序表示数字优先级的数组。

  • 我们将调用预定义的函数来找到大于当前给定数字的下一个数字。

  • We will define a function that take the parameter given number and array.

  • We will define a global array and store the priority of each digit taken from the given array to use later.

    知了追踪
    知了追踪

    AI智能信息助手,智能追踪你的兴趣资讯

    下载
  • 我们将把给定的数字转换为字符串,以便在其上应用下一个排列函数。

  • We will define a custom function that will used to compare the characters of the string in the next permutation function of the stl.

  • 获取下一个排列后,我们将把字符串转换为整数并返回。

Example

的中文翻译为:

示例

#include 
using namespace std;
// priority array or array in which the priority of each digit will be stored 
int prio[10];
// defining the compare function 
bool compare(char& a, char& b){
   return prio[a-'0'] < prio[b-'0'];
}
// function to get the next permuatation 
int nextNum(int n, int arr[]){
   for(int i=0; i<10; i++){
      prio[arr[i]] = i;
   }
   // converting the given number into string 
   string str = to_string(n);
   // calling the next permuatation stl function for the given string we will compare by custom function 
   bool cur = next_permutation(str.begin(),str.end(),compare);
   if(cur == false){
      // indicating the next permutation does not exist 
      return n;
   }
   n = stoi(str); // converting string back to number 
   return n;
}
int main(){
   int n = 312; // the given number 
   int arr[] = {9, 2, 3, 6, 8, 1, 0, 5, 4, 7}; // given array
   cout<<"The next number or permutation for the given number is: "<

Output

The next number or permutation for the given number is: 123

Time and Space Complexity

The time complexity of the above code is O(N), where N is the number of digits in the given number.

The space complexity of the above code is O(N) because we are creating a new string to use the next permutation function.

Note: If the current number is the greater number among all the permutations then the next permutation function will return false and we will return the same number as the next permutation does not exist.

结论

在本教程中,我们实现了一个代码来找到下一个数字,该数字比当前数字大,但数字的优先级不是从0到9的顺序,而是在单独的数组中给出。我们使用了STL函数next_permutation和一个自定义函数来根据新的优先级获取下一个数字。以上代码的时间和空间复杂度为O(数字的位数)。

相关专题

更多
string转int
string转int

在编程中,我们经常会遇到需要将字符串(str)转换为整数(int)的情况。这可能是因为我们需要对字符串进行数值计算,或者需要将用户输入的字符串转换为整数进行处理。php中文网给大家带来了相关的教程以及文章,欢迎大家前来学习阅读。

312

2023.08.02

typedef和define区别
typedef和define区别

typedef和define区别在类型检查、作用范围、可读性、错误处理和内存占用等。本专题为大家提供typedef和define相关的文章、下载、课程内容,供大家免费下载体验。

100

2023.09.26

define的用法
define的用法

define用法:1、定义常量;2、定义函数宏:3、定义条件编译;4、定义多行宏。更多关于define的用法的内容,大家可以阅读本专题下的文章。

313

2023.10.11

if什么意思
if什么意思

if的意思是“如果”的条件。它是一个用于引导条件语句的关键词,用于根据特定条件的真假情况来执行不同的代码块。本专题提供if什么意思的相关文章,供大家免费阅读。

712

2023.08.22

js 字符串转数组
js 字符串转数组

js字符串转数组的方法:1、使用“split()”方法;2、使用“Array.from()”方法;3、使用for循环遍历;4、使用“Array.split()”方法。本专题为大家提供js字符串转数组的相关的文章、下载、课程内容,供大家免费下载体验。

248

2023.08.03

js截取字符串的方法
js截取字符串的方法

js截取字符串的方法有substring()方法、substr()方法、slice()方法、split()方法和slice()方法。本专题为大家提供字符串相关的文章、下载、课程内容,供大家免费下载体验。

205

2023.09.04

java基础知识汇总
java基础知识汇总

java基础知识有Java的历史和特点、Java的开发环境、Java的基本数据类型、变量和常量、运算符和表达式、控制语句、数组和字符串等等知识点。想要知道更多关于java基础知识的朋友,请阅读本专题下面的的有关文章,欢迎大家来php中文网学习。

1435

2023.10.24

字符串介绍
字符串介绍

字符串是一种数据类型,它可以是任何文本,包括字母、数字、符号等。字符串可以由不同的字符组成,例如空格、标点符号、数字等。在编程中,字符串通常用引号括起来,如单引号、双引号或反引号。想了解更多字符串的相关内容,可以阅读本专题下面的文章。

609

2023.11.24

php源码安装教程大全
php源码安装教程大全

本专题整合了php源码安装教程,阅读专题下面的文章了解更多详细内容。

3

2025.12.31

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
PHP自制框架
PHP自制框架

共8课时 | 0.6万人学习

简单聊聊mysql8与网络通信
简单聊聊mysql8与网络通信

共1课时 | 777人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号