文档库 最新最全的文档下载
当前位置:文档库 › 第四章C++课后习题答案.

第四章C++课后习题答案.

第四章C++课后习题答案.
第四章C++课后习题答案.

第四章答案

一、选择题:

1A 2C 3D 4A 5C 6C 7B 8D 9B 10C 11D 12A

二、填空题:

(1).template double power(T x,int n) (2).T powerx=1;

(3).powerx= powerx*x; (4).return powerx

三、程序分析题:

(1).&a:0013FF7C, &b:0013FF78 a=3,b=4 &x:0013FF24, &y:0013FF28 &t:0013FF18 a=3,b=4

(2).由于T1 add(T1 x,T2 y)中用到自定义类型T1、T2,T1、T2没有定义,可将它们定义成参数化类型。

#include

using namespace std;

int add(int x,int y.int z=0){

cout<<”(int,int,int=0)\t”;

return x+y;

}

Template

T1 add(T1 x,T2 y)

{

cout<<”(”<

return x+y;

}

int main()

{

cout<

cout<

cout<

cout<

cout<

cout<(9,?A?)<

cout<

return 0;

}

程序结果如下:

(int,int,int=0) 17

(8,8) 17

(4,8) 17

(8,4) 17

(int,char) 74

(int,char) 74

(1,4) R

五编程题:

#include

using namespace std;

int f2c(int f)

{

return (5/9.0*(f-32))+0.5;

}

int c2f(int c)

{

return 9/5.0*c+32+0.5;

}

int main()

{

int c,f;

cout<<”输入华氏温度:”;

cin>>f;

cout<<”摄氏温度为:”<

cin>>c;

cout<<”华氏温度为:”<

}

(2)

#include

using namespace std;

double PI(double precision)

{

double tmp(0);

for(int i=1;1.0/(i*2-1)>precision;i++) tmp=tmp+1.0/(i*2-1)*(i*2?1:-1);

return tmp*4;

}

int main()

{

cout<

return 0;

}

(3)

#include

using namespace std;

int hcf(int x,int y)

{

int t,r;

while(x!=y)

if(x>y)

x=x-y;

else if(y>x)

y=y-x;

}

return 0;

}

int lcd(int x,int y)

{

return x*y/hcf(x,y);

}

int main()

{

int x,y;

cout<<”input two number:”;

cin>>x>>y;

cout<<”最大公约数:”<

}

(4)

#include

#include

using namespace std;

bool IsPrimeNumber(int number)

{

if(number<=1)

return false;

for(int i=2;i

if(number%i==0)

return true;

}

int main()

{

int x;

cout<<”input a number:”;

cin>>x;

if(IsPrimeNumber(x))

cout<<”a prime number!”<

else

cout<<”not a prime number!”<

return 0;

}

#include

using namespace std;

long fun(int m,int n)

{

if(m==n||n==0)

return 1;

else return m*fun(m-1,n)/(m-n);

}

int main()

{

int m,n;

cout<<”input m,n:”;

cin>>n>>m;

cout<<”c(m,n)=”<

return 0;

}

(6)

#include

using namespace std;

long fun(int n)

{

if(n==1)

return 1;

else

return 2*n-1+fun(n-1);

}

int main()

{

int n;

cout<<”input n:”;

cin>>n;

cout<<”1+…+”<<2*n-1<<”=”<

}

(7)

#include

using namespace std;

double power(double x, unsigned y)

{

if(y==0)

return 1;

else

return x*power(x,y-1);

}

int main()

{

double x;

unsigned y;

cout<<”input x,y:”;

cin>>x>>y;

cout<<”power(“<

}

C语言课后习题答案(最终)

第0章习题 1. 将下列十进制数分别转化为二进制数、八进制数和十六进制数: (1)128 (2)511 (3)1024 (4)65535 (5)1048575 答: (1)10000000、200、80 (2)111111111、777、1FF (3)10000000000、2000、400 (4)1111111111111111、177777、FFFF (5)11111111111111111111、3777777、FFFFF 2. 将下列二进制数转化为十进制数和十六进制数: (1)1100110101B (2)101101.1011B 答: (1)821、335 (2)45.6875、2D.B 3. 写出下列数的原码、反码、补码:15、-20、-27/32 答: (1)00001111、00000000、00001111 (2)10010100、11101011、11101100 (3)1.1101100、1.0010011、1.0010100 4. 16位无符号定点整数的数值表示范围为多少?8位补码的表示范围是多少?16位补码的表示范围是多少? 答: 0~65535、-128~127、-32768~32767 5.1968年Dijkstra提出结构化程序设计的思想的原因是什么?简要回答结构化程序设计的经典定义。 答: 结构化程序设计概念的提出主要是源于程序结构的层次性与模块化使得构造出来的软件具有良好的可理解性和可维护性,随着软件规模的扩大与复杂性的提高,程序的可维护性成为程序设计者们关注的重要问题之一。 如果一个程序的代码块仅仅通过顺序、选择和循环这3种基本控制结构进行连接,并且每个代码块只有一个入口和一个出口,则称这个程序是结构化的。 6.C程序在内存中存储在哪儿?计算机的内存空间是如何分区的?分区存放不同类型的数据的目的是什么? 答:

最新c语言课后习题答案汇总

c语言课后习题答案

第二章习题 2.什么叫做结构化算法?为什么要提倡结构化算法? 答:结构化算法是由一些基本结构顺序组成的。在基本结构之间不存在向前或向后的跳转,流程的转移只存在于一个基本的结构范围内。一个非结构化的算法可以用一个等价的结构化算法代替,其功能不变。 跟结构化算法比较起来,非结构化算法有以下缺点: 流程不受限制的随意转来转去,使流程图豪无规律使人在阅读的时候难以理解算法的逻辑.难以阅读,也难以修改,从而使算法的可靠性和可维护性难以保证。 4. 第三章习题 1.#include #include void main() { unsigned int n;

float p,p1,r=0.09; scanf("%u",&n); p=pow(1+r,n); p1=(p-1)*100; printf("%5.2f%%\n",p1); } 运行结果:输入,回车,见结果: 2.#include #include int main() { int bj=1000; float r1,r2,r3,r5,r0,lx1,lx2,lx3,lx4,lx5; r1=0.0414; r2=0.0468; r3=0.0540; r5=0.0585; r0=0.0072; lx1=bj*r5; lx2=bj*(1+r2)*r3; lx3=bj*(1+r3)*r2; lx4=bj*pow(1+r1,5); lx5=bj*r0*5; printf("lx1=%f lx2=%f lx3=%f lx4=%f lx=5%f\n",lx1,lx2,lx3,lx4,lx5); return 0; } 运行结果: 3.#include #include int main() { long d,p; d=300000; p=6000; double m,r=0.01; m=log(p/(p-d*r))/log(1+r); printf("%4.2f",m); return 0;

《C语言程序设计》课后习题答案(第四版)谭浩强

第1章程序设计和C语言1 1.1什么是计算机程序1 1.2什么是计算机语言1 1.3C语言的发展及其特点3 1.4最简单的C语言程序5 1.4.1最简单的C语言程序举例6 1.4.2C语言程序的结构10 1.5运行C程序的步骤与方法12 1.6程序设计的任务14 1-5 #include int main ( ) { printf ("**************************\n\n"); printf(" Very Good!\n\n"); printf ("**************************\n"); return 0; } 1-6#include int main() {int a,b,c,max; printf("please input a,b,c:\n"); scanf("%d,%d,%d",&a,&b,&c); max=a; if (max

2.5结构化程序设计方法34 习题36 第章最简单的C程序设计——顺序程序设计37 3.1顺序程序设计举例37 3.2数据的表现形式及其运算39 3.2.1常量和变量39 3.2.2数据类型42 3.2.3整型数据44 3.2.4字符型数据47 3.2.5浮点型数据49 3.2.6怎样确定常量的类型51 3.2.7运算符和表达式52 3.3C语句57 3.3.1C语句的作用和分类57 3.3.2最基本的语句——赋值语句59 3.4数据的输入输出65 3.4.1输入输出举例65 3.4.2有关数据输入输出的概念67 3.4.3用printf函数输出数据68 3.4.4用scanf函数输入数据75 3.4.5字符数据的输入输出78 习题82 3-1 #include #include int main() {float p,r,n; r=0.1; n=10; p=pow(1+r,n); printf("p=%f\n",p); return 0; } 3-2-1 #include #include int main() {float r5,r3,r2,r1,r0,p,p1,p2,p3,p4,p5; p=1000;

C语言课后作业答案

3-5-1正确 大写字母转化成小写或者小写变大写 #include void main() { char ch; printf("请输入一个字符:"); scanf("%c",&ch); if(ch>='A'&&ch<='Z'||ch>='a'&&ch<='z') { if(ch>='A'&&ch<='Z')ch=ch+32; else ch=ch-32; } else ch=ch; printf("%c\n",ch); } 3-5-2大写转化成小写或者小写变大写 #include void main() { char ch; printf("请输入一个字符:"); scanf("%c",&ch); ch=ch; { if(ch>='A'&&ch<='Z')ch=ch+32; else ch=ch-32; } printf("%c\n",ch); } 3-5-3大写转化成小写或者小写变大写 #include void main() { char ch; printf("请输入一个字符:"); scanf("%c",&ch); if(ch>='a'&&ch<='z') ch=ch-32; else if(ch>='A'&&ch<='Z') ch=ch+32; else ch=ch; printf("%c\n",ch); } 3-6-1正确分段函数

#include void main() { int x,y; printf("请输入x:"); scanf("%d",&x); if(x<=1) y=x; else { if(1 void main() { int x,y; printf("请输入x:"); scanf("%d",&x); if(x>=10) y=3*x-8; else if(x>1) y=2*x+1; else y=x; printf("x=%d,y=%d\n",x,y); } 3 -6 -3正确 #include void main() { int x,y; printf("请输入x:"); scanf("%d",&x); if(x<=1) y=x; else if(1=10) y=3*x-8; printf("x=%d,y=%d\n",x,y); } 计算器正确 #include void main() {

C语言课后答案

习题一 一、简答题 1.顺序结构、选择(分支)结构和循环结构。 2. (1)

(2)

3.编辑、编译、连接和运行 二、填空题 1. Ctrl+F9;Alt+F5;F2。 2. main(主) 3. main(主) 4. 任意 5. /* */ 不 6. ; 7.。 程序: include studio.h main{} /* this program prints the number of weeks in a year. /* ( int s s:=52; print(There are s weeks in a year"); 正确的为: #include main() /* this program prints the number of weeks in a year. */ { int s; s=52; printf("There are s weeks in a year"); }

习题二 一、选择题 1、C 2、B,D,F,G 3、C 4、A 5、C 6、B 二、填空题 1、声明;使用。 2、整型、浮点型(实型)、字符型。 3、hat_1,cat1,all, Dollar, piece_f, SIN, _ , FALSE. 4、'A'(字符),005(整型),3e0(整型),'\\'(字符),'\05'(字符), 1.2e+5(整型),0xf12(整型)。 5、(1)6+(4+5)*(4+5)/(2+3) (2)sin(a+b)*ain(a+b)/ (4*2)/(3*2)+2 三、读程题 1.若x为float型,其原值为5,a=2,b=4.7。写出下列表达式运算后x的值。 (1)x=(int)(b-a)%3*a/4-a (2)x=(x=b+1)+(int)(b)%10/2.0 (3)x+=x (4)x-=x (5)x*=x+x (6)x/=x+x (7)x+=x-=x*=x (8)x%=x (9)x=3*4,5*6 答案:(1)-1,(2)7.7,(3)10,(4)0,(5)50,(6)0.5,(7)0,(8)非法,(9)12。 2.写出下面程序的运行结果。 #include void main() { int a=2; printf("abcdefghijk\n"); printf("lmnop/n"); printf("I am a /n beginner of C !"); printf("I am a \n beginner of C !"); printf("%d + %d = %d",a,a,a); } 答案: abcdefghijk

C语言课后练习及答案

作业A: 一、单项选择题 1、如果要把常量327存入变量a中,a不能定义的类型是哪一个?() A)int B)char C)long D)float 2、若x 为unsigned int 型变量,则执行下列语句后x值为() x = 65535; printf(“%d\n”,x); A)65535 B)1 C)无定值D)-1 3、有以下程序 main() {int a=1,b=2,m=0,n=0,k;k=(n=b>a)||(m=a

C语言课后答案最新版本

第3章 三、编程题 1. 编写程序,输入一个非负数,输出以此数为半径的圆周长以及面积。 #include "stdio.h" #define PI 3.1415 void main() { float r,area,circumference; scanf("%f",&r); area=PI*r*r; circumference=2*r*PI; printf("area=%6.2f\ncircumference=%6.2f\n",area,circumference); } 2. 编写程序,输出下面结果,注意,双引号也要输出: “I'm a student!” #include void main() { printf("\"I\'m a student!\"\n"); } 3. 编写程序,输入一个小写字母,将其转换为大写字母输出。例如输入b ,则输出B 。提示:小写字母和 对应的大写字母的ASCII 码值相差32。 void main() { char ch; ch=getchar(); ch-=32; putchar(ch); //printf("%c",ch); } 4. 编写程序,输入一个华氏温度f ,输出其相应的摄氏温度c 。华氏温度和摄氏温度的转换公式为: )32f (95c -= #include void main() { float f,c; scanf(“%f ”,&f); c=5.0*(f-32)/9; printf(“华氏温度%5.2f 转换为摄氏温度为:%5.2f\n ”,f,c);

第4章 三、编程题 1.输入一个整数,判断这个整数是奇数还是偶数(提示:整数的奇偶性可以利用取余运算符%判定)。#include void main() { int a; scanf("%d",&a); if(a%2) printf("奇数\n"); else printf("偶数\n"); } 2.编写程序,输入一个24小时制的时间,转换为12小时制时间后进行输出。以13点15分为例,输入:13:15,则输出:下午1:15。 #include void main() { int hour,minute; scanf("%d:%d",&hour,&minute); if (hour>12) hour=hour-12; printf("%d:%d\n",hour,minute); } 3.输入年号,判断它是否是闰年(如果年号能被400整除,或能被4整除,而不能被100整除,则是闰年,否则不是)。 void main() { int year; scanf("%d",&year); if (year%400==0||(year%4==0&&year%100==0)) printf("%d是闰年\n",year); else printf("%d不是闰年\n",year); }

《C语言程序设计》课后习题答案(第四版)谭浩强

第1章程序设计和C语言1 什么是计算机程序1 什么是计算机语言1 语言的发展及其特点3 最简单的C语言程序5 最简单的C语言程序举例6 语言程序的结构10 运行C程序的步骤与方法12 程序设计的任务14 1-5 #include <> int main ( ) { printf ("**************************\n\n"); printf(" Very Good!\n\n"); printf ("**************************\n"); return 0; } 1-6#include <> int main() {int a,b,c,max; printf("please input a,b,c:\n");

scanf("%d,%d,%d",&a,&b,&c); max=a; if (max

数据类型42 整型数据44 字符型数据47 浮点型数据49 怎样确定常量的类型51 运算符和表达式52 语句57 语句的作用和分类57 最基本的语句——赋值语句59 数据的输入输出65 输入输出举例65 有关数据输入输出的概念67 用printf函数输出数据68 用scanf函数输入数据75 字符数据的输入输出78 习题82 3-1 #include <> #include <> int main() {float p,r,n; r=; n=10; p=pow(1+r,n); printf("p=%f\n",p); return 0;

C语言程序设计课后答案

2.2 #include main() { float x=2.5,y=2.5,z=2.5; printf("x=%f\n",x); printf("y=%f\n",y); printf("z=%f\n",z); } 3.1(1) #include main() { int a=12,b=3; float x=18.5,y=4.6; printf("%d\n",(float)(a*b)/2); printf("%d\n",(int)x%(int)y); } 3.2 #include main() { int x,b0,b1,b2,s; printf("Inputx:"); scanf("%d",&x); b2=x/100; b1=(x-b2*100)/10; b0=x%10; s=b0*100+b1*10+b2; printf("s=%d\n",s); } 3.3 #include #include main() { float rate=0.0225; float n,capital,deposit; printf("Input n,capital:"); scanf("%f,%f",&n,&capital); deposit=capital*pow(1+rate,n); printf("deposit=%f\n",deposit); } 3.4 #include #include main() { float a,b,c;

double x,y; printf("Inputa,b,c:"); scanf("%f,%f,%f",&a,&b,&c); x=(-b+sqrt(b*b-4*a*c))/(2*a); y=(-b-sqrt(b*b-4*a*c))/(2*a); printf("x=%f,y=%f\n",x,y); } 4.1(1) #include main() { char c1='a',c2='b',c3='c'; printf("a%cb%cc%c\n",c1,c2,c3); } 4.1(2) #include main() { int a=12,b=15; printf("a=%d%%,b=%d%%\n",a,b); } 4.1(3) #include main() { int a,b; scanf("%2d%*2s%2d",&a,&b); printf("%d,%d\n",a,b); } 4.2 #include main() { long a,b; float x,y; scanf("%d,%d\n",&a,&b); scanf("%f,%f\n",&x,&y); printf("a=%d,b=%d\n",a,b); printf("x=%f,b=%f\n",x,y); } 5.1 #include main() { float a; printf("Innputa:"); scanf("%f",&a); if(a>=0) {

C语言习题及解答

C 语言习题及解答 1、输入一个华氏温度,要求输出摄氏温度。公式 为#include void main( ) {float C,F; printf("Input F:"); scanf("%f",&F); C=5.0/9*(F-32); printf("C=%.2f\n",C);} 2、编写程序,从键盘输入一个大写字母,将它转换为对应的小写字母后输出。(提示:同一个字母的大写比小写小32) #include void main( ) { char ch; printf("Input ch:"); scanf("%c",&ch); ch=ch+32; printf(“ch=%c\n",ch);} 3、编写程序,输入梯形的上底、下底和高,计算 并输出梯形的面积。 #include void main( ) { float a,b,h,area; printf("Input a,b,h: "); scanf("%f%f%f", &a,&b,&h); area=(a+b)*h/2; printf("area=%.2f\n", area); } 4、编写程序,输入圆半径r ,求圆周长、圆面积、圆球表面积、圆球体积。 #include #define PI 3.1415926 void main( ) {float r,L,s1,s2,V; printf("Input r:"); scanf("%f", &r); L=2*PI*r; s1=PI*r*r; s2=4*PI*r*r; V=4.0/3*PI*r*r*r; printf("L=%.2f, s1=%.2f, s2=%.2f, V=%.2f\n", L,s1,s2,V); } 5、有三个电阻r1、r2、r3并联,编写程序计算并输出并联后的电阻r 。已知电阻并联公式为: #include void main( ) { float r,r1,r2,r3; printf("Input r1,r2,r3: "); scanf("%f%f%f", &r1,&r2,&r3); r=1/(1/r1+1/r2+1/r3); printf("r=%.2f\n", r); } 6、由键盘输入一个10-99之间的整数,将该数分解,分别输出其个位数字和十位数字。例如,输入85,输出:5,8。 提示:用算术运算中的整除和取余运算实现。 #include void main( ) {int x, a, b; scanf("%d", &x); a=x%10; b=x/10; printf(“a=%d, b=%d\n", a, b);} 7、编写程序,输入三角形的三条边,计算并输出三角形的面积。(注意输入的三条边必须要能构成一个三角形)求三角形的面积公式为 其中s=(a+b+c)/2。 #include #include void main( ) scanf("%f%f%f", &a,&b,&c); s= (a+b+c)/2; area=sqrt(s*(s-a)*(s-b)*(s-c)); printf("area=%.2f\n", area); 8、周期为T 秒的人造卫星离地面 的平均高度H 的计算公式为: 其中:M=6×1024kg 是地球质量,R=6.371×106m 是地球半径。 编写程序,输入人造卫星的周期T ,计算并输出人造卫星离地面的高度H 。 算法提示:求xy 结果的数学函数是pow(x, y) ()3295 -= F C 3 121111r r r r ++=()()() c s b s a s s area ---=R MT H -?=-32 2 1141067.6π

C语言课后习题答案

习题一 一、填空题 1.主函数/main、主函数/main、主函数/main 2.声明部分、执行部分 3.源程序 4.由一个或多个函数 5.最前面 6./*和*/ 二、选择题 1.C 2.B 3.A 4.C 5.C 6.C 7.A 8.B 9.B 10.B 习题二一、填空题 1.scanf和printf函数 2.标识符 3.字母、数字、下划线、字母、下划线 4.十进制、八进制、十六进制 5.单撇号 6.32 7.18,14,a 8.123、45.0 9.67,G 10.10300 11.25 21 37 12.n1=%d\nn2=%d\n 二、选择题 1.A 2.D 3.C 4.C 5.C 6.B 7.A 8.D 9.A 10.D 11.D 12.C 13.A 14.C 一、填空题 1.0,非0 2.else 总是与它前面最近的、等待与else配对的if配对3.将下列条件写成C语言的逻辑表达式:

(1)x<0&&y!=0 (2)x%3==0&&x%7==0 (3)a<0||b<0||c<0 (4)x>0 && x<=10 (5)ch>=’a’ && ch<=’z’ || ch>=’A’ && ch<=’Z’ 4. 2 1 5.5 4 6. -4 二、选择题 1.B 2.C 3.B 4.A 5.B 6.C 7.D 8.D 9.D 10.A 11.C 12.B 习题四 一、填空题 1.10 2.36 3.3 4.死、0 5.6 6.2*i – 1、printf("\n") 7.100 – i*5 –j*2、k>=0 8.4321 9.2.400000 10.*#*#*#$ 二、选择题 1.C 2.A 3.C 4.都不对(8)5.B 6.C 7.C 8.C 9.C 10.D 11.A 12.B 13.D 14.B 15.C 16.D 17.D 18.C 19.B 20.A

C语言课后习题参考答案(前5章)

《C语言程序设计基础》书后习题参考答案 (无编程题答案,红色为书上错误) 第二章习题 一、选择题。 1、在C语言中,要求参加运算的数必须是整数的运算符是() A)% B)/ C)!D)* 2、若已定义x和y为double类型,则表达式“x=1.0,y=x+3/2”的值是() A)1 B)2 C)2.0 D) 2.5 3、若变量已正确定义并赋值,符合C语言语法的表达式是() A)a=a+7; B) a=7+b+c,a++ C) int (12.3/4 ) D) a=a+7=c+b 4、若有定义:int x=3,y=2;float a=2.5,b=3.5;则表达式(x+y)%2+(int)a/(int)b 的值为() A)1.0 B)1 C) 2.0 D)2 5、若x和n均是int型变量,且x的初值为12,n的初值为5 ,则执行表达式x%=(n%=2) 后x 的值为() A) 0 B)1 C) 2 D)3 6、假设所有变量均为整型,则表达式(a=2,b=5,a++,b++,a+b)的值为() A)7 B)8 C)9 D)10 7、指出下列哪一个选项的程序是错误的() A)#include B) #include void main( ) void main( ) {int x,y,z; {int x,y,z; x=0;y=x+1; x=0,y=x+1; z=x+y; z=x+y; } } C) #include D) #include void main( ) void main( ) {int x,y,z; {int x,y,z; x=0;y=x+1, x=0,y=x+1; z=x+y; z=x+y, } } 8、若a为整型变量,则以下语句( ) a=-2L; printf(“%d\n”,a); A)赋值不合法B)输出值为-2 C)输出为不确定值D)输出值为2 9、若变量a,i已经正确定义,且i已正确赋值,则合法的语句是() A)i=int(a) B) ++i; C)a=a++=5; D) a=int(i); 10、若执行以下程序段后,c3的值为() int c1=1,c2=2,c3; c3=1.0/c2*c1; A)0 B)0.5 C)1 D)2

C语言课后答案

第3章 三、编程题 1. 编写程序,输入一个非负数,输出以此数为半径的圆周长以及面积。 #include "" #define PI void main() { float r,area,circumference; scanf("%f",&r); area=PI*r*r; circumference=2*r*PI; printf("area=%6.2f\ncircumference=%6.2f\n",area,circumference); } 2. 编写程序,输出下面结果,注意,双引号也要输出: “I'm a student!” #include <> void main() { printf("\"I\'m a student!\"\n"); } 3. 编写程序,输入一个小写字母,将其转换为大写字母输出。例如输入b ,则输出B 。提示:小写字母和 对应的大写字母的ASCII 码值相差32。 void main() { char ch; ch=getchar(); ch-=32; putchar(ch); )32f (95c -= 5.2f5.2f5.2f #include <> #define n 10 void main() { int a[n],i,count1=0,count2=0,sum=0;float ave; for (i=0;i80) count1+=1; else

if(a[i]<60) count2+=1; } ave=sum/n; printf("%d个优秀, %d个不及格,平均分:%f。",count1,count2,ave); } 2. #include <> #define n 10 void main() { int a[n],i,j,k,max,min; for (i=0;ia[i]) {min=a[i]; k=i; } } } printf("最大值%d在第%d位,最小值%d在第%d位。",max,j+1,min,k+1); } 3. #include <> #include <> void main() { char s[20],ch; int i,n; printf("请输入字符串:"); gets(s); n=strlen(s); for(i=0;i

C语言课后答案

第3章 三、编程题 1. 编写程序,输入一个非负数,输出以此数为半径的圆周长以及面积。 #include "stdio.h" #define PI 3.1415 void main() { float r,area,circumference; scanf("%f",&r); area=PI*r*r; circumference=2*r*PI; printf("area=%6.2f\ncircumference=%6.2f\n",area,circumference); } 2. 编写程序,输出下面结果,注意,双引号也要输出: “I'm a student!” #include void main() { printf("\"I\'m a student!\"\n"); } 3. 编写程序,输入一个小写字母,将其转换为大写字母输出。例如输入b ,则输出B 。提示:小写字母和 对应的大写字母的ASCII 码值相差32。 void main() { char ch; ch=getchar(); ch-=32; putchar(ch); //printf("%c",ch); } 4. 编写程序,输入一个华氏温度f ,输出其相应的摄氏温度c 。华氏温度和摄氏温度的转换公式为: )32f (95c -= #include void main() { float f,c; scanf(“%f ”,&f); c=5.0*(f-32)/9; printf(“华氏温度%5.2f 转换为摄氏温度为:%5.2f\n ”,f,c);

第4章 三、编程题 1.输入一个整数,判断这个整数是奇数还是偶数(提示:整数的奇偶性可以利用取余运算符%判定)。#include void main() { int a; scanf("%d",&a); if(a%2) printf("奇数\n"); else printf("偶数\n"); } 2.编写程序,输入一个24小时制的时间,转换为12小时制时间后进行输出。以13点15分为例,输入:13:15,则输出:下午1:15。 #include void main() { int hour,minute; scanf("%d:%d",&hour,&minute); if (hour>12) hour=hour-12; printf("%d:%d\n",hour,minute); } 3.输入年号,判断它是否是闰年(如果年号能被400整除,或能被4整除,而不能被100整除,则是闰年,否则不是)。 void main() { int year; scanf("%d",&year); if (year%400==0||(year%4==0&&year%100==0)) printf("%d是闰年\n",year); else printf("%d不是闰年\n",year); }

数据结构 c语言版 课后习题答案完整版

第1章绪论 5.选择题:CCBDCA 6.试分析下面各程序段的时间复杂度。 (1)O(1) (2)O(m*n) (3)O(n2) (4)O(log3n) (5)因为x++共执行了n-1+n-2+……+1= n(n-1)/2,所以执行时间为O(n2) (6)O(n) 第2章线性表 1.选择题 babadbcabdcddac 2.算法设计题 (6)设计一个算法,通过一趟遍历在单链表中确定值最大的结点。 ElemType Max (LinkList L ){ if(L->next==NULL) return NULL; pmax=L->next; //假定第一个结点中数据具有最大值 p=L->next->next; while(p != NULL ){//如果下一个结点存在 if(p->data > pmax->data) pmax=p; p=p->next; } return pmax->data; (7)设计一个算法,通过遍历一趟,将链表中所有结点的链接方向逆转,仍利用原表的存储空间。 void inverse(LinkList &L) { // 逆置带头结点的单链表 L p=L->next; L->next=NULL; while ( p) { q=p->next; // q指向*p的后继 p->next=L->next; L->next=p; // *p插入在头结点之后 p = q; } } (10)已知长度为n的线性表A采用顺序存储结构,请写一时间复杂度为O(n)、空间复杂度为O(1)的算法,该算法删除线性表中所有值为item的数据元素。

[题目分析] 在顺序存储的线性表上删除元素,通常要涉及到一系列元素的移动(删第i个元素,第i+1至第n个元素要依次前移)。本题要求删除线性表中所有值为item的数据元素,并未要求元素间的相对位置不变。因此可以考虑设头尾两个指针(i=1,j=n),从两端向中间移动,凡遇到值item 的数据元素时,直接将右端元素左移至值为item的数据元素位置。 void Delete(ElemType A[ ],int n) ∥A是有n个元素的一维数组,本算法删除A中所有值为item的元素。 {i=1;j=n;∥设置数组低、高端指针(下标)。 while(i

C语言程序设计教程课后习题答案

第1章 1-3 CAB 4 .c .obj .exe 5 /* */ 6 ; 7 算法 8 ①中级语言:C语言具有高级语言的先进思想又能直接对存储器进行操作,能进行位运算,能实现汇编语言的大部分功能,生成目标代码质量高,程序执行效率高。 ②结构化语言:C语言用函数作为程序模块,以实现程序的模块化,语言简洁、紧凑,具有结构化的特点。 ③可移植性好:C语言不包含依赖硬件的输入输出机制,使C语言本身不依赖于硬件系统,可移植性好。 9 #include<> main( ) { ; } 10 #include “” main() { printf(“This is my first C Program!”); } 第2章 1.yes 2.-3 3.2,1 4.1)a!=b||a<=c 2)x>=4||x<=-4 5.x>20&&x<30||x<-100 6.#include <> main() { int x; printf(“please input an integar:”); scanf("%d",&x); if(x%5==0&&x%7==0) printf("yes\n"); else printf("no\n"); } 7. #include <> main() { int year,month; printf("please input the year and month:"); scanf("%d%d",&year,&month);

switch(month) { case 1: case 3: case 5: case 7: case 8: case 10: case 12:printf("this month have 31 days."); break; case 4: case 6: case 9: case 11:printf("this month have 30 days."); break; case 2:if(year%4==0&&year%100!=0||year%400==0) { printf("this month have 29 days."); break; } else { printf("this month have 28 days."); break; } } } 8. #include <> main() { float money; int year; printf("\nplease input the money and the year:"); scanf("%f%d",&money,&year); if(year==1) money+=money**12*year; if(year==2) money+=money**12*year; if(year==3||year==4) money+=money**12*year; if(year>=5&&year<=7) money+=money**12*year; if(year>=8) money+=money**12*year; printf("the money is:%f",money); } 第3章

相关文档