#include <algorithm> #include <iostream> #include <iterator> #include <string> using namespace std; int main() { long long N; cin>>N; string s; s=to_string(N); reverse(s.begin(), s.end()); string res; for (int i=0; i<s.length(); i++) { if (i>0&&i%3==0) {//这个条件好坑 res+=","; } res+=s[i]; } reverse(res.begin(), res.end()); cout<<res<<endl; return 0; }
这题也可以不要res,而且这个条件很严格,还要转来转去的:
#include <iostream>#include <string>using namespace std;
int main() {string s;cin >> s;
#include <iostream> #include <string> using namespace std; int main() { string s; cin >> s; int len = s.length(); // 从倒数第3位开始,每3位插入一个逗号 for (int i = len - 3; i > 0; i -= 3) { s.insert(i, ","); } cout << s; return 0; }
这样最快,需要数学思考能力……