题解 | 添加逗号
添加逗号
https://www.nowcoder.com/practice/f51c317e745649c0900996fd3f683aed
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <string>
using namespace std;
int main() {
int count =0;
string str, res="";
cin>>str;
for(int i=str.length()-1;i>=0;i--){
res=str[i]+res;
count++;
if(count==3&&i>0){
res=","+res;
count =0;
}
}
cout <<res;
}
// 64 位输出请用 printf("%lld")
要点一:可以用str类型直接接收数字,这样就少去了long int类型转str的操作
要点二:可以用字符串的“+”法进行字符串的前后拼接,减少最后一步的字符串的逆序输出
要点三:在判断时,需要增加"i>0"的判断,避免数字的首位添加“,”

查看4道真题和解析