第4 章习题参考答案
1.单项选择题
(1) D (2 ) D (3 ) D (4 ) B (5 ) D
(6 ) B (7 ) A (8) D (9 ) C (10) D
(11) B (12) C (13) A (14) B
2 .填空题
(1)2
(2 )嵌套 , 递归
(3 )全局变量 , 局部变量 , 静态变量 , 动态变量
(4 )auto , static , register , extern
(5 )static
(6 )编译 ,运行
3 .阅读程序,指出结果
(1)15
(2 )5
(3 )5,4,3
(4 )i=5
i=2
i=2
i=4
i=2
(5 )求水仙花数
(6 )-125=-5*5*5
(7 )30
(8 )0 10
1 11
2 12
4 .程序填空
(1)float fun(float , float) , x+y,x-y , z+y,z-y
(2 )x , x*x+1
(3 )s=0 , a=a+b
5 .编程题
(1).
#include
unsigned int fun(unsigned int);
int main()
{
unsigned int s;
scanf("%u",&s);
printf("%u\n",fun(s));
return 0;
}
unsigned int fun(unsigned int s)
1
{
unsigned int p=0;
while(s!=0)
{
p=p+s%10;
s=s/10;
}
return p;
}
(2).
#include
#include
#include
void f1(float,float,float,float);
void f2(float,float,float,float);
void f3(float,float,float,float);
int main()
{
float a,b,c,d;
scanf("%f %f %f",&a,&b,&c);
if(a==0)
{
printf("不是一元二次方程\n");
exit(0);
}
d=b*b-4*a*c;
if(d>0)
f1(a,b,c,d);
else if(d==0)
f2(a,b,c,d);
else
f3(a,b,c,d);
return 0;
}
void f1(float a,float b,float c,float d)
{
float x1,x2;
{
x1=(-b+sqrt(d))/(2*a);
x2=(-b-sqrt(d))/(2*a);
printf("%.2f ,%.2f\n",x1,x2);
}
}
void f2(float a,float b,float c,float d)
2
{
float x1,x2;
{
x1=-b/(2*a);
x2=-b/(2*a);
printf("%.2f ,%.2f\n",x1,x2);
}
}
void f3(float a,float b,float c,float d)
{
float x1,x2;
{
x1=-b/(2*a);