結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 120 ms
55,704 KB
testcase_01 AC 120 ms
55,900 KB
testcase_02 AC 121 ms
55,396 KB
testcase_03 AC 120 ms
55,752 KB
testcase_04 AC 120 ms
55,636 KB
testcase_05 AC 120 ms
55,640 KB
testcase_06 AC 121 ms
55,496 KB
testcase_07 AC 120 ms
56,088 KB
testcase_08 AC 121 ms
55,500 KB
testcase_09 AC 122 ms
55,868 KB
testcase_10 AC 120 ms
55,704 KB
testcase_11 AC 121 ms
55,656 KB
testcase_12 WA -
testcase_13 AC 120 ms
55,660 KB
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
権限があれば一括ダウンロードができます

ソースコード

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++){
            for(int j = i ; j <Disassembly.length ; j++){
                //i番目の数以上の数字がj番目にあった場合
                if(!(Disassembly[i] >= Disassembly[j])){
                    Count = j; 
                }
            }
            //条件分岐がされて変数が初期値から変動していた場合
            if(Count != 0){
                char Result = Disassembly[i];
                Disassembly[i] = Disassembly[Count];
                Disassembly[Count] = Result;
                break;
            }
        }
        return Disassembly;
    }
}
0