No.927 Second Permutation
タグ : / 解いたユーザー数 332
作問者 :
leaf_1415
/ テスター :
tempura_pp
Problem Statement
You are given a positive integer $X$.
Rearrange the digits that make up $X$ in any way you like to form a positive integer $Y$.
However, the most significant digit of $Y$ must not be '0'.
Output the $2$nd largest number among the numbers that can be formed as $Y$.
If no such number exists, output $-1$.
Input
$X$
The input satisfies the following constraints.
$10 \leq X < 10^{100000}$
$X$ is an integer
Output
Output the 2nd largest number among the numbers that can be formed as $Y$. If no such number exists, output −1. Print a newline at the end.
Sample
Sample 1
Input
213
Output
312
The values of $Y$ that can be formed from $X=213$ are,
in descending order, the six numbers $321, 312, 231, 213, 132, 123$,
so output $312$, the $2$nd largest among them.
Sample 2
Input
999
Output
-1
No matter how the digits that make up $X$ are rearranged, the resulting number is $999$.
The only number that can be formed as $Y$ is $999$, so "the $2$nd largest number among the numbers that can be formed as $Y$" does not exist.
Therefore, output $-1$.
Sample 3
Input
50
Output
-1
The only number that can be formed as $Y$ is $50$,
so "the $2$nd largest number among the numbers that can be formed as $Y$" does not exist.
Therefore, output $-1$.
提出するには、Twitter 、GitHub、 Googleもしくは右上の雲マークをクリックしてアカウントを作成してください。