結果

問題 No.39 桁の数字を入れ替え
ユーザー Tsukasa_TypeTsukasa_Type
提出日時 2018-02-25 00:43:44
言語 Java21
(openjdk 21)
結果
AC  
実行時間 117 ms / 5,000 ms
コード長 890 bytes
コンパイル時間 2,328 ms
コンパイル使用メモリ 78,492 KB
実行使用メモリ 54,292 KB
最終ジャッジ日時 2024-04-10 05:49:49
合計ジャッジ時間 5,232 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 112 ms
54,024 KB
testcase_01 AC 116 ms
54,120 KB
testcase_02 AC 103 ms
52,940 KB
testcase_03 AC 116 ms
54,144 KB
testcase_04 AC 105 ms
53,252 KB
testcase_05 AC 114 ms
54,120 KB
testcase_06 AC 116 ms
53,948 KB
testcase_07 AC 117 ms
53,968 KB
testcase_08 AC 116 ms
53,908 KB
testcase_09 AC 105 ms
52,624 KB
testcase_10 AC 114 ms
54,192 KB
testcase_11 AC 114 ms
54,076 KB
testcase_12 AC 97 ms
52,712 KB
testcase_13 AC 115 ms
54,172 KB
testcase_14 AC 116 ms
54,292 KB
testcase_15 AC 113 ms
53,884 KB
testcase_16 AC 112 ms
54,116 KB
testcase_17 AC 102 ms
52,948 KB
testcase_18 AC 101 ms
52,980 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
	static Scanner sc = new Scanner(System.in);
	public static void main(String[] args) {
		String s = sc.next();

		int[] ar = new int[s.length()];
		for (int i=0; i<s.length(); i++) {
			ar[i] = Integer.parseInt(s.substring(i,i+1));
		}

		int base = 0;
		int search = 0;
		for (int i=0; i<s.length()-1; i++) {
			base = Integer.parseInt(s.substring(i,i+1));
			int max = -1;
			int l = 0;
			int r = 0;
			for (int j=s.length()-1; j>=i+1; j--) {
				search = Integer.parseInt(s.substring(j,j+1));
				if (search > max) {
					max = Math.max(max,search);
					l = i;
					r = j;
				}
			}
			if (max > base) {swap(ar,l,r); break;}
		}

		for (int i=0; i<s.length(); i++) {
			System.out.print(ar[i]);
		}
		System.out.println();
	}

	static int[] swap (int[] ar, int a, int b) {
		int temp = ar[a];
		ar[a] = ar[b];
		ar[b] = temp;
		return ar;
	}
}
0