C语言中数字之间插入空格

发布于 2024-08-18 22:48:00 字数 72 浏览 6 评论 0原文

我该如何获取像 123456 这样的数字并将其打印为 1 2 3 4 5 6

How would I go about taking a number like 123456 and having it print as 1 2 3 4 5 6?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

半世蒼涼 2024-08-25 22:48:00

最简单的方法(尽管不是最快的)可能是首先 sprintf< /code>将数字存入字符串缓冲区,然后循环遍历缓冲区 printf - 一次输入一个字符和一个空格。

标准 printf 格式中没有内置方法可以执行此操作。

The simplest way of doing this (though not the fastest) would probably be to first sprintf the number to a string buffer, and then loop through the buffer printf-ing one character and one space at a time.

There's no built-in way of doing this within the standard printf formatting.

无言温柔 2024-08-25 22:48:00

正如“jamesdlin”在他的评论中提到的那样,GMan 的方法是可行的,但是您需要将其存储在缓冲区中,以便以正确的顺序打印(他的算法将打印出“6 5 4 3 2 1”作为输入) 123456)。在这一点上,我想说,按照他的答案中建议的“therefromhere”使用 sprintf 会简单得多(当然,如果这不是算法类作业)。

在我看来,最简单的方法是使用递归,这样您就可以按正确的顺序打印出数字,而无需使用缓冲区。

递归实现非常简单:

void PrintfRecursivly(int number)
{
     if (number < 0) 
     {
       number *= -1;
       printf("- ");           
     }
     if (number > 10) 
     {
        PrintfRecursivly(number / 10);
        printf(" ");
     }

     printf("%d", number % 10);     
}

int main()
{
   int number = -78900456;
   PrintfRecursivly(number);

   return 0;
}

输入:

-78900456

输出:

<前>- 7 8 9 0 0 4 5 6

编辑:感谢 Steve Jessop 在我不在的时候建议了一个正确的正整数算法。我更改了上述方法以正确打印所有整数(正数和负数),没有最后一个空格。

请注意,我们可以避免在每次递归中检查负值,只需检查一次(在主函数中或其他地方),但我没有编写它,因为我们在清晰度上的损失会比性能上的收益更多。

As 'jamesdlin' has mentioned in his comment, GMan's approach would work, however you will need to store it in a buffer in order to print out in the correct order (his algorithm would print out "6 5 4 3 2 1" for input 123456). At this point I'd say that it would be much simpler to just use sprintf as 'therefromhere' suggested in his answer (if this is not an algorithm class assignment of course).

In my opinion the simplest way to do it would be using recursion, this way you can print out digits in the right order without using buffers.

The recursive implementation is very simple:

void PrintfRecursivly(int number)
{
     if (number < 0) 
     {
       number *= -1;
       printf("- ");           
     }
     if (number > 10) 
     {
        PrintfRecursivly(number / 10);
        printf(" ");
     }

     printf("%d", number % 10);     
}

int main()
{
   int number = -78900456;
   PrintfRecursivly(number);

   return 0;
}

Input:

-78900456

Output:

- 7 8 9 0 0 4 5 6

EDIT: Thanks to Steve Jessop who suggested a correct algorithm for positive integers while I was away. I changed the above method to print out correctly for all ints (positive and negative), without the last space.

Please note that we can avoid checking for negative values in every recursion, by doing the check just once (in the main function or wherever) but I didn't write it because we would lose more on clarity than gain in performance.

千秋岁 2024-08-25 22:48:00

一种常见的方法是提取每个数字,然后打印该数字。我不会给你代码,但它是以下内容的实现版本:

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 */

这将是向后的。我将把它作为练习留给您来解决这个问题。 (提示:在循环中,将结果放入字符数组中,完成后从数组的最后一个索引开始向后打印。)

A common method would be to extract each digit, and then print that digit. I won't give you the code, but it's the implemented version of:

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 */

This will be backwards. I'll leave it as an exercise to you to fix that. (Hint: In the loop, put the result into an array of characters, and when you're finished start at the last index of the array and print backwards.)

情场扛把子 2024-08-25 22:48:00

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');

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');
那片花海 2024-08-25 22:48:00
int number = 123456;
char strNumber[64];
strNumber[0] = '\0';
sprintf_s(strNumber, "%d", number);
int i = 0;
while(strNumber[i] != '\0')
    printf("%c ", strNumber[i++]);
int number = 123456;
char strNumber[64];
strNumber[0] = '\0';
sprintf_s(strNumber, "%d", number);
int i = 0;
while(strNumber[i] != '\0')
    printf("%c ", strNumber[i++]);
£噩梦荏苒 2024-08-25 22:48:00

这只适用于无符号整数:

#include <stdio.h>
#include <math.h>

void print_number(unsigned int number) {
    int n = number, c = 0, p;
    while (n > 0) {
        n /= 10;
        c++;
    }
    for (n = c - 1; n >= 0; n--) {
        p = pow(10, n);
        printf("%d ", number / p);
        number -= number / p * p;
    }
    printf("\n");
}


int main(int argc, char *argv[]) {
    print_number(1);
    print_number(12);
    print_number(123);
    print_number(1234);
    print_number(12345);
    print_number(1234567);
    print_number(12345678);
    print_number(123456789);
    return 0;
}

This only works for unsigned integers:

#include <stdio.h>
#include <math.h>

void print_number(unsigned int number) {
    int n = number, c = 0, p;
    while (n > 0) {
        n /= 10;
        c++;
    }
    for (n = c - 1; n >= 0; n--) {
        p = pow(10, n);
        printf("%d ", number / p);
        number -= number / p * p;
    }
    printf("\n");
}


int main(int argc, char *argv[]) {
    print_number(1);
    print_number(12);
    print_number(123);
    print_number(1234);
    print_number(12345);
    print_number(1234567);
    print_number(12345678);
    print_number(123456789);
    return 0;
}
谜泪 2024-08-25 22:48:00
#include <stdio.h>
int main ()
{
    int a, b, c, d, e;
    printf("Write a number of four figures\n");
    scanf("%d", &a);
    printf("Your number is:\n");

    b = (a - (a % 1000)) / 1000;
    c = ((a - (a % 100)) / 100) - b * 10;
    d = ((a - (a % 10)) / 10) - b * 100 - c * 10;
    e = (a - b * 1000 - c * 100 - d * 10);

    printf("%d\t%d\t", b, c);
    printf("%d\t", d);
    printf("%d", e);

    return 0;
}
#include <stdio.h>
int main ()
{
    int a, b, c, d, e;
    printf("Write a number of four figures\n");
    scanf("%d", &a);
    printf("Your number is:\n");

    b = (a - (a % 1000)) / 1000;
    c = ((a - (a % 100)) / 100) - b * 10;
    d = ((a - (a % 10)) / 10) - b * 100 - c * 10;
    e = (a - b * 1000 - c * 100 - d * 10);

    printf("%d\t%d\t", b, c);
    printf("%d\t", d);
    printf("%d", e);

    return 0;
}
荭秂 2024-08-25 22:48:00
#include <stdio.h>
#include <conio.h>

int main ()
{

    int n, a, b, c, d, e;
    printf("Enter integer number from 0 to 32, 767: ");
    scanf("%d", &n);
    printf("\n");
    a= n/10000%10;
    b= n/1000%10;
    c= n/100%10;
    d= n/10%10;
    e= n%10;
    printf("%d %d %d %d %d\n", a, b, c, d, e);

结果/输出:

Enter integer number from 0 to 32,767: 12345

1 2 3 4 5
#include <stdio.h>
#include <conio.h>

int main ()
{

    int n, a, b, c, d, e;
    printf("Enter integer number from 0 to 32, 767: ");
    scanf("%d", &n);
    printf("\n");
    a= n/10000%10;
    b= n/1000%10;
    c= n/100%10;
    d= n/10%10;
    e= n%10;
    printf("%d %d %d %d %d\n", a, b, c, d, e);

RESULTS/OUPUT:

Enter integer number from 0 to 32,767: 12345

1 2 3 4 5
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文