📌  相关文章
📜  查找可以使用给定数字的位数形成的最大数字

📅  最后修改于: 2021-05-07 01:30:52             🧑  作者: Mango

给定一个数字,编写一个程序以查找可以使用该数字的所有数字形成的最大数字。

例子:

Input : 38293367
Output : 98763332

Input : 1203465
Output: 6543210

简单方法:解决此问题的简单方法是提取给定数字的数字并将其存储在整数数组中,然后将该数组按降序排序。对数组进行排序后,打印数组的元素。
时间复杂度:O(N log N),其中N是给定数字中的位数。

高效的方法:我们知道一个数字中的数字范围是0到9,因此我们的想法是创建一个大小为10的哈希数组,并将该数字中出现的每个数字的计数存储在哈希数组中。然后遍历从索引9到0的哈希数组,并据此计算数字。

下面是上述高效方法的实现:

C++
// CPP program to print the maximum number
// from the set of digits of a given number
#include 
using namespace std;
  
// Function to print the maximum number
int printMaxNum(int num)
{
    // hashed array to store count of digits
    int count[10] = {0};
  
    // Converting given number to string
    string str = to_string(num);
  
    // Updating the count array
    for (int i=0; i 0)
        {
            result = result + (i * multiplier);
            count[i]--;
            multiplier = multiplier * 10;
        }
    }
  
    // return the result
    return result;
}
  
// Driver program to test above function
int main()
{
    int num = 38293367;
    cout << printMaxNum(num);
    return 0;
}


Java
// Java program to print the maximum number
// from the set of digits of a given number
public class GFG
{
    // Function to print the maximum number
    static int printMaxNum(int num)
    {
        // hashed array to store count of digits
        int count[] = new int[10];
          
        // Converting given number to string
        String str = Integer.toString(num);
          
        // Updating the count array
        for(int i=0; i < str.length(); i++)
            count[str.charAt(i)-'0']++;
          
        // result is to store the final number
        int result = 0, multiplier = 1;
          
        // Traversing the count array
        // to calculate the maximum number
        for (int i = 0; i <= 9; i++)
        {
            while (count[i] > 0)
            {
                result = result + (i * multiplier);
                count[i]--;
                multiplier = multiplier * 10;
            }
        }
       
        // return the result
        return result;
    }
      
    // Driver program to test above function
    public static void main(String[] args) 
    {
        int num = 38293367;
        System.out.println(printMaxNum(num));
    }
}
// This code is contributed by Sumit Ghosh


Python
# Python program to print the maximum number
# from the set of digits of a given number
  
# Function to print maximum number
def printMaximum(inum):
  
    # Hashed array to store count of digits
    count = [0 for x in range(10)]
  
    # Connverting given number to string
    string = str(num)
  
    # Updating the count array
    for i in range(len(string)):
        count[int(string[i])] = count[int(string[i])] +  1
  
    # Result stores final number
    result = 0
    multiplier = 1
  
    # traversing the count array
    # to calculate the maximum number
  
    for i in range(10):
        while count[i] > 0:
            result = result + ( i * multiplier )
            count[i] = count[i] - 1
            multiplier = multiplier * 10
  
    # return the result
    return result
  
# Driver code
num = 38293367
print printMaximum(num)
  
# This code is contributed by Harshit Agrawal


C#
// C# program to print the maximum number
// from the set of digits of a given number
using System;
  
class GFG 
{ 
  
// Function to print the maximum number
static int printMaxNum(int num)
{
    // hashed array to store 
    // count of digits
    int []count = new int[10];
      
    // Converting given number 
    // to string
    String str = num.ToString();
      
    // Updating the count array
    for(int i = 0; i < str.Length; i++)
        count[str[i] - '0']++;
      
    // result is to store the 
    // final number
    int result = 0, multiplier = 1;
      
    // Traversing the count array
    // to calculate the maximum number
    for (int i = 0; i <= 9; i++)
    {
        while (count[i] > 0)
        {
            result = result + (i * multiplier);
            count[i]--;
            multiplier = multiplier * 10;
        }
    }
  
    // return the result
    return result;
}
  
// Driver Code
public static void Main() 
{
    int num = 38293367;
    Console.Write(printMaxNum(num));
}
}
  
// This code is contributed 
// by PrinciRaj1992


PHP
 0)
        {
            $result = $result + ($i * $multiplier);
            $count[$i]--;
            $multiplier = $multiplier * 10;
        }
    }
   
    // return the result
    return $result;
}
   
// Driver program to test above function
  
    $num = 38293367;
    echo printMaxNum($num);
?>


输出:

98763332

时间复杂度: O(N),其中N是给定数字中的位数。
注意:对于非常大的数字,我们可以使用字符串来接受输入,而不是将输入存储为整数数据类型。