結果

問題 No.539 インクリメント
ユーザー TautConyTautCony
提出日時 2017-11-20 12:57:54
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 10 ms / 2,000 ms
コード長 1,832 bytes
コンパイル時間 554 ms
コンパイル使用メモリ 67,816 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-17 08:18:55
合計ジャッジ時間 1,478 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 3 ms
4,380 KB
testcase_02 AC 8 ms
4,380 KB
testcase_03 AC 10 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <string>
#include <cstdlib>

using namespace std;

class Number
{
public:
    string number;
    Number(){}
    Number(string num)
    {
        number = num;
    }
    Number& operator++()
    {
        size_t length = number.size();
        for (int i = length - 1; i >= 0; --i)
        {
            number[i] = ((number[i] - '0') + 1) % 10 + '0';
            if (number[i] != '0') break;
            else if (i == 0) {
                number = "1" + number;
            }
        }
        return *this;
    }
};

int main()
{
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    int T;
    cin >> T;
    string line;
    getline(cin, line);
    while(T--)
    {
        getline(cin, line);
        string head = "";
        string num = "";
        string tail = "";
        bool begin = false;
        int lastIndexOfBegin = 0;
        int firstIndexOfTail = line.length() - 1;
        

        for(int i = line.length() - 1; i >= 0; --i)
        {
            if (line[i] >= '0' && line[i] <= '9')
            {
                if (!begin) firstIndexOfTail = i + 1;
                begin = true;
            }
            else
            {
                if (!begin) continue;
                lastIndexOfBegin = i + 1;
                break;
            }
        }
        if (lastIndexOfBegin == 0 && firstIndexOfTail == line.length() - 1)
        {
            cout << line << endl;
            continue;
        }
        //cout << lastIndexOfBegin << " " << firstIndexOfTail << endl;
        Number number(line.substr(lastIndexOfBegin, firstIndexOfTail - lastIndexOfBegin));
        ++number;
        cout << line.substr(0, lastIndexOfBegin);
        cout << number.number;
        cout << line.substr(firstIndexOfTail, line.length() - firstIndexOfTail) << endl;
    }
}

0