結果

問題 No.539 インクリメント
ユーザー Tsukasa_TypeTsukasa_Type
提出日時 2018-02-26 23:28:00
言語 Java21
(openjdk 21)
結果
AC  
実行時間 489 ms / 2,000 ms
コード長 1,373 bytes
コンパイル時間 2,144 ms
コンパイル使用メモリ 77,224 KB
実行使用メモリ 58,472 KB
最終ジャッジ日時 2024-05-03 20:54:59
合計ジャッジ時間 4,661 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 137 ms
54,092 KB
testcase_01 AC 489 ms
58,472 KB
testcase_02 AC 450 ms
58,428 KB
testcase_03 AC 461 ms
58,332 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;
import static java.lang.System.*;

public class Main {
	static Scanner sc = new Scanner(System.in);
	public static void main(String[] args) {
		int n = sc.nextInt();
		sc.nextLine();
		boolean increment = false;;
		boolean isFinished;
		boolean isFirst;
		boolean isSearch;
		StringBuilder ans = new StringBuilder();

		for (int i=0; i<n; i++) {
			char[] ar = sc.nextLine().toCharArray();
			isFinished = false;
			isFirst = true;
			isSearch = false;
			for (int j=ar.length-1; j>=0; j--) {
				if ('0'<=ar[j] && ar[j]<='9') {
					if (isFinished==false) {
						if (isFirst==true) {
							if (ar[j]=='9') {
								increment = true;
								ar[j] = '0';
								isSearch = true;
							}
							else {
								ar[j]++;
								increment = false;
								isFinished = true;
							}
							isFirst = false;
						}
						else if (isFirst==false) {
							if (ar[j]=='9') {
								ar[j] = '0';
							}
							else if (ar[j]<'9') {
								ar[j]++;
								increment = false;
								isFinished = true;
							}
						}
						if (j==0 && increment==true) {ans.append("01"); break;}
					}
				}
				else {
					if (isSearch==true) {
						isFinished = true;
						isSearch = false;
						if (increment==true) {
							ans.append(1);
						}
					}
				}
				ans.append(ar[j]);
			}
			ans.reverse();
			out.println(ans);
			ans.setLength(0);
		}
	}
}
0