题目链接:表示数字_牛客题霸_牛客网
注意点:
1.考虑用栈来处理,如果是数字就弹出,如果连续的数字需要判断
2.方法二参考了其他人的方法,正则表达式
#include <iostream>
using namespace std;
int main(){
string str;
string res;
while(getline(cin, str)){
int i = 0;
while(str[i] != '\0'){
if( str[i] >= '0' && str[i]<= '9'){
res.push_back('*');
//如果是多个数字,再次循环
while(str[i]>='0' && str[i]<='9'){
res.push_back(str[i]);
i++;
}
res.push_back('*');
}
else {
res.push_back(str[i]);
i++;
}
}
cout << res <<endl;
}
return 0;
}
方法二:
#include <iostream>
#include <regex>
using namespace std;
int main(){
string str;
while (getline(cin, str)){
//在str中匹配多个数字的字符设为s
regex s("(\\d+)");
//将字符串str中与s匹配的前后加*
string res = regex_replace(str,s,"\*$1\*");
cout << res << endl;
}
return 0;
}