Eagle233-Blog

[Algorithms] KamaCoder 54. 替换数字(第八期模拟笔试)


Categories Algorithms String
Tags

137 Words   |   1 Minutes

来源:代码随想录

KamaCoder 54. 替换数字(第八期模拟笔试)

双指针

每次遇到数字都把第二个指针指向五个元素后,将字符串复制到后面。

#include <bits/stdc++.h>

using namespace std;

int main(void) {
    string s;
    cin >> s;

    for (int i = 0; i < s.size(); i++) {
        if (isdigit(s[i])) {
            s.resize(s.size() + 5);
            for (int j = s.size() - 1; j > i; j--) {
                s[j] = s[j - 5];
            }
            s[i] = 'n';
            s[i + 1] = 'u';
            s[i + 2] = 'm';
            s[i + 3] = 'b';
            s[i + 4] = 'e';
            s[i + 5] = 'r';
        }
    }

    cout << s << endl;
}


Page views: Loading...  ·  Visitors: Loading...
Except where otherwise noted, original content on this site is dedicated to the public domain under CC0 1.0.
Powered by Hexo & Theme mdsuper
沪ICP备2026040813号
Search