题目来源
题目描述
![在这里插入图片描述](https://img-blog.csdnimg.cn/720d8d881d8b46d7b7807b61258df58e.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAT2NlYW5TdGFy55qE5a2m5Lmg56yU6K6w,size_11,color_FFFFFF,t_70,g_se,x_16)
![在这里插入图片描述](https://img-blog.csdnimg.cn/03202de6c2a44166978981d19ec733ef.png)
题目解析
递归
![在这里插入图片描述](https://img-blog.csdnimg.cn/3f057dfd67134808b73df8c943312d9d.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAT2NlYW5TdGFy55qE5a2m5Lmg56yU6K6w,size_18,color_FFFFFF,t_70,g_se,x_16)
class Solution {
std::string digui(std::string str){
if(str.size() <= 3){
return str;
}
return digui(str.substr(0, str.size() - 3)) + '.' + str.substr(str.size() - 3);
}
public:
string thousandSeparator(int n) {
std::string str = std::to_string(n);
return digui(str);
}
};
![在这里插入图片描述](https://img-blog.csdnimg.cn/c2086b3fe9dd4466a52f66372ffe6bb8.png)
STL
class Solution {
public:
string thousandSeparator(int n) {
std::string str = std::to_string(n);
for (int i = str.size() - 3; i >= 0; i -= 3) {
str.insert(i, 1, '.');
}
if(str.front() == '.'){
str.erase(0, 1);
}
return str;
}
};
![在这里插入图片描述](https://img-blog.csdnimg.cn/dce2a53f55a747efac4d43200e92168b.png)