列表

详情


NC24754. [USACO 2010 Dec B]Adding Commas

描述

Bessie is working with large numbers N (1 <= N <= 2,000,000,000) like 153920529 and realizes that the numbers would be a lot easier to read with commas inserted every three digits (as is normally done in the USA; some countries prefer to use periods every three digits). Thus, she would like to add commas: 153,920,529 . Please write a program that does this.

输入描述

* Line 1: A single integer: N

输出描述

* Line 1: The integer N with commas inserted before each set of three digits except the first digits (as traditionally done in many cultures)

示例1

输入:

153920529

输出:

153,920,529

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

Pascal(fpc 3.0.2) 解法, 执行用时: 2ms, 内存消耗: 256K, 提交时间: 2019-10-13 08:17:40

var
s:string;
n,i,a:longint;
begin
readln(s);
n:=length(s);
a:=3-(n mod 3);
for i:=1 to n-1 do
 begin
  write(s[i]);
  if (i+a) mod 3=0 then write(',');
 end;
writeln(s[n]);
end.

C++14(g++5.4) 解法, 执行用时: 3ms, 内存消耗: 376K, 提交时间: 2020-07-14 20:30:13

#include<iostream>
using namespace std;
string s;
int main()
{   int len,i;
	cin>>s;
	len=s.length();
	for(i=len-3;i>0;i-=3)
	s.insert(i,","); 
	cout<<s;
	return 0;
	
	
}

matlab(Octave 4.0.0) 解法, 执行用时: 105ms, 内存消耗: 10072K, 提交时间: 2019-10-13 21:18:24

a=input('','s');
b=length(a);
for i=1:b-1
    fprintf('%c',a(i));
    if mod(b-i,3) == 0
        fprintf(',');
    end
end
fprintf('%c',a(b));

JavaScript V8 解法, 执行用时: 11ms, 内存消耗: 5540K, 提交时间: 2023-05-25 01:31:15

print(readline().replace(/\B(?=(\d{3})+(?!\d))/g, ','));

Python(2.7.3) 解法, 执行用时: 10ms, 内存消耗: 2816K, 提交时间: 2020-07-15 15:14:24

print(format(int(input()), ","))

Python3(3.5.2) 解法, 执行用时: 38ms, 内存消耗: 3448K, 提交时间: 2019-10-12 16:23:55

print(format(int(input()), ','))

上一题