首页 > 试题广场 >

添加逗号

[编程题]添加逗号
  • 热度指数:16244 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
\hspace{15pt}给定一个正整数 N \left(1 \leqq N \leqq 2\times10^9\right)
\hspace{15pt}现在需要将其转换为千分位格式,即从整数最低位开始,每三位数字插入一个英文逗号,以提高可读性。
\hspace{15pt}例如,对于 980364535,转换后为 980,364,535
\hspace{15pt}请编写程序完成该格式转换。

输入描述:
\hspace{15pt}在一行中输入一个整数 N \left(1 \leqq N \leqq 2\times10^9\right)


输出描述:
\hspace{15pt}输出一个字符串,表示将 N 转换为千分位格式后的结果。 
\hspace{15pt}请不要输出多余的空格或换行。
示例1

输入

980364535

输出

980,364,535
示例2

输入

6

输出

6

备注:

n=input()
kk=n[::-1]
s=''
j=1
for i in kk:
    s=s+i
    if j%3==0:
        s+=","
    j+=1
s=s[::-1]
if s[0]==",":
    print(s[1:])
else:
    print(s)
发表于 2022-03-06 21:40:57 回复(0)
#include<bits/stdc++.h>

using namespace std;

int main() {
    string s, res;
    cin >> s;
    reverse(s.begin(), s.end());
    for (int i = 0; i < s.size(); ++i) {
        if (i % 3 == 0 && i != 0) res += ',';
        res += s[i];
    }
    reverse(res.begin(), res.end());
    cout << res;
    return 0;
}

发表于 2022-03-31 13:06:10 回复(0)
输入数据格式化输出
a = int(input())
print("{:,}".format(a))

编辑于 2024-02-11 17:04:41 回复(0)
#include <stdio.h>
void add(long long n){
    if(n<1000){
        printf("%lld",n);
    }else {
    add(n/1000);
    printf(",%03d",n%1000);
    }
}
int main() {
    long long N=0;
    scanf("%lld",&N);
    add(N);
    return 0;
}

发表于 2024-02-11 19:12:17 回复(0)
import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        long num = scanner.nextLong();
        //printf有自动分割
        System.out.printf("%,d",num);
    }
}

发表于 2022-07-05 11:02:15 回复(0)
#include <stdio.h>

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

发表于 2022-06-15 00:26:38 回复(0)
print("{:,}".format(int(input())))

发表于 2022-04-08 23:16:03 回复(1)
int main() {
    char arr1[20]={0};
    int a=0;
    scanf("%d\n",&a);
    int i=0;
    int b=0;
        while(a)
    {
    if(b!=0&& b%3==0)
    arr1[i++]=',';
        arr1[i++]=a%10+'0';
        a=a/10;
        b++;
    }
for(i--;i>=0;i--)
{
printf("%c",arr1[i]);
}
    return 0;
}
发表于 2024-04-29 17:49:39 回复(0)
int main() {
    int n;
    char str[20];//用来逆序存放最终输出的数字n和字符','
    int i = 0;//作为str字符数组的下标
    int count = 0;//用来计数
    scanf("%d", &n); 
    while (n)//结束条件
    {
        if (count == 3)//count每到3就放一个逗号字符到字符数组里
        {
            str[i] = ',';
            count = 0;//count重新开始计数
            goto qu;
        }
        int num= n % 10;
        sprintf(&str[i], "%d", num);//sprintf是格式转换函数,把数字转换成对应的字符
        n /= 10;
        count++;
    qu:
        i++;//每放进去一个字符,下标就++一下
    }
    for (int j = i - 1; j >= 0; j--)//逆序打印出字符数组即可
    {
        printf("%c", str[j]);
    }
    return 0;
}



发表于 2024-04-28 09:56:57 回复(0)
#include <stdio.h>
#include <math.h>
int main() {
    long long N;
    scanf("%lld",&N);
    int num = N,count1 = 0;
    while(num != 0){
        num = num/10;
        count1++;
    }
    int count2 = (count1-0.1)/3;
    int gf = pow(1000,count2);
    int a = N/gf;
    N = N % gf;
    printf("%d",a);
    for(int i = 0;i < count2;i++){
        int bf = pow(1000,count2-i-1);
        a = N / bf;
        N = N % bf;
        if(a < 10){
            printf(",00%d",a);
        }
        else if(a < 100){
            printf(",0%d",a);
        }
        else{
            printf(",%d",a);}
    }

    return 0;
}
发表于 2024-03-04 10:40:30 回复(0)
const rl = require("readline").createInterface({ input: process.stdin });
var iter = rl[Symbol.asyncIterator]();
const readline = async () => (await iter.next()).value;

void (async function () {
    // Write your code here
    while ((line = await readline())) {
        let arr = line.split("");
        let n = arr.length;
        let arr1 = arr.reverse();
        let num = parseInt(n / 3);
        for (let index = 3; index < n + num; index += 3) {
            arr1.splice(index, 0, ",");
            index++;
        }
        if (n % 3 == 0) {
            arr1.pop();
        }
        let re = arr1.reverse()
        let str = re.join("");
        console.log(str);
    }
})();

发表于 2022-11-07 20:58:27 回复(0)
s = input()
ls = []
max,min = 0,1000
for i in s:
    ls.append(i)
for i in range(len(ls)): #得到max
    count = 0
    for j in range(len(ls)):
        if(ls[j] == ls[i]):
            count+=1
    if(count>=max):
        max = count
for i in range(len(ls)): #得到min
    count = 0
    for j in range(len(ls)):
        if(ls[j] == ls[i]):
            count+=1
    if(count<=min):
        min = count
true_value = max - min
flag = 0
if(true_value == 0):
    print("No Answer")
    print(true_value)
elif(true_value == 1):
    print("No Answer")
    print('0')
else:
    for i in range(2, true_value):
        if (true_value % i == 0):
            flag = 1
            print("No Answer")
            print(true_value)
            break
    if (flag == 0):
        print("Lucky Word")
        print(true_value)

发表于 2022-05-25 12:34:02 回复(0)
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNext()) { // 注意 while 处理多个 case
            String a = in.nextLine();
            char[] list1= a.toCharArray();
            int l = (list1.length % 3 == 0?list1.length + (list1.length / 3) - 1:list1.length + list1.length / 3);//计算添加千分位后新数组的长度
            //System.out.println(l);
            char[] list2= new char[l];

            int count = 0,num;
            int j = 0;
            for(int i = list1.length - 1;i >= 0;i--)
            {
                //从原数组末尾开始数,每三个添加一个,
                if(count == 3){list2[j] = ',';count=0;j++;list2[j] = list1[i];count++;}
                else{list2[j] = list1[i];count++;}
                num = j;
                j++;
            }
            for(int k = list2.length - 1;k >= 0;k--){System.out.printf("%c",list2[k]);}
        }
    }
}
发表于 2025-07-09 16:13:22 回复(0)
N=input()
rev_N=N[::-1]
a=len(N)

res=''
temp=0        
for i in rev_N :
    res+=i
    temp=temp + 1  
    if temp % 3 == 0 and temp!= a :
        res+=','
        continue

print(''.join(reversed(res)))

发表于 2025-07-07 21:35:55 回复(0)
N = input()
l = len(N)
m = l % 3
parts = []
if
m:
    parts.append(N[:m])
for i in range(m, l, 3):
    parts.append(N[i:i+3])
print(','.join(parts))
发表于 2025-07-07 15:06:29 回复(0)
N=str(input())
n=len(N)
N2=[]
i=0
if n>3:
    while i <(n):
        if i==0 and n%3==0:
            N2.append(''.join(N[i:3]))
            i+=3
        elif i==0 and n%3!=0:
            N2.append(''.join(N[i:(n%3)]))
            i+=(n%3)
        else:
            N2.append(''.join(N[i:i+3]))
            i+=3
    print(','.join(N2))
else:
    print(N)
发表于 2025-07-04 19:10:51 回复(0)
另辟蹊径
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNext()) { // 注意 while 处理多个 case
            String str = in.nextLine();
            int len = str.length();
            int m=len%3;
            if(m==0) str=str;
            if(m==1) str="xx"+str;
            if(m==2) str="x"+str;
            char[] ch=str.toCharArray();
            String res="";
            int count=0;
            for(int i=0;i<ch.length;){
                if(count==3){
                    res+=",";
                    count=0;
                }else{
                    res+=String.valueOf(ch[i]);
                    i++;
                    count++;
                }
            }
            char[] ress=res.toCharArray();
            for(int i =0;i<ress.length;i++){
                if(ress[i]!='x') System.out.print(ress[i]);
            }            
        }
    }
}

发表于 2025-06-11 18:28:33 回复(0)
看看我的
#include <iostream>

int main() {
    long long int n;
    std::cin >> n;
    std::string str = std::to_string(n);
    for (int i = str.size() - 3; i > 0; i = i -3) {
        str.insert(i, ",");
    }
    std::cout << str;

}


发表于 2025-06-10 15:12:37 回复(0)
def format_number(n):
    num_str = str(n)
    if len(num_str) <= 3:
        return num_str
    
    format_str = []
    for i in range(len(num_str),0,-3):
        format_str.append(num_str[max(i-3,0):i])
    
    return ",".join(format_str[::-1])

n = int(input())

# 输出结果
print(format_number(n))

发表于 2025-06-08 15:19:21 回复(0)
N = input()

N = str(N)[::-1]

new_string = ''
for i in range(len(N)):
    if i!=0 and i%3==0:
        new_string += ','
    new_string += N[i]
print(new_string[::-1])

发表于 2025-05-29 16:18:08 回复(0)

问题信息

难度:
67条回答 2593浏览

热门推荐

通过挑战的用户

查看代码
添加逗号