给定一个数字,编写一个程序,用这个数字的所有数字找出一个最大数字。 例如:
null
Input : 38293367Output : 98763332Input : 1203465Output: 6543210
简单方法 :解决这个问题的简单方法是提取给定数字的数字并将其存储在整数数组中,然后按降序排序。对数组排序后,打印数组的元素。 时间复杂性 :O(N logn),其中N是给定数字中的位数。 有效方法: 我们知道数字中的数字范围是0-9,所以我们的想法是创建一个大小为10的散列数组,并存储该数字中出现的散列数组中每个数字的计数。然后从索引9到0遍历散列数组,并相应地计算数字。 以下是上述有效方法的实施情况:
C++
// CPP program to print the maximum number // from the set of digits of a given number #include <bits/stdc++.h> 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<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 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 |
Python3
# 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 )] # Converting 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
<?php // Php program to print the maximum number // from the set of digits of a given number // Function to print the maximum number function printMaxNum( $num ) { // hashed array to store count of digits $count = array_fill (0,10, NULL); // Converting given number to string $str = (string) $num ; // Updating the count array for ( $i =0; $i < strlen ( $str ); $i ++) $count [ord( $str [ $i ])-ord( '0' )]++; // result is to store the final number $result = 0; $multiplier = 1; // Traversing the count array // to calculate the maximum number for ( $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 $num = 38293367; echo printMaxNum( $num ); ?> |
Javascript
<script> // Javascript program to print the maximum number // from the set of digits of a given number // Function to print the maximum number function printMaxNum(num) { // hashed array to store count of digits let count = new Array(10); for (let i=0;i<count.length;i++) { count[i]=0; } // Converting given number to string let str = num.toString(); // Updating the count array for (let i=0; i < str.length; i++) count[str[i]- '0' ]++; // result is to store the final number let result = 0, multiplier = 1; // Traversing the count array // to calculate the maximum number for (let 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 let num = 38293367; document.write(printMaxNum(num)); //This code is contributed by avanitrachhadiya2155 </script> |
输出:
98763332
时间复杂性: O(N),其中N是给定数字中的位数。 笔记 :对于非常大的数字,我们可以使用字符串来获取输入,而不是以整数数据类型存储输入。
本文由 罗希特·塔普里亚尔 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 写极客。组织 或者把你的文章寄去评论-team@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。 如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END