C程序设计教程课后习题答案吉顺如版.docx
《C程序设计教程课后习题答案吉顺如版.docx》由会员分享,可在线阅读,更多相关《C程序设计教程课后习题答案吉顺如版.docx(49页珍藏版)》请在冰豆网上搜索。
C程序设计教程课后习题答案吉顺如版
第1章习题
一、选择题
1、A 2、D 3、C 4、D 5、B 6、C 7、C 8、B 9、B 10、A
二、填空题
1、一,函数体
2、main(主),main(主)
3、编译,目标
4、.c,.obj,.exe
5、缩进,无影响,/* */
三、程序分析题
1、
*****************
Very good!
*****************
2、
a=12,b=5
a=17,b=22
3、
r= 5.50,s=95.033098
四、编程题
1.用printf()函数在屏幕上输出自己的班级、学号、。
#include"stdio.h"
void main()
{
printf("班级:
BX1109\n");
printf("学号:
1\n");
printf(":
王孟荣\n");
}
2.从键盘输入两个整数,计算它们的和、差、积、商,并在屏幕上输出结果。
#include
void main()
{
int x,y,a,b,c;
float d;
printf("Please Input Two Integers:
\n");
scanf("%d%d",&x,&y);
a=x+y;
b=x-y;
c=x*y;
d=1.0*x/y;
printf("a=%d\nb=%d\nc=%d\nd=%f\n",a,b,c,d);
}
第2章习题
一、选择题
1~5 BCDCD
6~10 DBBCA
二、填空题
1.单精度浮点型、双精度浮点型、字符型
2.0
3.x%10*10+x/10
4.5.5
5.5.7 20
6.sin(sqrt(x*x))/(a*b)
7. ,
8.1111011 173 7B
9.3 3
10.3
三、程序分析题
1. 7,8,10
2. 1,3
3. 5 F
4. (int)i=12,
12.50
5. 4
61
57
9
112
四、编程题
1.编写程序,求表达式c=21%9-(float)a+4/b*b的值,假设表达式中a和b的值分别由键盘输入。
#include
void main( )
{
int a,b;
float c;
printf("Please input data a,b:
");
scanf("%d%d",&a,&b);
c=21%9-(float)a+4/b*b;
printf("c=%5.2f\n", c);
}
2. 编写程序,从键盘输入两个整数存入变量a和b中,求a2-b2的值并输出。
#include
void main( )
{
int a,b,c;
printf("Please input data a,b:
");
scanf("%d%d",&a,&b);
c=a*a-b*b;
printf("c=%d\n", c);
}
第3章习题
一、选择题
1
2
3
4
5
6
7
8
9
10
D
C
A、B
A
B
B
B、C
B
D
D
二、填空题
1. 有穷性 确定性 可行性
2. #
3. 从终端(键盘)输入一个字符
4. m d
5. 97,g
6. 123.456000, 123.46, 123
7. 格式控制符不正确(输入数据时不能控制列宽和小数位数)
8. 63,63,77,3f
9. %f%f &x,&y
10. ch ch
三、程序分析题
1. c=11
2. a=123 f=457
3. x=345
y=3.460000
4. a=1,b=3
a=3,b=1
5. (题目需修改为:
从键盘输入一个除a和z以外的小写字母)
程序的功能:
从键盘输入一个除a和z以外的小写字母,求出该字母对应的大写字母的前趋和后继字母,然后顺序输出此三个大写字母,每个字母所占列宽为3。
运行结果:
假如输入:
f '
输出:
E F G
四、编程题
1.编写程序,计算的值并输出(保留3位小数),其中a、b的值由键盘输入。
#include
#include
void main( )
{ float a, b,x;
scanf("%f%f", &a, &b);
x=(a*a+b*b)/sqrt(3*(a+b));
printf("x=%.3f\n",x);
}
2.编写程序,从键盘输入一个梯形的上底a、下底b和高h,输出梯形的面积s。
#include
#include
void main( )
{ float a, b,h,s;
scanf("%f,%f,%f", &a,&b,&h);
s=(a+b)*h/2;
printf("梯形面积s=%.2f\n",s);
}
3.编写程序,在屏幕上输入你的第一个字母,显示出该字母以及下面的信息:
Hello!
This is a c program.
My name is [第一个字母]
#include
void main( )
{
char name;
name=getchar();
putchar(name);
printf("\nHello!
\n");
printf("This is a c program.\n");
printf("My name is %c\n",name);
}
第4章习题
一、选择题
1
2
3
4
5
6
7
8
9
10
B
B
C
B
C
A C
B
A
C
C
二、填空题
1. !
&& ||
2. 1
3. a b
4. 1
5. 0
6. x%4= =0 && x%100!
=0
7. 5 6
8. A
9. x<0 || x>10 && x<50
10. -1
三、程序分析题
1. No
2. -2
3. 5
3
2
4. #&
5.
(1)2
(2)3
CC
a=2,b=2,c=4
四、编程题
1. 有一个函数如下:
x2-2 (x<5)
y= 3x+5 (5≤x<50)
x- (x≥50)
编写程序,输入x的值,计算相应的y值输出(保留3位小数)。
#include
#include
void main()
{ float x,y;
scanf("%f", &x);
if(x<5) y=x*x-2;
else if(x<50) y=3*x+5;
else y=x-sqrt(4*x-1);
printf("x=%f, y=%.3f\n", x,y);
}
2.编写一个程序,根据输入的三角形的三条边判断是否能组成三角形,如果可以则输出它的面积和三角形类型(等边、等腰、直角、一般三角形)。
#include
#include
void main( )
{ float a, b, c, s, area;
scanf("%f,%f,%f", &a,&b,&c);
if(a+b>c && b+c>a && a+c>b)
{ s=(a+b+c)/2;
area=sqrt(s*(s*(s-a)*(s-b)*(s-c)));
printf("area=%f\n",area);
if(a==b && b==c)
printf("等边三角形\n");
else if(a==b || a==c || b==c)
printf("等腰三角形\n");
else if((a*a + b*b== c*c)||(a*a + c*c== b*b)||(b*b + c*c == a*a))
printf("直角三角形\n");
else printf("一般三角形\n");
}
else printf("不能组成三角形\n");
}
3.设奖金税率r有如下的要求 (n代表奖金) :
0 n<1000
5% 1000≤n<2000
r= 8% 2000≤n<3000
10% 3000≤n<6000
15% 6000≤n
用switch多分支选择语句编写程序,输入奖金值,计算并输出相应的税率和实际应得奖金值。
#include
void main( )
{ float n, r, s;
int m;
printf("请输入奖金值:
");
scanf("%f", &n);
if(n>=6000) m=6;
else m=n/1000;
switch(m)
{ case 0:
r=0; break;
case 1:
r=0.05; break;
case 2:
r=0.08; break;
case 3:
case 4:
case 5:
r=0.1; break;
case 6:
r=0.15; break;
}
s=n-r*n;
printf("税率r=%.0f%%, 奖金值n=%.2f, 实际应得奖金值s=%.2f\n",r*100, n, s);
}
4. 从键盘输入任意4个数a、b、c、d,按照从大到小的顺序排列后重新输出。
#include
void main( )
{ int a, b, c, d,t;
scanf("%d,%d,%d,%d", &a,&b,&c,&d);
if(a
if(a if(a if(b if(b if(c printf("%d %d %d %d\n", a,b,c,d);
}
5. 给出一个不超过4位数的正整数,判断它是几位数,并按逆向输出各位数字。
例1234,输出为4321。
#include
void main( )
{ int num,i,j,k,m;
printf("输入一个少于4位的正整数:
");
scanf("%d",&num);
if(num>=0 && num<=9999)
{ if(num>1000 && num<=9999)
{ printf("是一个4位数\n");
m=num%10; /*求个位上的数字*/
k=num/10%10; /*求十位上的数字*/
j=num/100%10; /*求百位上的数字*/
i=num/1000; /*求千位上的数字*/
printf("逆序数为:
%d%d%d%d\n",m,k,j,i);
}
else if(num>=100)
{ printf("是一个3位数\n");
m=num%10; /*求个位上的数字*/
k=num/10%10; /*求十位上的数字*/
j=num/100; /*求百位上的数字*/
printf("逆序数为:
%d%d%d\n",m,k,j);
}
else if(num>=10)
{printf("是一个2位数\n");
m=num%10; /*求个位上的数字*/
k=num/10; /*求十位上的数字*/
printf("逆序数为:
%d%d\n",m,k);
}
else
{printf("是一个1位数\n");
printf("逆序数为:
%d\n",num);
}
}
else printf("是一个无效的数\n");
}
第5章习题
一、选择题
1
2
3
4
5
6
7
8
9
10
B
C
B
C
A
B
D
B
A
B
二 填空题
1. 循环 switch
2. 4,7
3. 6
4. # #
5. 0 2
6. 18
7. 1
8. sum=80
9. c=getchar()
n:
m
10. i %13= =0
break
三 程序分析题
1. output1:
21 6
output2:
5 6
output3:
5 7 7
2. n=7
3. Max=18
Min=3
4. 1*1=1
2*1=2 2*2=4
3*1=3 3*2=6 3*3=9
5. 0 1 2 3
1 2 3 0
2 3 0 1
3 0 1 2
四、编程题
1. 从键盘上输入若干字符,以按Enter键结束,统计其中字符A或a的个数。
#include "stdio.h"
main()
{ char ch;
int k=0;
while((ch=getchar())!
='\n')
{ if(ch=='A' || ch=='a')
k=k+1; }
printf("%d\n",k);
}
2. 利用=×…的前100项之积计算p的值。
#include
main()
{
double term, result = 1;/*累乘项初值应为1*/
int n;
for (n=2; n<=100; n = n + 2)
{
term=(double)( n * n)/((n-1)*(n+1));/*计算累乘项*/
result = result * term;
}
printf("result = %f\n", 2*result);
}
3. 用1元5角钱人民币兑换5分、2分和1分的硬币(每一种都要有)共100枚,问共有几种兑换方案?
每种方案各换多少枚?
#include
main()
{
int x,y,z,count=0;
for(x=1;x<=28;x++)
for(y=1;y<=73;y++)
{
z=100-x-y;
if(5*x+2*y+z==150)
{
count++;
printf("%d,%d,%d\n",x,y,z);
}
}
printf("count=%d\n",count);
}
4. 鸡兔同笼,共有98个头,386只脚,编程求鸡、兔各多少只。
#include
main()
{int x, y;
for (x=1;x<=97;x++)
{y=98-x;
if(2*x+4*y==386)
printf("鸡=%d,兔=%d",x,y);
}
}
5. 将一个正整数分解质因数。
例如:
输入90,打印出90=2*3*3*5。
#include
main()
{
int n,i;
printf("\nplease input a number:
\n");
scanf("%d",&n);
printf("%d=",n);
for(i=2;i<=n;i++)
while(n!
=i)
{
if(n%i==0)
{ printf("%d*",i);
n=n/i;
}
else break;
}
printf("%d",n);
6. 从键盘任意输入一个4位数x,编程计算x的每一位数字相加之和。
例如,输入x为1234,则由1234分离出其千位1、百位2、十位3、个位4,然后计算1+2+3+4=10,并输出10。
#include
#include
main()
{
int i1,i2,i3,i4,k,n;
printf("Inputdatais:
");
scanf("%d",&n);
k=fabs(n);/*取绝对值*/
i1=k/1000;/*分离出千位*/
i2=(k-i1*1000)/100;/*分离出百位*/
i3=(k-i1*1000-i2*100)/10; /*分离出十位*/
i4=k%10;/*分离出个位*/
printf("The sum of the total bit is %d\n",i1+i2+i3+i4);
}
7. 打印出所有"水仙花数",所谓"水仙花数"是指一个三位数,其各位数字立方和等于该数本身。
例如:
153是一个水仙花数,因为153=13+53+33。
#include
void main()
{
int i,j,k,n;
printf("parcissus numbers are:
\n");
for (n=100;n<1000;n++)
{
i=n/100;
j=n/10-i*10;
k=n%10;
if (n==i*i*i+j*j*j+k*k*k)
printf("%d\n",n);
}
}
8. 利用泰勒级数sin(x)≈计算sin(x) 的值。
要求最后一项的绝对值小于10-5,并统计出此时累加了多少项(x由键盘输入)。
#include
#include
main()
{
int n=1,count=1;
float x;
double sum,term; /*因为位数多,所以定义为双精度 */
printf("Input x:
");
scanf("%f", &x);
sum=x;
term=x; /*赋初值*/
do
{
term=-term*x*x/((n+1)*(n+2));
sum=sum+term; /*累加 */
n=n+2;
count++;
}while(fabs(term)>=1e-5);
printf("sin(x)=%.1f,count=%d\n",sum,count);
}
9.编写一个猜数游戏:
任意设置一个整数,请用户从键盘上输入数据猜想设置的数是什么,告诉用户是猜大了还是小了。
10次以猜对,用户获胜;否则,告诉用户设置的数据是什么。
#include
void main()
{int num=123,x,n;
printf("hint:
0 for(n=1;n<=10;n++)
{printf("guess:
");
scanf("%d",&x);
if(x==num) {printf("Win!
\n");break;}
if(x>num) printf("bigger!
\n");
if(x\n");
}
if(n==11) printf("Lost!
the number is %d\n",x);
}
10. 编程输出以下图案。
*
***
*****
*******
*****
***
*
#include
main()
{
int i,j;
for(i=1;i<=4;i++)
{
for (j=20-i;j>=1;j--)
printf("%c",' ');
for (j=0;j<2*i-1;j++)
printf("*");
printf("\n");
}
for (i=3;i>=1;i--)
{
for (j=20-i;j>=1;j--)
printf("%c",' ');
for (j=0;j<2*i-1;j++)
printf("*");
printf("\n");
}
}
第6章习题
一、选择题
1
2
3
4
5
6
7
8
9
10
D
A
D
B
D
C
B
D
B
D
二 填空题
1. 0
2. 14 m
3.10 7
4. 数据类型
5. strcpy(S2,S1) #include
6. 0 3
7. 7,6
8. sum=0
i==j
a[i][j]
9. 4
10. Tony
三 程序分析题
1. max=85
min=5
sum=180
aver=30.00
2. a=2
b=2
c=2
d=2
e=1
f=2
g=2
3. j=45
4. Ti_saCPorm
Tss_Pgm
5. 1 1