费舍尔数是一个整数,其因子(包括自身)乘积等于该数字的立方。例如,12
是一个Fisher数,因为12 3 = 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)
目标:
这是我的代码:
#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
,这是错误的!
答案 0 :(得分:2)
您应在此处乘以j
:
if(i%j == 0) {
product = product * i;
}
如果您使用比i
和j
更有意义的变量名,这将更加明显。
您还需要在内循环之前重置product
变量(为什么仍然是全局变量?您不知道它们是邪恶的吗?)
cube = i * i * i;
product = 1;
for(j=1;j<=i;j++) { ...