結果

問題 No.539 インクリメント
ユーザー takeya_okinotakeya_okino
提出日時 2019-10-02 19:54:39
言語 Java21
(openjdk 21)
結果
AC  
実行時間 489 ms / 2,000 ms
コード長 1,689 bytes
コンパイル時間 2,270 ms
コンパイル使用メモリ 80,068 KB
実行使用メモリ 59,716 KB
最終ジャッジ日時 2024-04-14 08:59:26
合計ジャッジ時間 4,795 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 145 ms
54,500 KB
testcase_01 AC 436 ms
58,636 KB
testcase_02 AC 395 ms
58,236 KB
testcase_03 AC 489 ms
59,716 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int T = sc.nextInt();
    String line = sc.nextLine();
    String s1 = "";
    String s2 = "";
    for(int i = 0; i < T; i++) {
      String s = sc.nextLine();
      char[] ss = s.toCharArray();
      int p = 0;
      int increment = 1;
      int k = 0;
      int ind = 0;
      for(int j = s.length() - 1; j >= 0; j--) {
        String str = String.valueOf(s.charAt(j));
        int t = -1;
        try {
          t = Integer.parseInt(str);
        } catch(NumberFormatException e) {
        }
        if(t == -1) {
          if(p == 0) {
            
          } else {
            if(increment == 1) {
              k = 1;
              ind = j + 1;
              increment = 0;
              break;
            }
          } 
        } else {
          p = 1;
          if(increment == 1) {
            if(t == 9) {
              ss[j] = "0".charAt(0);
            } else {
              ss[j] = (String.valueOf(t + 1)).charAt(0);
              increment = 0;
              break;
            }
          }
        }
      }
      if(k == 0) {
        String sa = new String(ss);
        if((p == 1) && (increment == 1)) {
          System.out.println("1" + sa);
        } else {
          System.out.println(new String(ss));
        }
      } else {
        String sa = new String(ss);
        if((p == 1) && (increment == 1)) {
          System.out.println("1" + sa);
        } else {
          s1 = sa.substring(0, ind);
          s2 = sa.substring(ind, s.length());
          System.out.println(s1 + "1" + s2);          
        }
      }
    }
  }
}
0