結果

問題 No.39 桁の数字を入れ替え
ユーザー holegumaholeguma
提出日時 2015-08-11 01:22:25
言語 Java21
(openjdk 21)
結果
AC  
実行時間 52 ms / 5,000 ms
コード長 870 bytes
コンパイル時間 2,002 ms
コンパイル使用メモリ 77,600 KB
実行使用メモリ 50,500 KB
最終ジャッジ日時 2024-04-10 05:07:38
合計ジャッジ時間 3,695 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 49 ms
50,352 KB
testcase_01 AC 50 ms
50,456 KB
testcase_02 AC 50 ms
50,492 KB
testcase_03 AC 49 ms
50,500 KB
testcase_04 AC 49 ms
50,452 KB
testcase_05 AC 49 ms
50,276 KB
testcase_06 AC 49 ms
50,388 KB
testcase_07 AC 50 ms
50,176 KB
testcase_08 AC 49 ms
50,324 KB
testcase_09 AC 49 ms
50,332 KB
testcase_10 AC 52 ms
50,160 KB
testcase_11 AC 50 ms
50,112 KB
testcase_12 AC 50 ms
50,460 KB
testcase_13 AC 50 ms
50,340 KB
testcase_14 AC 50 ms
50,396 KB
testcase_15 AC 50 ms
50,448 KB
testcase_16 AC 50 ms
50,428 KB
testcase_17 AC 49 ms
50,304 KB
testcase_18 AC 50 ms
50,416 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;

class Main{

static final PrintWriter out=new PrintWriter(System.out);

public static void main(String[] args) throws IOException{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String line="";
while((line=br.readLine())!=null&&!line.isEmpty()){
int n=Integer.parseInt(line);
char[] num=line.toCharArray();
boolean[] used=new boolean[10];
for(int i=0;i<num.length;i++){
used[(int)(num[i]-'0')]=true;
}
loop:
for(int i=9;i>=1;i--){
if(!used[i]) continue;
int l=rightNum(i,num);
for(int j=0;j<l;j++){
if((int)(num[j]-'0')>=i) continue;
swap(j,l,num);
break loop;
}
}
out.println(num);
out.flush();
}
}

private static void swap(int a,int b,char[] num){
char c=num[a];
num[a]=num[b];
num[b]=c;
}

private static int rightNum(int a,char[] num){
for(int i=num.length-1;i>=0;i--){
if((int)(num[i]-'0')==a) return i;
}
return -1;
}
}
0