无法让我的列表用逗号正确打印出来

标签 c

我试图让我的程序找出一个数是否为质数,如果不是,则列出该数可以被什么数整除

#include <stdio.h>

int main()
{
    int n, i, j, k = 0, c = 0;

    printf("Enter an integer between 1 and 1000 (inclusive): \n");
    scanf("%d", &n);

    if (n > 1000 || n < 0) {
        printf("You must enter a number between 1 and 1000 (inclusive).\n");
    }
    else
    {
        for (i = 1; i <= n; i++)
        {
            if (n % i == 0) // check divisible number from 1 to n
            {
                c++; // count the divisible numbers
            }
        }
        if (c == 2) // c is 2 the number is prime
            printf("%d is prime.", n);
        else
        {
            printf("%d is divisible by ", n);
            for (i = 2; i <= 31; i++) // first 11 prime numbers
            {
                k = 0;
                for (j = 1; j <= i; j++)
                {
                    if (i % j == 0) //i=(2,3,7,11,13,17,19,23,31)
                    {
                        k++;
                    }
                }
                if (k == 2)
                {
                    if (n % i == 0) //if i prime number. n is divisible by i or not
                    printf("%d", i);
                    if (i < 5)
                    {
                        printf(", ");
                    }
                }
            }
            printf(".");
            printf("\n%d is not prime.\n", n);
        }
    }
    return 0;
}

目前,当我输入 62 时,它会输出

62 可以被 2, , 31 整除。

但是当我尝试更改 if(i < 3) 语句时,它会与其他打印结果混淆,例如尝试使用 468,它会打印出来

468 能被 2, 313 整除

最佳答案

以下建议代码:

  1. 干净地编译
  2. 不检查 I/O 错误
  3. 执行所需的功能
  4. 利用 C 的可变长度数组特性

现在,建议的代码:

#include <stdio.h>

int main()
{
    int n, c = 0;

    do {
        printf("Enter an integer between 1 and 1000 (inclusive): \n");
        scanf("%d", &n);
    } while( n > 1000 || n < 0 );

    int divisors[n];

    divisors[ 0 ] = 0;
    divisors[ 1 ] = 0;

    for ( int i = 2; i < n; i++)
    {
        if (n % i == 0) 
        {
            divisors[ i ] = i;
            c++; // count the divisible numbers
        }
        else
            divisors[ i ] = 0;
    }

    if ( !c ) 
        printf("%d is prime.", n);

    else
    {
        printf("%d is divisible by ", n);
        for( int i = 0; i < n; i++ )
        {
            if( divisors[i] )
            {
                printf( "%d ", i );
            }
        }

        printf(".");
        printf("\n%d is not prime.\n", n);
    }
    return 0;
}

以下运行使用 OP 提供的值

Enter an integer between 1 and 1000 (inclusive): 
62
62 is divisible by 2 31 .
62 is not prime.

Enter an integer between 1 and 1000 (inclusive): 
468
468 is divisible by 2 3 4 6 9 12 13 18 26 36 39 52 78 117 156 234 .
468 is not prime.

关于无法让我的列表用逗号正确打印出来,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58125938/

相关文章:

c - 程序的奇怪响应

c - 基本缓冲区溢出教程

从 x86 汇编语言调用 C 函数

c - C语言用指针交换一个数组与另一个数组的索引

c - 错误 : request for member xxxxxx in something not a structure or union

c - 向 C 添加程序参数

c - 多线程程序中的错误(运行缓慢)

c - 带指针的语句似乎缺少运算符

c - 使用 fgets 将行复制到字符串数组中?

c - 打印一个 char* 比一次打印一个 char 更快吗?