Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature 0054.替换数字.md 替换数字 C++ 题解提交 #143

Open
DIDA-lJ opened this issue Nov 23, 2023 · 0 comments
Open

feature 0054.替换数字.md 替换数字 C++ 题解提交 #143

DIDA-lJ opened this issue Nov 23, 2023 · 0 comments

Comments

@DIDA-lJ
Copy link
Contributor

DIDA-lJ commented Nov 23, 2023

#include<iostream>
#include<cmath>
#include<algorithm>

using namespace std;
int main() {
    string s;
    while (cin >> s) {
        int count = 0; // 统计数字的个数
        int sOldSize = s.size();
        for (int i = 0; i < s.size(); i++) {
            if (s[i] >= '0' && s[i] <= '9') {
                count++;
            }
        }
        // 扩充字符串s的大小,也就是每个空格替换成"number"之后的大小
        s.resize(s.size() + count * 5);
        int sNewSize = s.size();
        // 从后先前将空格替换为"number"
        for (int i = sNewSize - 1, j = sOldSize - 1; j < i; i--, j--) {
            if (s[j] > '9' || s[j] < '0') {
                s[i] = s[j];
            } else {
                s[i] = 'r';
                s[i - 1] = 'e';
                s[i - 2] = 'b';
                s[i - 3] = 'm';
                s[i - 4] = 'u';
                s[i - 5] = 'n';
                i -= 5;
            }
        }
        cout << s << endl;
    }
}
@DIDA-lJ DIDA-lJ changed the title Update 0054.替换数字.md 替换数字 C++ 题解提交 feature 0054.替换数字.md 替换数字 C++ 题解提交 Nov 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant