使用递归从用户处打印给定范围内两个最大素数的程序.

在这段代码中,我试图得到2个最大的素数并打印出来.你可以看到num1和num2是未使用的,它们应该是给定范围内两个最大素数的变量.

#include <stdio.h>

void usersInput(int *x, int *y);
void swapping(int *x, int *y);
int primeCheck(int i, int j);

int main() {
    int x, y, i, j, num1, num2;
    
    usersInput(&x, &y);

    if (x > y) {
        swapping(&x, &y);
    }

    printf("The value of x is %d, and the value of y is %d.\n", x, y);

    printf("\nThe two largest prime numbers from %d to %d are : ", x, y);
    for (i = x; i <= y; i++)
        if (primeCheck(i, y) == 0)
            printf("%d ", i);

    return 0;
}

void usersInput(int *x, int *y) {

    printf("Enter the value of x: ");
    scanf("%d", x);
    printf("Enter the value of y: ");
    scanf("%d", y);
}

void swapping(int *x, int *y) {
    int temp;
    temp = *x;
    *x = *y;
    *y = temp;
}

int primeCheck(int i, int j) {
    if (j == i) {
        return 0;
    } else if (j % i == 0) {
        return 1;
    } else {
        return primeCheck(i + 1, j);
    }
}

正如你所看到的,我还没有任何函数可以得到两个最大的素数.我真的不知道怎么做,所以请帮帮我

以下是实际输出:

Enter the value of x: 10
Enter the value of y: 3
The value of x is 3, and the value of y is 10.
The two largest prime numbers from 3 to 10 are: 6 7 8 9 10

以下是预期输出

Enter the value of x: 10
Enter the value of y: 3
The value of x is 3, and the value of y is 10.
The two largest prime numbers from 3 to 10 are: 5 7

推荐答案

代码中存在多个问题:

  • 素数if (primeCheck(i, y) == 0)的测试是不正确的:第一个参数应该通过2,第二个参数应该通过i.
  • 循环应该从i = y开始,在i >= x时运行,递减i以首先找到范围内最大的素数.
  • 你应该储存素数并在找到2时停止.
  • 素性测试函数应该修改为返回1 if i > j,以防止对某些输入(如primeCheck(2, 1))进行无限递归.
  • 测试小于等于sqrt(j)的除数在primeCheck()中就足够了.您可以在j < i * i时停止递归,这可以在没有潜在溢出的情况下进行测试,如j / i < i.

以下是您的代码的修改版本:

#include <stdio.h>

int usersInput(int *x, int *y);
void swapping(int *x, int *y);
int primeCheck(int i, int j);

int main() {
    int x, y, i, num1, num2;
    
    if (!usersInput(&x, &y)) {
        return 1;
    }
    if (x > y) {
        swapping(&x, &y);
    }

    printf("The value of x is %d, and the value of y is %d.\n", x, y);

    for (i = y, num1 = num2 = 0; i >= x; i--) {
        if (primeCheck(2, i) == 0) {
            if (num2 == 0) {
                num2 = i;
            } else {
                num1 = i;
                break;
            }
        }
    }
    printf("The two largest prime numbers from %d to %d are: %.0d %.0d\n",
            x, y, num1, num2);
    return 0;
}

// read user input return zero on failure
int usersInput(int *x, int *y) {
    printf("Enter the value of x: ");
    if (scanf("%d", x) != 1)
       return 0;
    printf("Enter the value of y: ");
    return scanf("%d", y) == 1;
}

void swapping(int *x, int *y) {
    int temp = *x;
    *x = *y;
    *y = temp;
}

int primeCheck(int i, int j) {
    if (j < 2) {
        return 1;  // negative, 0 or 1: not prime
    } else if (j / i < i) {
        return 0;  // all factors tested up to sqrt(j): j is prime
    } else if (j % i == 0) {
        return 1;  // composite, not prime
    } else {
        return primeCheck(i + 1, j);
    }
}

输出:

Enter the value of x: 10
Enter the value of y: 3
The value of x is 3, and the value of y is 10.
The two largest prime numbers from 3 to 10 are: 5 7

C++相关问答推荐

理解没有返回语句的递归C函数的行为

GCC不警告隐式指针到整数转换'

以c格式打印时间戳

如何在IF语句中正确使用0.0

将fget()与strcMP()一起使用不是正确的比较

Win32API Wizzard97 PropSheet_SetWizButton不工作

为什么sscanf不能正确地从这个字符串格式中提取所有数字?

我怎么才能用GCC编译一个c库,让它包含另一个库呢?

在C语言中,指针指向一个数组

可变宏不能编译

条件跳转或移动取决于未初始化值(S)/未初始化值由堆分配创建(Realloc)

如何将两个uint32_t值交织成一个uint64_t?

如何修复我的qsort()算法?它每次都给出不同的结果

从另一个宏函数调用C宏

在C中打印指针本身

令人困惑的返回和 scanf 问题相关

在列表中查找素数

C 程序不显示任何输出,但它接受 CS50 Lab1 的输入问题

在链表中插入一个值

C 预处理器中的标记分隔符列表