题解 | #添加逗号#

添加逗号

https://www.nowcoder.com/practice/f51c317e745649c0900996fd3f683aed

int main() 
{
    int arr[10] = { 0 };
    long num = 0;
    int i = 0;
    int j = 0;
    int count = 0;
    int m = 0;
    scanf("%d", &num);
    if (num > 100) {
        while (num > 0) {
            arr[i] = num % 10;
            num /= 10;
            i++;
        }
        m = i % 3;
        i = i - 1;
        for (j = 0; j < m; j++) {
            printf("%d", arr[i]);
            i--;
        }
        if (m > 0) {
            printf(",");
        }
        for (; i >= 0; i--) {
            printf("%d", arr[i]);
            count++;
            if (count == 3 && i != 0) {
                printf(",");
                count = 0;
            }
        }
    } else {
        printf("%ld", num);
    }
    return 0;
}

全部评论

相关推荐

评论
点赞
收藏
分享
牛客网
牛客企业服务