Given a nonnegative integer n. From all its digits create the biggest and then the smallest
number. Print the sum of the obtained numbers.
For example, for n
= 56002 the biggest will be 65200 and the smallest will be 256 (the leading zeros
in the number 00256 do not count). The resulting sum is 65200 + 256 = 65456.
Input. One integer n
(0 ≤ n ≤ 108).
Output. Print the sum of the biggest and the smallest numbers,
that can be gotten from n by
permutation of its digits.
Sample input |
Sample output |
56002 |
65456 |
sort
Algorithm
analysis
Read the number into the character array. Sort the
digits in descending order and get the largest number. Sort the numbers in
ascending order and get the smallest number. Find and print their sum.
Algorithm
realization
Read the input number into the character array s.
char s[20];
Read the input number. Sort the numbers in descending
order and put the maximum number in the variable a.
gets(s);
sort(s,s+strlen(s),greater<char>());
sscanf(s,"%d",&a);
Sort the numbers in ascending order and put the
smallest number in the variable b.
sort(s,s+strlen(s),less<char>());
sscanf(s,"%d",&b);
Print the sum of the numbers.
printf("%d\n",a+b);
Algorithm
realization – string
Read the input number into the character array s.
cin >> s;
Read the input number. Sort the numbers in descending
order and put the maximum number in the variable a.
sort(s.begin(),s.end(),greater<char>());
a = stoi(s);
Sort the numbers in ascending order and put the
smallest number in the variable b.
sort(s.begin(),s.end(),less<char>());
b = stoi(s);
Print the sum of the numbers.
cout << a + b << endl;
Java realization
import
java.util.*;
public class Main
{
public static void main(String[] args)
{
Scanner con = new
Scanner(System.in);
String s[] = con.nextLine().split("");
// s =
{"1", "2", "3", "4", "5",
"6"}
Arrays.sort(s);
int a =
Integer.parseInt(String.join("", s));
Arrays.sort(s,Collections.reverseOrder());
int b =
Integer.parseInt(String.join("", s));
System.out.println(a + b);
con.close();
}
}
Python realization
n = sorted(input())
print(int(''.join(n)) + int(''.join(n)[::-1]))