检查 space 是否应该在 for 循环的中间或末尾打印?

Checking whether space should be printed in the middle or at the end in for loop?

我正在尝试向我的程序提供信息,它是否应该打印 space。

我的代码看起来像这样,最后打印 spaces(这不是我想要的)

void function(int given_limit)
{
    int current_num = 2;
    while (given_limit > 1)
    {
        if (given_limit % current_num == 0)
        {
            if (current_num == 2)
            {
                printf("%d ", current_num);
            }
            else if (current_num == 3)
            {
                printf("%d ", current_num);
            }
            else if (current_num == 5)
            {
                printf("%d ", current_num);
            }
            else if (current_num == 7)
            {
                printf("%d ", current_num);
            }
            else
            {
                printf("%d ", current_num);
            }

            given_limit /= current_num;
        }
        else
            current_num++;
    }
    printf("\n");
}

在 main() 中,我这样称呼它:

int main()
{
    int given_limit = 13;
    for (int i = 0; i <= given_limit; i++)
    {
        printf("%d\t\t", i);
        function(i);
    }
}

如果有任何提示和帮助,我将不胜感激。 其中一个想法可能是将它存储在一个数组中。

我用星号替换了 spaces 以获得更好的可见性,并删除了多余的 if-elements。然后我引入了一个标志,指示它是第一个因子还是后面一个因子的输出。在后面的每一个前面,我们放置 space(或星号)。

#include <stdio.h>
#include <stdbool.h>
void function(int given_limit)
{
    bool is_first_factor = true;

    int current_num = 2;
    while (given_limit > 1)
    {
        if (given_limit % current_num == 0)
        {
            if (is_first_factor) {
                is_first_factor = false; // not first anymore
                // print nothing
            } else {
                printf("*"); // between two factors
            }            
            printf("%d", current_num);

            given_limit /= current_num;
        }
        else
            current_num++;
    }
    printf("\n");
}

int main(int argc, char **argv)
{
    int given_limit = 13;
    for (int i = 0; i <= given_limit; i++)
    {
        printf("%d\t\t", i);
        function(i);
    }
}
$ gcc spacing.c
$ ./a.out      
0
1
2               2
3               3
4               2*2
5               5
6               2*3
7               7
8               2*2*2
9               3*3
10              2*5
11              11
12              2*2*3
13              13
$    

如上所述,将 space 字符移动到每个质因数的前面,然后对齐输出以考虑初始起始 space 字符。

这个例子也跳过了不必要的因素。

/* primefactors.c

*/

#include <stdio.h>

void primeFactors(int number)
{
    printf("  %2d   ", number);

    // only test factors <= sqrt(number) 
    // skip even factors > 2
    
    int factor = 2;
    while (factor <= number / factor) {
        if (number % factor == 0) {
            printf(" %d", factor);
            number /= factor;
        }
        else if (factor == 2){
            factor = 3;
        }
        else {
            factor += 2;
        }
    }

    // at this point number equals the greatest prime factor
    printf(" %d\n", number);
}

int main (void)
{
    int max = 45;

    printf("\nnumber  prime factors\n");
    printf("------  -------------\n");

    // skip 0 and 1 which have no prime factors
    printf("  %2d\n", 0);
    printf("  %2d\n", 1);

    for (int i = 2; i <= max; ++i) {
        primeFactors(i);
    }
      
    return 0;
}