题解 | #添加逗号#
添加逗号
https://www.nowcoder.com/practice/f51c317e745649c0900996fd3f683aed
#include <iostream> #include <string> #include <algorithm> using namespace std; // 先声明函数原型 string formatNumberWithCommas(int n); int main() { int n; cin >> n; string result = formatNumberWithCommas(n); cout << result << endl; return 0; } // 在主函数后面定义函数 string formatNumberWithCommas(int n) { string numStr = to_string(n); int len = numStr.length(); // We will insert commas from right to left for (int i = len - 3; i > 0; i -= 3) { numStr.insert(i, ","); } return numStr; }
#C++#