题解 | #表示数字#__huawei_no.96-1
表示数字
https://www.nowcoder.com/practice/637062df51674de8ba464e792d1a0ac6
#include <cctype>
#include<iostream>
#include<string>
#include<regex>
using namespace std;
int main() {
string s;
while (cin >> s) {
for(int i = 0; i < s.length(); i++){
if(isdigit(s[i])){
s.insert(i,"*");
i++;
while(isdigit(s[i])){
i++;
}
s.insert(i, "*");
}
}
cout<<s<<endl;
}
return 0;
}
// 64 位输出请用 printf("%lld")
这个题目补上了一个我怀疑的点,那就是在 C++ 中,string 类型是动态的,可以随着元素的插入和删除而改变长度。因此,在 for 循环中不断修改字符串 s 的内容时,s.length() 的值确实会随之更新。
所以,即使是 在中途进行增删插件,s.length()是动态变化的,不用担心会超出界。
深信服公司福利 897人发布