题目链接
#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;
}
}
import java.util.Scanner;
public class Main {
public static String replaceNumber(String str) {
int count = 0;
int sOldSize = str.length();
for (int i = 0; i < str.length(); i++) {
if (Character.isDigit(str.charAt(i))) { // 如果是数字字符
count++;
}
}
char[] newCharArray = new char[str.length() + count * 5];
int sNewSize = newCharArray.length;
System.arraycopy(str.toCharArray(), 0, newCharArray, 0, sOldSize);
for (int i = sNewSize - 1, j = sOldSize - 1; j < i; j--, i--) {
if (!Character.isDigit(newCharArray[j])) {
newCharArray[i] = newCharArray[j];
} else {
newCharArray[i--] = 'r';
newCharArray[i--] = 'e';
newCharArray[i--] = 'b';
newCharArray[i--] = 'm';
newCharArray[i--] = 'u';
newCharArray[i] = 'n'; // 在 for 循环条件内 i 还会再减去一次
}
}
return new String(newCharArray);
};
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String str = scanner.next();
System.out.println(replaceNumber(str));
scanner.close();
}
}
class Solution:
def change(self, s):
# 将输入的字符串转换为列表,以便进行字符级别的操作
lst = list(s)
# 开始遍历
for i in range(len(lst)):
# 如果当前字符是数字,则替换为字符串 "number"
if lst[i].isdigit():
lst[i] = "number"
# 将列表中的字符连接成一个字符串并返回
return ''.join(lst)
# 实例化 Solution 类
sol = Solution()
# 获取输入字符串
s = input()
# 调用 change 函数
result = sol.change(s)
# 输出结果
print(result)
package main
import (
"bufio"
"fmt"
"os"
"unicode"
)
func main() {
reader := bufio.NewReader(os.Stdin)
str, _ := reader.ReadString('\n') //以换行符读入,防止用空格分割读入
var res string
for _, char := range str {
if unicode.IsDigit(char) {
res += "number"
} else {
res += string(char)
}
}
fmt.Println(res)
}