結果

問題 No.927 Second Permutation
ユーザー ks2mks2m
提出日時 2019-11-22 22:46:56
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 909 bytes
コンパイル時間 2,311 ms
コンパイル使用メモリ 77,700 KB
実行使用メモリ 55,212 KB
最終ジャッジ日時 2024-04-19 12:09:20
合計ジャッジ時間 8,841 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 130 ms
54,068 KB
testcase_01 AC 133 ms
54,212 KB
testcase_02 AC 133 ms
54,324 KB
testcase_03 AC 137 ms
53,756 KB
testcase_04 AC 141 ms
53,976 KB
testcase_05 AC 136 ms
54,032 KB
testcase_06 AC 129 ms
54,164 KB
testcase_07 AC 132 ms
54,016 KB
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 AC 195 ms
54,156 KB
testcase_25 AC 192 ms
54,444 KB
testcase_26 AC 177 ms
54,212 KB
testcase_27 AC 171 ms
54,012 KB
testcase_28 AC 207 ms
54,120 KB
testcase_29 AC 182 ms
54,072 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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());
	}
}
0