結果

問題 No.539 インクリメント
ユーザー nanaenanae
提出日時 2017-06-30 23:58:57
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 11 ms / 2,000 ms
コード長 1,618 bytes
コンパイル時間 616 ms
コンパイル使用メモリ 93,660 KB
実行使用メモリ 6,668 KB
最終ジャッジ日時 2023-09-03 14:54:55
合計ジャッジ時間 1,979 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

// tested by Hightail - https://github.com/dj3500/hightail
import std.stdio, std.string, std.conv, std.algorithm;
import std.range, std.array, std.math, std.typecons, std.container, core.bitop;

int T;
string s;

void main() {
    scan(T);

    while (T--) {
        s = readln.chomp;
        writeln(solve(s));
    }
}

string solve(string s) {
    int n = s.length.to!int;

    int r = -1;

    foreach_reverse (i ; 0 .. n) {
        if ('0' <= s[i] && s[i] <= '9') {
            r = i + 1;
            break;
        }
    }

    if (r == -1) return s;

    int l = 0;

    foreach_reverse (i ; 0 .. r) {
        if (s[i] < '0' || s[i] > '9') {
            l = i + 1;
            break;
        }
    }

    return s[0 .. l] ~ increment(s[l .. r]) ~ s[r .. $];
}

string increment(string t) {
    int n = t.length.to!int;
    char[] res = ("0" ~ t).to!(char[]);

    bool flag = 1;
    int r = n;

    while (flag) {
        if (r == 0) {
            break;
        }

        if (res[r] == '9') {
            res[r] = '0';
        }
        else {
            res[r] += 1;
            flag = 0;
        }

        r--;
    }

    if (flag) res[0] = '1';
    else res = res[1 .. $];

    return res.to!string;
}


void scan(T...)(ref T args) {
    string[] line = readln.split;
    foreach (ref arg; args) {
        arg = line.front.to!(typeof(arg));
        line.popFront();
    }
    assert(line.empty);
}


void fillAll(R, T)(ref R arr, T value) {
    static if (is(typeof(arr[] = value))) {
        arr[] = value;
    }
    else {
        foreach (ref e; arr) {
            fillAll(e, value);
        }
    }
}
0