Closed. This question is off-topic。它当前不接受答案。
                            
                        
                    
                
                            
                                
                
                        
                            
                        
                    
                        
                            想改善这个问题吗? Update the question,所以它是on-topic,用于堆栈溢出。
                        
                        2年前关闭。
                                                                                            
                
        
计划详情:费舍尔号码

Fisher数是一个整数,其因子(包括自身)乘积等于该数字的立方。例如,12是Fisher数,因为123 = 2 x 3 x 4 x 6 x 12。

例:

Input: 12
Output: true (12<sup>3</sup> = 2 x 3 x 4 x 6 x 12)

Input: 8
Output: false (8<sup>3</sup> != 2 x 4 x 8)


目标:


编写程序以检查用户输入是否为Fisher数。
打印给定范围内的所有费舍尔数




这是我的代码:

#include <stdio.h>
#include <conio.h>
int ch, product = 1, cube, num, i, j, min, max;

// Check Function

void check() {
    int i;
    printf("Enter the Number to check whether It is Fisher or Not\n");
    scanf("%d", &num);
    cube = num * num * num;
    for(i = 2; i <= num; i++) {
        if(num % i == 0) {
            product = product * i;
        }
    }
    if(cube == product) {
        printf("It is a Fisher Number!\n");
    }
    else {
        printf("It is not a Fisher Number\n");
    }
}

// RANGE FUNCTION

void range() {
    printf("Enter the Minimum and Maximum value of Range\n");
    scanf("%d%d", &min, &max);
    for(i = min; i <= max; i++) {
        cube = i * i * i;
        for(j = 1; j <= i; j++) {
            if(i % j == 0) {
                product = product * i;
            }
        }
        if(cube == product) {
            printf("%d\n", i);
        }
    }
}

void main() {
    clrscr();
    printf("Enter Your Choice \n");
    printf("1 - Check Fisher Number \n");
    printf("2 - Display Fisher Numbers in a Range \n");
    scanf("%d", &ch);
    switch(ch) {
        case 1: check();
                break;
        case 2: range();
                break;
        default: printf("Enter Valid Choice \n");
    }
    getch();
}


Output picture和文本:

Enter Your Choice
1 - Check Fisher Number
2 - Display Fisher Numbers in a Range
2
Enter the Minimum and Maximum value of Range
1 40
1


我没有正确输出范围。例如,如果范围设置为1到15,则仅显示1,这是错误的!

最佳答案

您应在此处乘以j

if(i%j == 0) {
    product = product * i;
}


如果使用比ij更有意义的变量名,这将更加明显。

您还需要在内循环之前重置product变量(为什么仍然是全局变量?您不知道它们是邪恶的吗?)

cube = i * i * i;
product = 1;
for(j=1;j<=i;j++) { ...

10-04 11:47