#include <stdio.h>
int f(int n);
int main()
{
int bai, shi, ge, n, num;
printf("请选择功能: 1、100 - 999的水仙花数,2、2 - n之间所有的素数,3、输出斐波那契数列的第n项\n");
scanf("%d", &num);
switch (num)
{
case 1:
{
for (n = 100;n <= 999;n++) /*整数的取值范围*/
{
bai = n / 100;
shi = (n - bai * 100) / 10;
ge = n % 10;
if (n == bai * bai * bai + shi * shi * shi + ge * ge * ge) /*各位上的立方和是否与原数n相等*/
printf("%d ", n);
}
printf("\n");
break;
}
case 2:
{
int i, j, k = 0;
printf("请输入区间的上限:");
scanf("%d", &n);
for (i = 2;i <= n;i++)
{
for (j = 2;j < n / 2;j++)
{
if (i % j == 0)
break;
}
if (i == j)
{
printf("%d ", i);
k++;
}
}
printf("\n");
break;
}
case 3:
{
printf("求斐波拉契第几项?");
scanf("%d", &n);
printf("%d\n", f(n));
}
}
}
int f(int n)
{
int sum = 0;;
if (n >= 3)
{
sum = f(n - 1) + f(n - 2);
}
else
{
sum = 1;
}
return sum;
}
|