結果
問題 |
No.927 Second Permutation
|
ユーザー |
![]() |
提出日時 | 2019-11-22 22:46:56 |
言語 | Java (openjdk 23) |
結果 |
WA
|
実行時間 | - |
コード長 | 909 bytes |
コンパイル時間 | 2,515 ms |
コンパイル使用メモリ | 77,888 KB |
実行使用メモリ | 54,772 KB |
最終ジャッジ日時 | 2024-10-11 04:23:47 |
合計ジャッジ時間 | 9,007 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 11 WA * 16 |
ソースコード
import java.util.Scanner; public class Main { public static void main(String[] args) throws Exception { Scanner sc = new Scanner(System.in); String x = sc.next(); sc.close(); int[] a = new int[10]; for (int i = 0; i < x.length(); i++) { a[x.charAt(i) - '0']++; } StringBuilder sb = new StringBuilder(); boolean flg1 = false; int j = 0; for (int i = 9; i >= 0; i--) { if (a[i] > 0) { sb.append(i); a[i]--; j = i - 1; if (a[i] == 0) { flg1 = true; } break; } } for (int i = j; i >= 0; i--) { if (a[i] > 0) { if (flg1) { flg1 = false; } else { sb.append(i); a[i]--; break; } } } if (sb.length() < 2 || sb.charAt(0) == '0') { System.out.println(-1); return; } for (int i = 9; i >= 0; i--) { while (a[i] > 0) { sb.append(i); a[i]--; } } System.out.println(sb.toString()); } }