题解 | #表示数字#
表示数字
https://www.nowcoder.com/practice/637062df51674de8ba464e792d1a0ac6
一次遍历,用索引标记 “数字子串” 的开始位置和结束位置,并配合使用substr()方法即可。
int main() {
string s;
unsigned int n;
string res;
while(getline(cin,s)){
n = s.size();
for(int i=0; i<n;){
if(isdigit(s[i])){ //每次遇到的第一个数字,并记录当前 数字子串的长度
res += '*';
int start = i;
while(i<n && isdigit(s[i])) ++i; //让 i 后移,指向第一个 非数字的位置
res += s.substr(start,i-start) + '*';
}
else res += s[i++];
}
cout<<res<<endl;
}
return 0;
}
汤臣倍健公司氛围 388人发布

查看5道真题和解析