Steve_Ive
Steve_Ive
采纳率100%
2019-04-03 19:43 浏览 1.0k

请问C语言中多余的空行是怎么回事呀?是编译器的问题吗?

有一个C语言的编程练习,是输入正整数,然后显示所有小于该数的质数。
我写出来是这样的

#include <stdio.h>

int check_prime(long unsigned checking_num);

int main(void)
{
    long unsigned entered_num;
    long unsigned checking_num;
    int count = 0;

    printf("Enter an integer, this program will show you all the prime number less than the number you entered.\n");
    while (scanf("%lu", &entered_num) == 1)
    {
        printf("There are the following prime number less than the number you entered:\n");
        for (checking_num = 2,count = 0; checking_num < entered_num; checking_num++)
        {



            if (check_prime(checking_num) == 1)
            {

                printf("%lu\t", checking_num);
                count++;
            }
            if (count % 5 == 0)
                printf("\n");
        }
        printf("\n");

        if (count == 0)
            printf("There is no prime number less than the number you entered in.\n");
        printf("\nEnter another number to continue, Q to quit.\n");
    }

    return 0;

}

int check_prime(long unsigned checking_num)
{
    long unsigned div;
    int isprime;

    for (div = 2, isprime = 1; div*div <= checking_num; div++)
    {
        if ( checking_num % div == 0)
            isprime = 0;
    }

    return isprime;
}

其中有一个每输出5个质数就输出一个换行,但是不知道为什么总会多出来很多空行,截图如下图片说明
请大佬帮忙解决一下...

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

3条回答 默认 最新

  • 已采纳
    qq_40860484 依稀梦兮 2019-04-03 20:10

    原因:程序有一个bug,以第一行举例,你运行到第五个素数时,下一个数为12同时count为5,此时if(素数)判断为false,往下执行if(count)语句为true,进入执行换行
    解决方法:把判断count的if语句放在前面的if素数语句里面
    望采纳

    点赞 2 评论 复制链接分享
  • qq_38551717 蒙召. 2019-04-03 19:57

    已测试,望采纳:

    #include <stdio.h>
    
    int check_prime(long unsigned checking_num);
    
    int main(void)
    {
        long unsigned entered_num;
        long unsigned checking_num;
        int count = 0;
    
        printf("Enter an integer, this program will show you all the prime number less than the number you entered.\n");
        while (scanf("%lu", &entered_num) == 1)
        {
            printf("There are the following prime number less than the number you entered:\n");
            for (checking_num = 2,count = 0; checking_num < entered_num; checking_num++)
            {
    
    
    
                if (check_prime(checking_num) == 1)
                {
    
                    printf("%lu\t", checking_num);
                    count++;
                if (count % 5 == 0)     //位置改了一下
                    printf("\n");
                }
            }
            printf("\n");
    
            if (count == 0)
                printf("There is no prime number less than the number you entered in.\n");
            printf("\nEnter another number to continue, Q to quit.\n");
        }
    
        return 0;
    
    }
    
    int check_prime(long unsigned checking_num)
    {
        long unsigned div;
        int isprime;
    
        for (div = 2, isprime = 1; div*div <= checking_num; div++)
        {
            if ( checking_num % div == 0)
                isprime = 0;
        }
    
        return isprime;
    }
    
    点赞 1 评论 复制链接分享
  • shitDjango oneDjango 2019-04-03 19:49

    print里的 \t 制表符 自动后移一个tab的位置 去掉就ok

    点赞 评论 复制链接分享

相关推荐