結果

問題 No.539 インクリメント
ユーザー kenkooookenkoooo
提出日時 2017-06-30 23:01:11
言語 Java21
(openjdk 21)
結果
AC  
実行時間 482 ms / 2,000 ms
コード長 4,761 bytes
コンパイル時間 1,959 ms
コンパイル使用メモリ 79,220 KB
実行使用メモリ 60,792 KB
最終ジャッジ日時 2024-10-04 21:03:20
合計ジャッジ時間 4,468 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 119 ms
54,288 KB
testcase_01 AC 431 ms
58,872 KB
testcase_02 AC 397 ms
58,424 KB
testcase_03 AC 482 ms
60,792 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.NoSuchElementException;
import java.util.Scanner;

public class Main {

  private void solve(Scanner in, PrintWriter out) {
    int T = Integer.parseInt(in.nextLine());
    for (int t = 0; t < T; t++) {
      char[] S = in.nextLine().toCharArray();
      int N = S.length;
      int end = N;
      for (int i = N - 1; i >= 0; i--) {
        if (S[i] >= '0' && S[i] <= '9') {
          end = i;
          break;
        }
      }
      if (end == N) {
        out.println(S);
        continue;
      }

      int begin = end;
      for (int i = end; i >= 0; i--) {
        if (S[i] < '0' || S[i] > '9') {
          break;
        }
        begin = i;
      }

      int over = 1;
      for (int i = end; i >= begin; i--) {
        if (S[i] == '9') {
          S[i] = '0';
        } else {
          S[i] += 1;
          over = 0;
          break;
        }
      }

      if (over > 0) {
        for (int i = 0; i < begin; i++) {
          out.print(S[i]);
        }
        out.print(1);
        for (int i = begin; i < N; i++) {
          out.print(S[i]);
        }
        out.println();
      } else {
        out.println(S);
      }


    }
  }

  public static void main(String[] args) {
    PrintWriter out = new PrintWriter(System.out);
    new Main().solve(new Scanner(System.in), out);
    out.close();
  }

  private static class FastScanner {

    private final InputStream in = System.in;
    private final byte[] buffer = new byte[1024];
    private int ptr = 0;
    private int bufferLength = 0;

    private boolean hasNextByte() {
      if (ptr < bufferLength) {
        return true;
      } else {
        ptr = 0;
        try {
          bufferLength = in.read(buffer);
        } catch (IOException e) {
          e.printStackTrace();
        }
        if (bufferLength <= 0) {
          return false;
        }
      }
      return true;
    }

    private int readByte() {
      if (hasNextByte()) {
        return buffer[ptr++];
      } else {
        return -1;
      }
    }

    private static boolean isPrintableChar(int c) {
      return 33 <= c && c <= 126;
    }

    private void skipUnprintable() {
      while (hasNextByte() && !isPrintableChar(buffer[ptr])) {
        ptr++;
      }
    }

    boolean hasNext() {
      skipUnprintable();
      return hasNextByte();
    }

    public String next() {
      if (!hasNext()) {
        throw new NoSuchElementException();
      }
      StringBuilder sb = new StringBuilder();
      int b = readByte();
      while (isPrintableChar(b)) {
        sb.appendCodePoint(b);
        b = readByte();
      }
      return sb.toString();
    }

    long nextLong() {
      if (!hasNext()) {
        throw new NoSuchElementException();
      }
      long n = 0;
      boolean minus = false;
      int b = readByte();
      if (b == '-') {
        minus = true;
        b = readByte();
      }
      if (b < '0' || '9' < b) {
        throw new NumberFormatException();
      }
      while (true) {
        if ('0' <= b && b <= '9') {
          n *= 10;
          n += b - '0';
        } else if (b == -1 || !isPrintableChar(b)) {
          return minus ? -n : n;
        } else {
          throw new NumberFormatException();
        }
        b = readByte();
      }
    }

    double nextDouble() {
      return Double.parseDouble(next());
    }

    double[] nextDoubleArray(int n) {
      double[] array = new double[n];
      for (int i = 0; i < n; i++) {
        array[i] = nextDouble();
      }
      return array;
    }

    double[][] nextDoubleMap(int n, int m) {
      double[][] map = new double[n][];
      for (int i = 0; i < n; i++) {
        map[i] = nextDoubleArray(m);
      }
      return map;
    }

    public int nextInt() {
      return (int) nextLong();
    }

    public int[] nextIntArray(int n) {
      int[] array = new int[n];
      for (int i = 0; i < n; i++) {
        array[i] = nextInt();
      }
      return array;
    }

    public long[] nextLongArray(int n) {
      long[] array = new long[n];
      for (int i = 0; i < n; i++) {
        array[i] = nextLong();
      }
      return array;
    }

    public String[] nextStringArray(int n) {
      String[] array = new String[n];
      for (int i = 0; i < n; i++) {
        array[i] = next();
      }
      return array;
    }

    public char[][] nextCharMap(int n) {
      char[][] array = new char[n][];
      for (int i = 0; i < n; i++) {
        array[i] = next().toCharArray();
      }
      return array;
    }

    public int[][] nextIntMap(int n, int m) {
      int[][] map = new int[n][];
      for (int i = 0; i < n; i++) {
        map[i] = nextIntArray(m);
      }
      return map;
    }
  }
}
0