結果

問題 No.39 桁の数字を入れ替え
ユーザー SagTokiSagToki
提出日時 2018-06-01 13:46:52
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 2,469 bytes
コンパイル時間 3,490 ms
コンパイル使用メモリ 75,304 KB
実行使用メモリ 55,880 KB
最終ジャッジ日時 2023-09-12 21:18:41
合計ジャッジ時間 6,760 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 122 ms
55,716 KB
testcase_01 AC 120 ms
55,400 KB
testcase_02 AC 121 ms
55,640 KB
testcase_03 AC 121 ms
55,456 KB
testcase_04 AC 122 ms
55,708 KB
testcase_05 WA -
testcase_06 WA -
testcase_07 AC 121 ms
55,792 KB
testcase_08 AC 121 ms
55,648 KB
testcase_09 AC 122 ms
55,712 KB
testcase_10 AC 121 ms
55,640 KB
testcase_11 AC 121 ms
55,576 KB
testcase_12 AC 122 ms
55,876 KB
testcase_13 AC 122 ms
55,524 KB
testcase_14 AC 121 ms
55,836 KB
testcase_15 AC 120 ms
55,600 KB
testcase_16 AC 120 ms
55,544 KB
testcase_17 AC 121 ms
55,592 KB
testcase_18 AC 122 ms
55,584 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Scanner;
import java.util.regex.Pattern;
import java.util.regex.Matcher;

public class ReplacingNumbers {
    public static void main(String[] args){
        char[] Disassembly = Processing(Input());
        String Result = String.valueOf(Disassembly);
        System.out.println(Result);
    }
    
    //数値を入力し条件を判定して1文字ずつ配列に格納して返却するメソッド
    public static char[] Input(){
        Scanner scanner = new Scanner(System.in);
        char[] Disassembly = null;
        try{
            //数値の入力
            String N = scanner.nextLine();
            //数列が1~9で構成されているかチェック
            Pattern pattern = Pattern.compile(".*[^1-9].*");
            Matcher matcher = pattern.matcher(N);
            if(matcher.find()){
                System.out.println("1~9の数字のみで入力してください");
                System.exit(0);
            }
            //数列の長さが2以上9以下かチェック
            if(N.length() < 2 || N.length() > 9){
                System.out.println("2文字以上9文字以下で入力してください");
                System.exit(0);
            }
            //String型の数列をchar配列で1文字ずつ格納する
            Disassembly = N.toCharArray();
            
        }catch(Exception E){
            System.out.println("想定外のエラーです");
            System.exit(0);
        }
        return Disassembly;
    }
    
    public static char[] Processing(char[] Disassembly){
        //何番目に最も大きい数があるかを記憶する変数を定義
        int Count = 0;
        //左から順番に固定してその値より大きな値が右側にあるか調べるループ
        for(int i = 0 ; i < Disassembly.length ; i++){
            Count = i;
            for(int j = i + 1 ; j <Disassembly.length ; j++){
                //i番目の数以上の数字がj番目にあった場合
                if(Disassembly[Count] < Disassembly[j]){
                    Count = j; 
                }
            }
            //条件分岐がされて変数が初期値から変動していた場合
            if(Count != i){
                char Result = Disassembly[i];
                Disassembly[i] = Disassembly[Count];
                Disassembly[Count] = Result;
                break;
            }
        }
        return Disassembly;
    }
}
0