如何获取像123456
这样的数字并将其打印为1 2 3 4 5 6
发布于 2010-01-18 16:31:55
发布于 2010-01-18 16:34:57
一种常见的方法是提取每个数字,然后打印该数字。我不会给你代码,但它是的实现版本:
int d; // your number
/* While `d` is not zero */
/* Modulus `d` with 10 to extract the last digit */
/* Print it, with your space */
/* Divide by 10 to remove the last digit */
/* Repeat */
这将是倒退的。我将把它作为练习留给您来解决。(提示:在循环中,将结果放入一个字符数组中,完成后从数组的最后一个索引开始并向后打印。)
发布于 2010-01-18 16:55:21
char buffer[50];
int myNum = 123456;
int n;
int i;
n = snprintf(buffer, sizeof buffer, "%d", myNum);
for (i = 0; i < n; i++)
{
putchar(buffer[i]);
putchar(' ');
}
putchar('\n');
https://stackoverflow.com/questions/2084690
复制相似问题