首頁  >  文章  >  後端開發  >  根據數字的優先級,翻譯如下:基於數位優先級的下一個較大的數字

根據數字的優先級,翻譯如下:基於數位優先級的下一個較大的數字

王林
王林轉載
2023-08-28 08:45:101182瀏覽

根據數字的優先級,翻譯如下:基於數位優先級的下一個較大的數字

在正常的數字系統中,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.

  • 我們將把給定的數字轉換為字串,以便在其上應用下一個排列函數。

  • 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 <bits/stdc++.h>
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: "<<nextNum(n, arr);
   return 0;
}

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.#does not#結論

在本教程中,我們實作了一個程式碼來找到下一個數字,該數字比目前數字大,但數字的優先順序不是從0到9的順序,而是在單獨的陣列中給出。我們使用了STL函數next_permutation和一個自訂函數來根據新的優先權取得下一個數字。以上碼的時間和空間複雜度為O(數字的位數)。

以上是根據數字的優先級,翻譯如下:基於數位優先級的下一個較大的數字的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:tutorialspoint.com。如有侵權,請聯絡admin@php.cn刪除