1556千位分隔数

    科技2022-07-14  127

    题目描述: 给你一个整数 n,请你每隔三位添加点(即 “.” 符号)作为千位分隔符,并将结果以字符串格式返回。

    示例 1: 输入:n = 987 输出:“987”

    示例 2: 输入:n = 1234 输出:“1.234”

    示例 3: 输入:n = 123456789 输出:“123.456.789”

    示例 4: 输入:n = 0 输出:“0”

    提示: 0 <= n < 2^31

    方法1: 主要思路: (1)模拟过程;

    class Solution { public: string thousandSeparator(int n) { if(n==0){//处理特殊的情形 return "0"; } string str; int index=0;//分割3的标识 while(n){ ++index; char ch='0'+(n%10);//获得当前字符 str=ch+str;//将当前字符添加到字符串中 n/=10;//更新数字 //判断是否需要添加分割的点 if(index%3==0&&n){ str="."+str; } } return str; } };
    Processed: 0.016, SQL: 8