C语言课后答案最新版本
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、FFFFF2. 将下列二进制数转化为十进制数和十六进制数:(1)1100110101B (2)101101.1011B答:(1)821、335(2)45.6875、2D.B3. 写出下列数的原码、反码、补码:15、-20、-27/32答:(1)00001111、00000000、00001111(2)10010100、11101011、11101100(3)1.1101100、1.0010011、1.00101004. 16位无符号定点整数的数值表示范围为多少?8位补码的表示范围是多少?16位补码的表示范围是多少?答:0~65535、-128~127、-32768~327675.1968年Dijkstra提出结构化程序设计的思想的原因是什么?简要回答结构化程序设计的经典定义。
答:结构化程序设计概念的提出主要是源于程序结构的层次性与模块化使得构造出来的软件具有良好的可理解性和可维护性,随着软件规模的扩大与复杂性的提高,程序的可维护性成为程序设计者们关注的重要问题之一。
如果一个程序的代码块仅仅通过顺序、选择和循环这3种基本控制结构进行连接,并且每个代码块只有一个入口和一个出口,则称这个程序是结构化的。
6.C程序在内存中存储在哪儿?计算机的内存空间是如何分区的?分区存放不同类型的数据的目的是什么?答:C语言程序属于应用程序,程序代码本身存放在应用程序区,程序运行时处理的数据存放在应用程序数据区。
计算机的内存空间主要分为3个区:系统程序区、应用程序区和数据区,其中数据区又分为系统程序数据区和应用程序数据区两类。
最新c语言课后习题答案汇总

c语言课后习题答案第二章习题2.什么叫做结构化算法?为什么要提倡结构化算法?答:结构化算法是由一些基本结构顺序组成的。
在基本结构之间不存在向前或向后的跳转,流程的转移只存在于一个基本的结构范围内。
一个非结构化的算法可以用一个等价的结构化算法代替,其功能不变。
跟结构化算法比较起来,非结构化算法有以下缺点:流程不受限制的随意转来转去,使流程图豪无规律使人在阅读的时候难以理解算法的逻辑.难以阅读,也难以修改,从而使算法的可靠性和可维护性难以保证。
4.第三章习题1.#include<stdio.h>#include<math.h>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<stdio.h>#include<math.h>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<stdio.h>#include<math.h>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;}运行结果:4.#include<stdio.h>int main(){int c1,c2;c1=97;c2=98;printf("c1=%c,c2=%c\n",c1,c2);printf("c1=%d,c2=%d\n",c1,c2);return 0;}运行结果:①;②;③结果不变。
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.53、若变量已正确定义并赋值,符合C语言语法的表达式是()A)a=a+7; B) a=7+b+c,a++ C) int (12.3/4 ) D) a=a+7=c+b4、若有定义: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)25、若x和n均是int型变量,且x的初值为12,n的初值为5 ,则执行表达式x%=(n%=2) 后x 的值为()A) 0 B)1 C) 2 D)36、假设所有变量均为整型,则表达式(a=2,b=5,a++,b++,a+b)的值为()A)7 B)8 C)9 D)107、指出下列哪一个选项的程序是错误的()A)#include<stdio.h> B) #include<stdio.h>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<stdio.h> D) #include<stdio.h>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)输出值为29、若变量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)211、如下程序的运行结果是()#include<stdio.h>void main( ){ int y=3,x=3,z=1;printf(“%d%d\n”,(++x,y++),z+2);}A) 3 4 B) 4 2 C) 4 3 D) 3 312、以下程序的输入结果为()#include<stdio.h>void main( ){int i=4,a;a=i++;printf(“a=%d,i=%d’,a,i);}A)a=4,i=4 B)a=5,i=4 C)a=4,i=5 D)a=5,i=513、以下程序的输出结果是()#include <stdio.h>void main( ){ int a=5,b=4,c=6,d;printf(“%d\n”,d=a>b?(a>c?a:c):( b); //多了(}A) 5 B) 4 C) 6 D) 不确定14、在C语言中,如果下面的变量都是int类型,则输出的结果是()sum=pad=5;pad=sum++,pad++,++pad;printf(“%d\n”,pad);A)7 B)6 C)5 D)415、已知int i;float f;,正确的语句是()A)(int f)%i; B)int(f)%i; C) int(f%i); D)(int)f%i;16、已知int j,i=1;执行语句j=-i++;后,j的值是()A)1 B)2 C)-1 D)-217、已知各变量的类型说明如下:int k,a,b;unsigned long w=5;double x=1.42;则以下不符合C语言语法的表达式是()A)x%(-3) B)w+=-2 C)k=(a=2,b=3,a+b) D)a+=a-=a*(a=3)18、C语言中运算对象必须是整型的运算符是( )A) %= B) / C) = D)<=19、有以下程序main(){ int m=12,n=34;printf("%d%d",m++,++n);printf("%d%d\n",n++,++m);}程序运行后的输出结果是( )A)12353514 B)12353513 C)12343514 D)1234351320、下面程序的输出结果是()#include<stdio.h>main( ){int x=5,y=3;printf(“%d\n”,y=x/y); }A)0 B)1 C)3 D)不确定的值21、执行下列程序中的输出语句后,a的值是()#include<stdio.h>void main( ){int a;printf(“%d\n”,(a=3*5,a*4,a+5)); }A) 65 B)20 C)15 D)1022、执行下列程序时输入1234567,程序的运行结果为()#include<stdio.h>main( ){int x,y;scanf (“%2d%2ld”,&x,&y);printf(“%d\n”,x+y); }A)17 B)46 C)15 D)923 、已知char a;int b;float c;double d; 则表达式a*b+c-d结果类型为()A) double B)int C)float D) char二、填空题1、若i为int整型变量且赋值为6,则运算i++后的表达式的值是__6__,变量i的值是___7___。
C语言教材课后习题参考答案

习题参考答案习题 1一、选择题1、B2、C3、B4、D5、A6、B7、C二、填空题1、源程序文件 c2、obj3、可执行文件 exe4、机器语言汇编语言高级语言三、解答题略四、编程题1、main(){printf(“Hello! Welcome to China!”);}2、main(){int x;scanf(“%d”,&x);if(x>=20&&x<1000) printf(“x=%d”,x);else printf(“Input error!”);}习题 2一、选择题1、C2、A3、B4、D5、 A6、D7、B8、B9、B 10、D11、D 12、B 13、A 14、B 15、C二、填空题1、整型、实型、字符型2、用户标识符、关键字标识符3、存储单元、符号地址、内存地址4、十、十六、八5、double(双精度型)6、 87、5.5000008、a=-327682959、+0017,021,0x11三、写程序运行结果325732577.88, -345.12,7.8765,-345.12307.87654e+00, -3.5e+02a,97,141,611234,2322,4d2CHINESE, CHI四、scanf函数的使用a=3 b=78.5 71.82A a五、用scanf函数输入数据1020Aa1.5-3.75 123.45,67.8注意,其中123.45可以是任意实数,因为该值将被跳过,不用于赋值。
习题 3一、选择题1.C 2.B 3.D 4.D二、填空题基本概念题1.22.23.1阅读程序写出运行结果题4.1.005.1,0,16.9,11,9,10三、写出下面表达式运算后a的值,设原来a=12。
(1)24 (2)10 (3)60 (4)0 (5)0 (6)0习题4一、选择题1、B2、D3、B4、D5、A6、C二、填空题1、1,0,12、1,2,33、ch1>=′A′&&ch1<=′Z′ ch1=ch1-32;三、编程题2961、从键盘输入三个数,然后按照由小到大的顺序输出。
C语言课后全部答案

第一章C语言初步一、选择题1.C2.D3.D4.A5.D6.B7.B 8.B 9.C 10.A二、思考与应用题1.A.字符串常量B.字符串常量C.整型常量D.浮点型常量(double)2.A.long doubleB.long intC.floatD.字符型常量3.A.整型常量B.字符型常量C.字符串常量D.字符串常量4.FirstExample: 10,w is Yz is 5.125.int main(void){return 0;}6.#include<stdio.h>int main(void){print f("Hello World");return 0;}7.#include<stdio.h>int main(void){printf("We are to learn correct");printf("C language here");return 0;}/*main*/8.#include<stdio.h>int main(void){/*Local Definition*/int a;float b;char c;/*Statements*/printf("The end of the program."); return 0;}/*main*/9.#include<stdio.h>int main(void){/*Local Definition*/int a;float b;char c,d;/*Statements*/printf("The end of the program."); return 0;}/*main*/10.#include<stdio.h>int main(void){/*Local Definition*/int a;char b,c,d;float e,f;/*Statements*/printf("The end of the program."); return 0;}/*main*/11.A.char option;B.int sum=0;C.float produce=1;12.A.short code;B.float salesTax=.0825;C.double sum=0;13.#include<stdio.h>int main(void){float cost=172.53;printf("The sales total is: $ %6.2f\n",cost);printf("^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^");return 0;}14.#include<stdio.h>int main(void){printf("******\n");printf("******\n");printf("******\n");printf("******\n");return 0;}15.#include<stdio.h>int main(void){printf("*\n");printf("**\n");printf("***\n");printf("****\n");return 0;}16.#include<stdio.h>int main(void){int x1=1;int x2=10;int x3=100;int x4=1000;int x5=10000;printf("%d %d %d %d %d\n",x1,x2,x 3,x4,x5);printf("%f %f %f %f %f\n",x1,x2,x 3,x4,x5);return 0;}17.#include<stdio.h>int main(void){int x;printf("Please enter an integer:\n");scanf("%d",&x);printf("The number as a character:%c\n",x);printf("The number as a decimal:%d\n",x);printf("The number as a float:%f\n",x);return 0;}三、编程题1.#include<stdio.h>int main(void){int x1;int x2;int x3;printf("Please enter three numbers:");scanf("%d%d%d",&x1, &x2, &x3);printf("\nYour numbers forword:\n");printf("%8d\n%8d\n%8d\n",x1,x2,x3 );printf("Your numbers reversed:\n");printf("%8d\n%8d\n%8d\n",x3,x2,x1 );return 0;}2.#include<stdio.h>int main(void){int x1;int x2;int x3;int x4;int x5;int x6;int x7;int x8;int x9;int x10;printf("Please enter 10 numbers:\n");scanf("%d%d%d%d%d%d%d%d%d%d",&x1, &x2, &x3, &x4, &x5, &x6, &x7, &x8, &x9, &x10);printf("\nYour numbers are:\n");printf("%8d%8d\n",x1,x10);printf("%8d%8d\n",x2,x9);printf("%8d%8d\n",x3,x8);printf("%8d%8d\n",x4,x7);printf("%8d%8d\n",x5,x6);return 0;}3.#include<stdio.h>int main(void){int x1;int x2;int x3;int x4;int x5;int x6;int x7;int x8;int x9;printf("Input:\n");printf(" ");scanf("%d%d%d%d%d%d%d%d%d",&x1, &x2, &x3, &x4, &x5, &x6, &x7, &x8, &x9);printf("\nOutput\n");printf("%8d,%d,%d\n",x1,x2,x3);printf("%8d,%d,%d\n",x4,x5,x6);printf("%8d,%d,%d\n",x7,x8,x9);return 0;}第二章表达式与简单程序设计一、选择题1.A2.D3.C4.B5.B6.D7.B二、思考与应用题1.A.2 B.8 C.8 D.0 E.11 2.A.4,4B.4,4 C.4,4 D.2,3 E.4,53.A.12 B.-27 C.14 D.2 E.4 4.A.22.52 B.4.5 C.13.333333 D.0.882353 E.0.075.ABCEF6.A.5 B.2 C.4 D.1 E.7.A.5 B.2 C.4 D.-3 E.6 8.A.5 B.294 C.4 D.29 E.9 9. 50 50 100 2510.#include<stdio.h>int main(void){float x;float y;float p;float s;float total;printf("Please enter x and y:\n");scanf("%f%f",&x,&y);p=x*y;s=x+y;total=s*s+p*(s-x)*(p+y);printf("total=%f",total);return 0;}11.#include<stdio.h>int main(void){int x,y,z;printf("Please enter two integers:\n");scanf("%d%d",&x,&y);z=x*y;printf("%d*%d=%d",x,y,z);return 0;}12.#include<stdio.h>int main(void){float x;int y;printf("Please enter a floating point number:\n");scanf("%f",&x);y=(int)x%10;printf("%d",y);return 0;}13.#include<stdio.h>int main(void){float x;int y;printf("Please enter a floating point number:\n");scanf("%f",&x);y=(int)x/10%10;printf("%d",y);return 0;}14.#include<stdio.h>int main(void){float x;float y;float c;float s;p rintf("Please enter length and width:\n");s canf("%f%f",&x,&y);c=2*(x+y);s=x*y;p rintf("The circumference is %f\n",c);p rintf("The measure of area is %f",s);r eturn 0;}15.#include<stdio.h>int main(void){int a;f loat r;p rintf("Please enter an angle(integer):\n");s canf("%d",&a);r=a/57.295779;p rintf("%d°is %.5f radians",a,r);r eturn 0;}16.#include<stdio.h>int main(void){float c;float F;printf("Please enter centigrade:\n");scanf("%f",&c);F=32+(c*180.0/100.0);printf("Fahrenheit is %f ",F);return 0;}17.(1)double m,v,KinEn;KinEn=m*v*v/2;(2)double b,c,res;res=(b+c)/(2*b*c);18.(1)#include<stdio.h>int main(void){int x=25;x=x+5;printf("%d\n",x);x=x+5;printf("%d\n",x);return 0;}(2)#include<stdio.h>int main(void){int x=10;x=x+2;printf("%d\n",x);x=x+2;printf("%d\n",x);return 0;}(3)#include<stdio.h>int main(void){int x=32;x=x*2;printf("%d\n",x);x=x*2;printf("%d\n",x);return 0;}三、编程题1.A.#include<stdio.h>int main(void){float x;float y;printf("Please enter an number(inch):\n");scanf("%f",&x);y=x/12;printf("%f inches is %f feet ",x,y);return 0;}B.#include<stdio.h>int main(void){float x;float y;printf("Please enter an number(inch):\n");scanf("%f",&x);y=x/36;printf("%f inches is %f yards ",x,y);return 0;}C.#include<stdio.h>int main(void){float x;float y;printf("Please enter an number(inch):\n");scanf("%f",&x);y=x*2.54;printf("%f inches is %f centimeters ",x,y);return 0;D.#include<stdio.h>int main(void){float x;float y;printf("Please enter an number(inch):\n");scanf("%f",&x);y=x/39.37;printf("%f inches is %f meters ",x,y);return 0;}2.#include<stdio.h>int main(void){int fib1;int fib2;int fib3;fib1=13+21;fib2=21+fib1;fib3=fib1+fib2;printf("%d,%d,%d",fib1,fib2,fib3);return 0;}3.#include<stdio.h>int main(void){int x;int b1;int b2;int b3;int b4;int b5;printf("Please enter an integer(0-32767):\n");scanf("%d",&x);b1=x%10;b2=x/10%10;b3=x/100%10;b4=x/1000%10;b5=x/10000%10;printf("%4d%4d%4d%4d%4d\n",b5,b4,b3,b2,b1);printf("%4d%4d%4d%4d%\n",b4,b3,b2,b1);printf("%4d%4d%4d\n",b3,b2,b1);printf("%4d%4d\n",b2,b1);printf("%4d",b1);return 0;}第三章分支程序设计一、选择题1.C2.D3.A4.B5.B6.A7.C8.C 9.A二、思考与应用题1.A.0 B.1 C.1 D.0 E.0 2.A.x>=y B.x<y C.x!=y D.x==y E.x>y3.A.0 B.1 C.1 D.1 E.0 4.x=4,y=3,z=25.x=4,y=1,z=26.x=4,y=2,z=27.x=4,y=1,z=28.x=0,y=0,z=19.x=2,y=0,z=210.x=0,y=0,z=111.x=0,y=0,z=112.x=-1,y=0,z=013.x=0,y=1,z=014.x=1,y=3,z=115.x=1,y=3,z=116.x=4,y=3,z=017.if(aChar=='E'){c++;printf("Value is E\n");}18.switch(ch){case ‘E’:case ‘e’:countE++;break;case ‘A’:case ‘a’:countA++;break;case ‘I’:case ‘i’:countI++;break;default:printf("Error-Not A,E,or I\a\n"); }三、编程题1.#include<stdio.h>main( ){int x,x1,x2,x3;char grade;printf("please enter three text scores(0-100):\n");scanf("%d%d%d",&x1,&x2,&x3);x=(x1+x2+x3)/3;if(x>=90)grade='A';else if(x>=70&&x<90)if(x3>90)grade='A';elsegrade='B';else if(x>=50&&x<70)if((x2+x3)/2>70)grade='C';elsegrade='D';else if(x<50)grade='F';printf("%c\n",grade);}2.#include<stdio.h>main( ){int year;printf("please enter a year:\n");scanf("%d",&year);if(year%4==0&&year%100!=0||year%400==0)printf("%d is a leap year!\n",year);elseprintf("%d is a non-leap year!\n",year);}3.#include<stdio.h>main( ){i ntyearNow,monthNow,dayNow,year,month,day,x;printf("Please enter the date today(month day year):\n");scanf("%d%d%d",&monthNow,&dayNow,&yearNow);printf("Please enter your birthday(month day year):\n");s canf("%d%d%d",&month,&day,&year);if(monthNow<month)x=yearNow-year-1;else if(monthNow==month)if(dayNow<day)x=yearNow-year-1;elsex=yearNow-year;elsex=yearNow-year;printf("You are %d years old!\n",x);}4.#include<stdio.h>#include<stdlib.h>#include<time.h>main( ){int number,Ynumber;srand(time(NULL));number=rand()%20+1;printf("I am thinking of a number between 1 and 20.\n");printf("Can you guess what it is?");scanf("%d",&Ynumber);if(Ynumber==number)printf("Congratulations!You did it.");else{if(Ynumber>number)printf("Your guess is high.Try again:");elseprintf("Your guess is low.Try again:");scanf("%d",&Ynumber);if(Ynumber==number)printf("Congratulations!You did it.");else{if(Ynumber>number)printf("Your guess is high.Try again:");elseprintf("Your guess is low.Try again:");scanf("%d",&Ynumber);if(Ynumber==number)printf("Congratulations!You did it.");else{if(Ynumber>number)printf("Your guess is high.Try again:");elseprintf("Your guess is low.Try again:");scanf("%d",&Ynumber);if(Ynumber==number)printf("Congratulations!You did it.");else{if(Ynumber>number)printf("Your guess is high.Try again:");elseprintf("Your guess is low.Try again:");scanf("%d",&Ynumber);if(Ynumber==number)printf("Congratulations!You did it.");else{printf("sorry.The number was %d.\n",number);printf("You should have gotten it by now.\n");printf("Better luck next time.");}}}}}}5.#include<stdio.h>main( ){int year,month,day,x=0,y,z;printf("Please enter your birthday:(year month day)\n");scanf("%d%d%d",&year,&month,&day);y=((year-1)*365+((year-1)/4)-((year-1)/100) +((year-1)/400))%7;switch(month){case 12:x=x+30;case 11:x=x+31;case 10:x=x+30;case 9:x=x+31;case 8:x=x+31;case 7:x=x+30;case 6:x=x+31;case 5:x=x+30;case 4:x=x+31;case 3:x=x+28;case 2:x=x+31;case 1:x=x+0;}if(year%4==0&&year%100!=0||year%400==0)day++;z=(x+day+y)%7;switch(z){case 0:printf("The day of your birthday is Sunday!");break;case 1:printf("The day of your birthday is Monday!");break;case 2:printf("The day of your birthday is Tuseday!");break;case 3:printf("The day of your birthday is Wednesday!");break;case 4:printf("The day of your birthday is Thursday!");break;case 5:printf("The day of your birthday is Friday!");break;case 6:printf("The day of your birthday is Saturday!");break;}}6.#include<stdio.h>#include<math.h>main( ){int a,b,c,p;float x1,x2;printf("Please enter a,b,c:\n");scanf("%d%d%d",&a,&b,&c);p=b*b-4*a*c;if(a==0)if(b==0)printf("There is no solution!");else{x1=-(float)c/b;printf("The solution is x1=%f",x1);}elseif(p<0)printf("There is no real solution!");else{x1=-b+sqrt(p)/(2*a);x2=-b-sqrt(p)/(2*a);printf("The solutions are x1=%f,x2=%f",x1,x2);}}。
C语言课程 课后练习 参考答案

实验6-1课后练习:1.选择题(1)B (2)C(3)A(4)D(5)C (6)C (7)D 2.阅读程序,写出运行结果3020103.阅读程序,写出运行结果20104. 阅读程序,写出运行结果1 2 35.阅读程序,写出运行结果7 8 96.阅读程序,写出运行结果97.阅读程序,写出运行结果#include <stdio.h>int digit(int n,int k);int main(){ int n,k;printf("Enter n,k: ");scanf("%d %d",&n,&k);printf("\nThe result is:%d\n",digit(n,k));return 0;}int digit(int n,int k){ int d;while(k>0){ d=n%10;n=n/10;k--;}return d;}8.程序填空#include <stdio.h>double max(double a,double b);int main(){ double x,y;prinf("please input two double numbers:"); scanf("%lf %lf"),&x,&y;printf("%.2f\n",max(x,y));return 0;}double max(double a,double b){return (a>b?a:b);}9.原版#include <stdio.h>int check(int n,int d);int main(){ int n,d;printf("Enter n,d: ");scanf("%d %d",&n,&d);if(check(n,d)==1)printf("The digit %d is in data %d",d,n); elseprintf("The digit %d is not in data %d",d,n); return 0;}int check(int n,int d){ int temp;while(n>0){ temp=n%10;if(temp==d)return 1;n=n/10;}return 0;}改进版#include <stdio.h>int check(int n,int d);int main(){ int n,d,position;printf("Enter n,d: ");scanf("%d %d",&n,&d);position=check(n,d);if(check(n,d)>0)printf("The digit %d is in data %d,position is %d",d,n,position);elseprintf("The digit %d is not in data %d",d,n);return 0;}int check(int n,int d){ int temp,position=1;while(n>0){ temp=n%10;if(temp==d)return position;position++;n=n/10;}return 0;}实验6-2课后练习1.选择题(1)B (2)C(3)B(4)B(5)C (6)B (7)D (8)D (9)C(10)B(11)A2.填空(1) 函数中声明有局部变量,若声明中带有static,则该局部变量采用静态存储方式,在编译时分配空间,若函数中未显式设定初始值,则系统将其自动初始化,函数被调用结束时,该空间不释放。
C语言课后习题参考答案

C语言课后习题参考答案第一章:基础知识问题1:C语言的发展历史及特点C语言是由贝尔实验室的肯·汤普逊和丹尼斯·里奇在20世纪70年代初开发的一种高级程序设计语言。
它以其简洁、高效和可移植性而闻名。
C语言被广泛应用于系统级编程、嵌入式系统和科学计算等领域,并成为其他编程语言的基础。
问题2:C语言的基本数据类型及其存储范围C语言的基本数据类型包括字符型(char)、整型(int)、浮点型(float)、双精度浮点型(double)和空类型(void)。
它们的存储范围如下:- char:-128 到 127 或 0 到 255- int:-32,768 到 32,767 或 0 到 65,535- float:3.4E-38 到 3.4E38- double:1.7E-308 到 1.7E308- void:无特定存储范围问题3:如何在C语言中输出语句?在C语言中,可以使用printf函数来输出语句。
它的基本语法为:```cprintf("要输出的内容");```问题4:定义变量的语法规则在C语言中,定义变量的语法规则如下:```c数据类型变量名;```例如,定义一个整型变量x:```cint x;```问题5:如何进行变量的赋值?变量的赋值可以使用赋值运算符(=)。
例如,将一个整型常量10赋值给变量x:```cx = 10;```第二章:控制结构问题1:if语句的语法结构if语句用于根据条件执行不同的代码块。
它的基本语法结构如下:```cif (条件) {// 条件为真时执行的代码}```问题2:switch语句的用法及语法结构switch语句用于根据不同的表达式值执行相应的代码块。
它的基本用法及语法结构如下:```cswitch (表达式) {case 值1:// 当表达式的值等于值1时执行的代码break;case 值2:// 当表达式的值等于值2时执行的代码break;default:// 当表达式的值与所有case都不匹配时执行的代码break;}```问题3:循环语句的分类及语法结构C语言中的循环语句包括for循环、while循环和do-while循环。
(完整word版)数据结构(c语言版)课后习题答案完整版资料

第1章绪论5.选择题:CCBDCA6.试分析下面各程序段的时间复杂度。
(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.选择题babadbcabdcddac2.算法设计题(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) {// 逆置带头结点的单链表Lp=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的数据元素位置。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
第3章三、编程题1. 编写程序,输入一个非负数,输出以此数为半径的圆周长以及面积。
#include "stdio.h"#define PI 3.1415void 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 <stdio.h>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 <stdio.h>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 <stdio.h>void main(){int a;scanf("%d",&a);if(a%2)printf("奇数\n");elseprintf("偶数\n");}2.编写程序,输入一个24小时制的时间,转换为12小时制时间后进行输出。
以13点15分为例,输入:13:15,则输出:下午1:15。
#include <stdio.h>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);elseprintf("%d不是闰年\n",year);}4.输入一个字符,如果是大写字母则输出对应的小写字母,如果是小写字母则输出相应的大写字母,如果都不是则原样输出。
#include <stdio.h>void main(){char ch;scanf("%c",&ch);if(ch>='a'&&ch<='z')ch-=32;elseif(ch>='A'&&ch<='Z')ch+=32;printf("\n%c\n",ch);}5.设计一个简单的计算器程序,能输入整型运算数和基本运算符(+,-,*,/),输出计算结果。
例如:输入2+6,输出2+6=8。
#include<stdio.h>main(){float a,b,result;char op;scanf("%f%c%f",&a,&op,&b);switch(op){case'+': result=a+b; printf("=%f",result); break;case'-': result=a-b; printf("=%f",result); break;case'*': result=a*b; printf("%f",result); break;case'/': if(b==0.0) printf("error!\n");result=a/b;printf("=%f",result);default: printf("error due to the illegal input!\n");}}第5章三、编程题1.编写程序,显示100~200之间能被7除余2的所有整数。
#include "stdio.h"main(){ int i;for(i=100;i<=200;i++){ if(i%7==2)printf("\t%d\t",i);}}2.输入n个整数,求这n个整数中的最大数、最小数和偶数平均数,并输出。
#include <stdio.h>void main(){int i,n,data,max=0,min=0,even=0,evennumber=0;printf("Please input the number of data:");scanf("%d",&n);printf("Please input the data:\n");scanf("%d",&data);max=data;min=data;if (data%2==0){even=even+data;evennumber++;}for(i=1;i<n;i++){scanf("%d",&data);if (data>max)max=data;elseif (data<min)min=data;if (data%2==0){even=even+data;evennumber++;}}printf("The max is %d\nThe min is %d\n The average of even is %5.2f\n",max,min,(float)(even)/evennumber);}3.输入一串字符,以回车作为结束标志。
统计并输出这串字符中大写字母、小写字母和数字字符的个数。
#include <stdio.h>void main(){int upper=0,lower=0,number=0;char letter;while((letter=getchar())!='\n'){if (letter>='a'&&letter<='z') lower++;else if (letter>='A'&&letter<='Z') upper++;else if (letter>='0'&&letter<='9') number++;}printf("the number of uppercase is:%d\n",upper);printf("the number of lowercase is:%d\n",lower);printf("the number of number is:%d\n",number);}4.输出九九乘法表。
#include <stdio.h>void main(){int i,j;for(i=1;i<=9;i++){for(j=1;j<=i;j++)printf("%d*%d=%-3d ",i,j,i*j);printf("\n");}}5.编写程序,输出3~1000之间全部素数。
#include <stdio.h>#include "math.h"void main(){int k,data,tag;for(data=3;data<=1000;data++) //外层循环,用来产生2~1000之间的整数{tag=0; //tag用于表示数i是否是素数,没有判断前先假定是素数for(k=2;k<=sqrt(data)&&!tag;k++) //内层循环用来判断data是否有约数{if (data%k==0)tag=1;}if (tag==1) printf("%4d",data); //如果i是素数,则输出}}6.输入一个三位数,判断其是否是“水仙花数”。
水仙花数是指3位数中的各位数字的立方和等于这3位数本身。
如153=1*1*1+5*5*5+3*3*3。
#include"stdio.h"void main(){int S, a, b, c;printf("请输入一个3位数:");scanf("%d",&S);a=S/100; b=S%100/10; c=S%10;if(a*a*a+b*b*b+c*c*c==S)printf("%d是水仙花数。
",S);elseprintf("%d不是水仙花数。
\n",S);}7.编程求Fibonacci数列的前40个数。
该数列的生成方法是:F1=1,F2=1,F n=F n-1+F n-2(n>=3)(即从第三个数起,每个数等于前2个数之和)。