C++c语言输入输出的格式123 456 789输出456格式

以下试题来自:
单项选择题有以下程序:
{ int m,n,p; scanf("m=%dn=%dp=%d",&m,&n,&p); printf("%d%d%d\n",m,n,p);
若想从键盘上输入数据,使变量 m 中的值为 123,n 中的值为 456,p 中的值为 789,
则正确的输入是______。A.m=123n=456p=789 B.m=123 n=456 p=789 C.m=123,n=456,p=789 D.123 456 789
为您推荐的考试题库
你可能感兴趣的试题
1A.以上的说明形式非法 B.ST 是一个结构体类型 C.NEW 是一个结构体类型 D.NEW 是一个结构体变量2A.15B.16C.12D.593A.模块的内聚程度要尽量高,且各模块间的耦合程度要尽量强
B.模块的内聚程度要尽量高,且各模块间的耦合程度要尽量弱
C.模块的内聚程度要尽量低,且各模块间的耦合程度要尽量弱
D.模块的内聚程度要尽量低,且各模块间的耦合程度要尽量强4A.Main B._0 C._int D.sizeof5A.关系模型 B.网状模型 C.层次模型 D.以上三个都是
热门相关试卷
最新相关试卷C语言 带逗号的字符串“123,456,789”转换数字,怎么转换?_百度知道
C语言 带逗号的字符串“123,456,789”转换数字,怎么转换?
我有更好的答案
&&isdigit(pstrRun[0]))
iresult&0;10;
iresult&+=&(pstrRun[0]&*&pstrRun&&*=&iresult&=&\0') {
if&nbsp写的不好,也不知道是否符合c语言的规范;pstr){ int&nbsp!=&'&pstrRun[0]&255&&math.h&
++pstrR } return&char&int&StrToNum(const&char&*& const&);-&&#39,按c++到是可以编译,看着用吧;&&=&& while&nbsp:#include&(pstrRun[0]&&&1&0&#39。。;(pstrRun[0]&
采纳率:57%
从前向后获取每个字符,然后转换为数字,total = c然后获取下一个字符转为数字,然后进行进阶加 total=total*10+c如果是逗号,忽略,继续取下一个字符
输出是写%s以字符串的形式输出。
为您推荐:
其他类似问题
字符串的相关知识
换一换
回答问题,赢新手礼包
个人、企业类
违法有害信息,请在下方选择后提交
色情、暴力
我们会通过消息、邮箱等方式尽快将举报结果通知您。formatting - How to format a number from
to 1,123,456,789 in C? - Stack Overflow
Join Stack Overflow to learn, share knowledge, and build your career.
or sign in with
How can I in C language format a number from
to 1,123,456,789?
I tried using printf("%'10d\n", ); but that doesn't work.
Could you advise anything? The simpler the solution the better.
7,75713875
1,716133643
If your printf supports the ' flag, you can probably do it just by setting your locale appropriately.
#include &stdio.h&
#include &locale.h&
int main(void)
setlocale(LC_NUMERIC, "");
printf("%'d\n", );
And build & run:
$ ./example
1,123,456,789
Tested on Mac OS X & Linux (Ubuntu 10.10).
165k20312399
You can do it recursively as follows (beware INT_MIN if you're using two's complement, you'll need extra code to manage that):
void printfcomma2 (int n) {
if (n & 1000) {
printf ("%d", n);
printfcomma2 (n/1000);
printf (",%03d", n%1000);
void printfcomma (int n) {
if (n & 0) {
printf ("-");
printfcomma2 (n);
A summmary:
User calls printfcomma with an integer, the special case of negative numbers is handled by simply printing "-" and making the number positive (this is the bit that won't work with INT_MIN).
When you enter printfcomma2, a number less than 1,000 will just print and return.
Otherwise the recursion will be called on the next level up (so 1,234,567 will be called with 1,234, then 1) until a number less than 1,000 is found.
Then that number will be printed and we'll walk back up the recursion tree, printing a comma and the next number as we go.
There is also the more succinct version though it does unnecessary processing in checking for negative numbers at every level (not that this will matter given the limited number of recursion levels). This one is a complete program for testing:
#include &stdio.h&
void printfcomma (int n) {
if (n & 0) {
printf ("-");
printfcomma (-n);
if (n & 1000) {
printf ("%d", n);
printfcomma (n/1000);
printf (",%03d", n%1000);
int main (void) {
int x[] = {-, -123456, -12345, -1000, -999, -1,
0, 1, 999, , };
while (px != &(x[sizeof(x)/sizeof(*x)])) {
printf ("%-15d: ", *px);
printfcomma (*px);
printf ("\n");
and the output is:
: -1,234,567,890
: -123,456
: 1,234,567,890
An iterative solution for those who don't trust recursion (although the only problem with recursion tends to be stack space which will not be an issue here since it'll only be a few levels deep even for a 64-bit integer):
void printfcomma (int n) {
int n2 = 0;
int scale = 1;
if (n & 0) {
printf ("-");
while (n &= 1000) {
n2 = n2 + scale * (n % 1000);
n /= 1000;
scale *= 1000;
printf ("%d", n);
while (scale != 1) {
scale /= 1000;
printf (",%03d", n);
Both of these generate 2,147,483,647 for INT_MAX.
580k15311511586
Here's a very simple implementation. This function contains no error checking, buffer sizes must be verified by the caller. It also does not work for negative numbers. Such improvements are left as an exercise for the reader.
void format_commas(int n, char *out)
char buf[20];
sprintf(buf, "%d", n);
c = 2 - strlen(buf) % 3;
for (p = *p != 0; p++) {
*out++ = *p;
if (c == 1) {
*out++ = ',';
c = (c + 1) % 3;
*--out = 0;
610k1309781127
I do this all the time, using gcc/g++ and glibc on linux and yes, the ' operator may be non-standard, but I like the simplicity of it.
#include &stdio.h&
#include &locale.h&
int main()
int bignum=;
setlocale(LC_ALL,"");
printf("Big number: %'d\n",bignum);
Gives output of:
Big number: 12,345,678
Just have to remember the 'setlocale' call in there, otherwise it won't format anything.
Perhaps a locale-aware version would be interesting.
#include &stdlib.h&
#include &locale.h&
#include &string.h&
#include &limits.h&
static int next_group(char const **grouping) {
if ((*grouping)[1] == CHAR_MAX)
if ((*grouping)[1] != '\0')
size_t commafmt(char
/* Buffer for formatted string
/* Size of buffer
/* Number to convert
int len = 1;
int posn = 1;
int sign = 1;
char *ptr = buf + bufsize - 1;
struct lconv *fmt_info = localeconv();
char const *tsep = fmt_info-&thousands_
char const *group = fmt_info-&
char const *neg = fmt_info-&negative_
size_t sep_len = strlen(tsep);
size_t group_len = strlen(group);
size_t neg_len = strlen(neg);
int places = (int)*
if (bufsize & 2)
*buf = '\0';
*ptr-- = '\0';
if (N & 0L)
sign = -1;
for ( ; len &= ++len, ++posn)
*ptr-- = (char)((N % 10L) + '0');
if (0L == (N /= 10L))
if (places && (0 == (posn % places)))
places = next_group(&group);
for (int i=sep_ i&0; i--) {
*ptr-- = tsep[i-1];
if (++len &= bufsize)
goto ABORT;
if (len &= bufsize)
goto ABORT;
if (sign & 0)
if (len &= bufsize)
goto ABORT;
for (int i=neg_ i&0; i--) {
*ptr-- = neg[i-1];
if (++len &= bufsize)
goto ABORT;
memmove(buf, ++ptr, len + 1);
return (size_t)
#ifdef TEST
#include &stdio.h&
#define elements(x) (sizeof(x)/sizeof(x[0]))
void show(long i) {
char buffer[32];
commafmt(buffer, sizeof(buffer), i);
printf("%s\n", buffer);
commafmt(buffer, sizeof(buffer), -i);
printf("%s\n", buffer);
int main() {
long inputs[] = {1, 12, 123, , 378 };
for (int i=0; i&elements(inputs); i++) {
setlocale(LC_ALL, "");
show(inputs[i]);
This does have a bug (but one I'd consider fairly minor). On two's complement hardware, it won't convert the most-negative number correctly, because it attempts to convert a negative number to its equivalent positive number with N = -N; In two's complement, the maximally negative number doesn't have a corresponding positive number, unless you promote it to a larger type. One way to get around this is by promoting the number the corresponding unsigned type (but it's is somewhat non-trivial).
362k37428852
Without recursion or string handling, a mathematical approach:
#include &stdio.h&
#include &math.h&
void print_number( int n )
int order_of_magnitude = (n == 0) ? 1 : (int)pow( 10, ((int)floor(log10(abs(n))) / 3) * 3 ) ;
printf( "%d", n / order_of_magnitude ) ;
for( n = abs( n ) % order_of_magnitude, order_of_magnitude /= 1000;
order_of_magnitude & 0;
n %= order_of_magnitude, order_of_magnitude /= 1000 )
printf( ",%03d", abs(n / order_of_magnitude) ) ;
Similar in principle to Pax's recursive solution, but by calculating the order of magnitude in advance, recursion is avoided (at some considerable expense perhaps).
Note also that the actual character used to separate thousands is locale specific.
Edit:See @Chux's comments below for improvements.
53.1k851110
Based on @Greg Hewgill's, but takes negative numbers into account and returns the string size.
size_t str_format_int_grouped(char dst[16], int num)
char src[16];
char *p_src =
char *p_dst =
const char separator = ',';
int num_len,
num_len = sprintf(src, "%d", num);
if (*p_src == '-') {
*p_dst++ = *p_src++;
num_len--;
for (commas = 2 - num_len % 3; *p_ commas = (commas + 1) % 3) {
*p_dst++ = *p_src++;
if (commas == 1) {
*p_dst++ =
*--p_dst = '\0';
return (size_t)(p_dst - dst);
Another iterative function
int p(int n) {
if(n & 0) {
printf("-");
int a[sizeof(int) * CHAR_BIT / 3] = { 0 };
while(n & 0) {
*++pa = n % 1000;
n /= 1000;
printf("%d", *pa);
while(pa & a + 1) {
printf(",%03d", *--pa);
385k897401098
Here is the slimiest, size and speed efficient implementation of this kind of decimal digit formating:
const char *formatNumber (
int value,
char *endOfbuffer,
bool plus)
int savedV
savedValue =
if (unlikely (value & 0))
*--endOfbuffer = 0;
charCount = -1;
if (unlikely (++charCount == 3))
charCount = 0;
*--endOfbuffer = ',';
*--endOfbuffer = (char) (value % 10 + '0');
while ((value /= 10) != 0);
if (unlikely (savedValue & 0))
*--endOfbuffer = '-';
else if (unlikely (plus))
*--endOfbuffer = '+';
return endO
Use as following:
char buffer[16];
fprintf (stderr, "test : %s.", formatNumber (, buffer + 16, true));
test : +1,234,567,890.
Some advantages:
Function taking end of string buffer because of reverse ordered formatting. Finally, where is no need in revering generated string (strrev).
This function produces one string that can be used in any algo after. It not depends nor require multiple printf/sprintf calls, which is terrible slow and always context specific.
Minimum number of divide operators (/, %).
another solution, by saving the result into an int array, maximum size if 7 because of the long long int can handle number in the range 9,223,372,036,854,775,807 to -9,223,372,036,854,775,807 note it is not an unsigned
non recursive printing function
static void printNumber (int numbers[8], int loc, int negative)
if (negative)
printf("-");
if (numbers[1]==-1)//one number
printf("%d ", numbers[0]);
printf("%d,", numbers[loc]);
while(loc--)
if(loc==0)
{// last number
printf("%03d ", numbers[loc]);
{ // number in between
printf("%03d,", numbers[loc]);
main function call
static void getNumWcommas (long long int n, int numbers[8])
int negative=0;
if (n & 0)
negative = 1;
for(i = 0; i&7; i++)
if (n & 1000)
numbers[i] =
numbers[i+1] = -1;
numbers[i] = n%1000;
printNumber(numbers, i, negative);// non recursive print
testing output
-4775807: -9,223,372,036,854,775,807
: -1,234,567,890
: -123,456
: 1,234,567,890
4775807 : 9,223,372,036,854,775,807
in main() class
int numberSeperated[8];
long long int number = LL;
getNumWcommas(number, numberSeperated );
if printing is all needed then move int numberSeperated[8]; inside the function getNumWcommas and call it this way getNumWcommas(number);
14.4k24112210
My answer does not format the result exactly like the illustration in the question, but may fulfill the actual need in some cases with a simple one-liner or macro. One can extend it to generate more thousand-groups as necessary.
The result will look for example as follows:
Value: 0'000'012'345
printf("Value: %llu'%03lu'%03lu'%03lu\n", (value / 1000 / 1000 / 1000), (value / 1000 / 1000) % 1000, (value / 1000) % 1000, value % 1000);
1,99012346
Secure format_commas, with negative numbers:
Because VS & 2015 doesn't implement snprintf, you need to do this
#if defined(_WIN32)
#define snprintf(buf,len, format,...) _snprintf_s(buf, len,len, format, __VA_ARGS__)
char* format_commas(int n, char *out)
char buf[100];
char* q = // Backup pointer for return...
if (n & 0)
*out++ = '-';
n = abs(n);
snprintf(buf, 100, "%d", n);
c = 2 - strlen(buf) % 3;
for (p = *p != 0; p++) {
*out++ = *p;
if (c == 1) {
*out++ = '\'';
c = (c + 1) % 3;
*--out = 0;
Example usage:
size_t currentSize = getCurrentRSS();
size_t peakSize = getPeakRSS();
printf("Current size: %d\n", currentSize);
printf("Peak size: %d\n\n\n", peakSize);
char* szcurrentSize = (char*)malloc(100 * sizeof(char));
char* szpeakSize = (char*)malloc(100 * sizeof(char));
printf("Current size (f): %s\n", format_commas((int)currentSize, szcurrentSize));
printf("Peak size (f): %s\n", format_commas((int)currentSize, szpeakSize));
free(szcurrentSize);
free(szpeakSize);
40.5k54242339
There's no real simple way to do this in C. I would just modify an int-to-string function to do it:
void format_number(int n, char * out) {
int out_index = 0;
for (i = i != 0; i /= 10) {
digit = i % 10;
if ((out_index + 1) % 4 == 0) {
out[out_index++] = ',';
out[out_index++] = digit + '0';
out[out_index] = '\0';
// then you reverse the out string as it was converted backwards (it's easier that way).
// I'll let you figure that one out.
strrev(out);
114k34147181
A modified version of @paxdiablo solution, but using WCHAR and wsprinf:
static WCHAR buffer[10];
static int pos = 0;
void printfcomma(const int &n) {
if (n & 0) {
wsprintf(buffer + pos, TEXT("-"));
pos = lstrlen(buffer);
printfcomma(-n);
if (n & 1000) {
wsprintf(buffer + pos, TEXT("%d"), n);
pos = lstrlen(buffer);
printfcomma(n / 1000);
wsprintf(buffer + pos, TEXT(",%03d"), n % 1000);
pos = lstrlen(buffer);
void my_sprintf(const int &n)
printfcomma(n);
user586399
I'm new in C programming. Here is my simple code.
int main()
1223 =& 1,223
int a[10];
printf(" n: ");
scanf_s("%d", &n);
int i = 0;
while (n & 0)
int temp = n % 1000;
n /= 1000;
for (int j = i - 1; j &= 0; j--)
if (j == 0)
printf("%d.", a[j]);
else printf("%d,",a[j]);
#include &stdio.h&
void punt(long long n){
char s[28];
int i = 27;
if(n&0){n=-n; putchar('-');}
s[i--] = n%10 + '0';
if(!(i%4) && n&9)s[i--]='.';
}while(n);
puts(&s[++i]);
int main(){
punt(987);
punt(9876);
punt(-987);
punt(-9876);
punt(-654321);
punt(0x7FFFFFFFFFFFFFFF);
punt(0x0001); // -max + 1 ...
My solution uses a . instead of a ,
It is left to the reader to change this.
Can be done pretty easily...
//Make sure output buffer is big enough and that input is a valid null terminated string
void pretty_number(const char* input, char * output)
int iInputLen = strlen(input);
int iOutputBufferPos = 0;
for(int i = 0; i & iInputL i++)
if((iInputLen-i) % 3 == 0 && i != 0)
output[iOutputBufferPos++] = ',';
output[iOutputBufferPos++] = input[i];
output[iOutputBufferPos] = '\0';
Example call:
char szBuffer[512];
pretty_number("1234567", szBuffer);
//strcmp(szBuffer, "1,234,567") == 0
233k94518582
void printfcomma ( long long unsigned int n)
char nstring[100];
sprintf(nstring,"%llu",n);
m=strlen(nstring);
for (i=0;i&i++)
// print first digits before comma
printf("%c", nstring[i]);
printf(",");
for (i=i&m;i++)
// print the rest inserting commas
printf("%c",nstring[i]);
if (j%3==0)
if(i&(m-1)) printf(",");
3,50472443
// separate thousands
int idx = 0;
static char buffer[32];
char* p = &buffer[32];
*--p = '\0';
for (int i = fC i != 0; i /= 10)
digit = i % 10;
if ((p - buffer) % 4 == 0)
*--p = ' ';
*--p = digit + '0';
7,65812954
Your Answer
Sign up or
Sign up using Google
Sign up using Facebook
Post as a guest
Post as a guest
By posting your answer, you agree to the
Not the answer you're looking for?
Browse other questions tagged
Upcoming Events
Stack Overflow works best with JavaScript enabled关于数字分组 ( =& 123,456,789.99)解决方案 - C++当前位置:& &&&关于数字分组 ( =& 123,456,789.99)关于数字分组 ( =& 123,456,789.99)解决方案www.MyException.Cn&&网友分享于:&&浏览:31次关于数字分组 ( =& 123,456,789.99)请问有没有效率高点的分组算法?------解决方案--------------------
很早的时候写过一个函数C/C++ codechar * dcomma(double d, int n)
/* 整数部分 */
char *p, t[50];
static char s[50];
if( n & 8 ) n = 8;
else if( n & 0 ) n = 0;
if( n == 0 ) flag = 1;
s[sizeof(s) - 1] = 0;
sprintf(t, &%.*lf&, n, d);
p = t + strlen(t) - 1;
for( i = sizeof(s) - 1, j = 0; p - t &= 0; p-- )
if( flag == 1 ) j++;
else if( *p == '.' ) flag = 1;
s[i--] = *p;
if( j == 3 && p - t && *(p-1) != '-' )
s[i--] = ',';
return(s + i + 1);
12345678910
12345678910
12345678910 上一篇:下一篇:文章评论相关解决方案 12345678910 Copyright & &&版权所有C++如何采用双层循环分三行显示123\n 456\n 789\n_百度知道
C++如何采用双层循环分三行显示123\n 456\n 789\n
for(int j=i;= 3;&lt要求; j &= i+3; j++)
cout &lt:采用双层循环输出以下内容123456789白话C++ 上是这样的,但运行结果不对呀for(int i=1; i &lt
我有更好的答案
&= 3; i++){
for(int j=3*(i-1)+1; j &= i*3; j++)
cout &ltfor(int i=1; i &lt
采纳率:67%
来自团队:
i &= 9; i+=3) {
for(int j=i; j &
cout &iostream& i+3; j++)
cout && } return 0#include &int main(){ for(int i=1;&lt
//你的架子是对的,但是循环的控制上校小有问题,for(int&i=1;&i&&=&9;&i++){&&for(int&j=1;&j&&=&3;&j++)&&&&{&&&&&&cout&&&&i;&&&&}&&&cout&&&&}
第一个循环i=0第二个循环j=3*i 1
其他1条回答
为您推荐:
其他类似问题
换一换
回答问题,赢新手礼包
个人、企业类
违法有害信息,请在下方选择后提交
色情、暴力
我们会通过消息、邮箱等方式尽快将举报结果通知您。

我要回帖

更多关于 c 输入输出格式 的文章

 

随机推荐