简答题 C语言
题号 #2257388

下列给定程序中,函数proc()的功能是:用递归算法计算斐波拉契级数列中第n项的值。从第一项起,斐波拉契级数序列为1,1,2,3,5,8,13,21,…例如,若给n输入8,该项的斐波拉契级数值为21。
请修改程序中的错误,使它能得出正确的结果。
注意:不要改动main()函数,不得增行或删行,也不得更改程序的结构。
试题程序:
#include<stdio.h>
long proc(int g)
{
//****found****
switch(g);
{
case 0:return 0;
//****found****
case 1;case 2:return 1;
}
return(proc(g-1)+proc(g一2));
}
void main()
{
long fib;int n;
printf("Input n:");scanf("%d",&n);
printf("n=%d\n",n);
fib=proc(n):
printf("fib=%d\n\n",fib);
}

进入在线模考

参考答案

暂无

答案解析

(1)错误:swilch(g);
正确:switch(g)
(2)错误:case 1;C≠case 2:return 1;
正确:case 1:return 1;case2:return 1;
【解析】由C语言的语法规则可知,switch语句后面是没有分号的,因此,“switch(g);”要把后面的分号去掉;在C 语言中,case语句后面是冒号而不是分号,因此,“case 1;case 2: return 1;”应改为“case 1: return 1;case2:return 1;”。

你可能感兴趣的试题

1

学生的记录由学号和成绩组成,M名学生的数据已在主函数中放人结构体数组stu中,请编写函数proc(),它的功能是:把指定分数范围之外的学生数据放在b所指的数组中,分数范围之外的学生人数由函数值返回。
例如,输入的分数是70和79,则应当把分数低于70和高于79的学生数据输出,不包含70分和79分的学生数据。主函数中把70放在low中,把79放在heigh中。
注意:部分源程序给出如下。
请勿改动main()函数和其他函数中的任何内容,仅在函数proc()的花括号中填人所编写的若干语句。
试题程序:
#include<stdio.h>
#define M 16
typedef struct
{
char num[10];
int s:
}
STREC;
int proc(STREC*a,STREC*b,int 1,int h)
{

}
void main()
{
STREC stu[M]={{"GA005",55),{"GA003",96},
{"GA002",80),{"GA004",68),
{"GA001",88},{"GA007",74},
{"GA008",85),{"GA006",89},
{"GA015",90),{"GA013”,53),
{"GA012",66},{"GA014”,82),
{"GA011",73},{"GA017",69),
{"GA018",64),{"GA016",86}};
STREC h[M];
int i,n,low,heigh,t;
printf("Enter 2 integer number low&
heigh:");
scanf("%d%d",&low,&heigh);
if(heigh<low) ’
{
t=heigh:
heigh=low;
low=t;
}
n=proc(stu,h,low,heigh);
printf("The student’S data out
%d--%d:\n",low,heigh);
for(i=0;i<n;i++)
printf("%S%4d\n",h[i].num,h[i].s);
//输出指定分数范围内的学生记录
printf("\n");
}

找试题 · 找答案 · 找资料 在线模考