結果

問題 No.539 インクリメント
ユーザー pekempeypekempey
提出日時 2017-06-30 22:40:14
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 53 ms / 2,000 ms
コード長 779 bytes
コンパイル時間 703 ms
コンパイル使用メモリ 73,368 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-15 06:44:41
合計ジャッジ時間 1,573 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 39 ms
6,940 KB
testcase_02 AC 52 ms
6,940 KB
testcase_03 AC 53 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <cctype>

int main() {
	int T;
	std::cin >> T;

	std::cin.ignore();

	while (T--) {
		std::string s;
		std::getline(std::cin, s);

		int i = s.size() - 1;
		while (i >= 0 && !isdigit(s[i])) i--;
		int ii = i + 1;
		if (ii == 0) {
			std::cout << s << std::endl;
			continue;
		}
		while (i >= 0 && isdigit(s[i])) i--;
		i++;

		if (std::count(s.begin() + i, s.begin() + ii, '9') == ii - i) {
			s.erase(i, ii - i);
			s.insert(i, std::string(ii - i, '0'));
			s.insert(i, std::string(1, '1'));
		} else {
			int carry = 1;
			for (int j = ii - 1; j >= i; j--) {
				s[j] += carry;
				carry = (s[j] - '0') / 10;
				s[j] = (s[j] - '0') % 10 + '0';
			}
		}

		std::cout << s << std::endl;
	}
}
0