Two integers are given. Print the maximum number that can be obtained
from their digits.
For example, from digits of
numbers 345 and 6090737 we can get the maximum
number 9776543300.
Input. Two numbers a and b (1 ≤ a, b ≤ 109)
are given in one line.
Output. Print the biggest number that can be obtained from digits of numbers a and b.
Sample input |
Sample output |
345 6090737 |
9776543300 |
sort
Read both numbers into a character array, thus gluing them together. Sort
the numbers in decreasing order and get the largest number.
Numbers can also be read in two strings and then concatenated. Then sort
the numbers in decreasing order.
Algorithm
realization
Read the input number into the character array s.
char s[20];
Read the input numbers into a character array, placing them one after
another.
scanf("%s ",s);
scanf("%s",s+strlen(s));
Sort the digits in descending order.
sort(s,s+strlen(s),greater<char>());
Extract a 64-bit integer from the string into the variable a and print it.
sscanf(s,"%lld",&a);
printf("%lld\n",a);
Algorithm
realization –
iostream + string
#include <iostream>
#include <string>
#include <set>
#include <algorithm>
using namespace
std;
string s, p, q;
int main(void)
{
cin >> s
>> p;
q = s + p;
sort(q.begin(),q.end(),greater<char>());
cout << q
<< endl;
return 0;
}
Java realization
import
java.util.*;
public class Main
{
public static void main(String[] args)
{
Scanner con = new Scanner(System.in);
String s[] = (con.next() + con.next()).split("");
Arrays.sort(s,Collections.reverseOrder());
long res = Long.parseLong(String.join("", s));
System.out.println(res);
con.close();
}
}
Python realization
a, b = input().split()
c = list(a + b)
c.sort(reverse = True)
print(''.join(c))