【leetcode】千位分隔数

 

char * thousandSeparator(int n){
    char* s = (char*)calloc(20,1);
    sprintf(s,"%d",n);
    int len = strlen(s);
    if (len <= 3) return s;
    char* str = (char*)calloc(20,1);
    int i = len - 1;
    int count = 0;
    int j = i + len/3;
    while(i>=0)
    {
        count++;
        if (count % 4 == 0)
        {
            str[j--] = '.';
            continue;
        }
        str[j--] = s[i--];    
    }
    return &str[j+1];
}

 

posted @ 2020-08-31 20:29  温暖了寂寞  阅读(161)  评论(0编辑  收藏  举报