高等教育出版社出版社c语言程序设计实践教程习题参考答案
《C语言程序设计教程》(第三版)课后习题参考答案(精选5篇)[修改版]
第一篇:《C语言程序设计教程》(第三版)课后习题参考答案C语言程序设计课后习题参考答案习题一一、单项选择题1、C2、B3、B4、C5、D6、A7、C8、A二、填空题1、判断条件2、面向过程编程3、结构化4、程序5、面向对象方法6、基本功能操作、控制结构7、有穷性8、直到型循环结构9、算法10、可读性11、模块化12、对问题的分解和模块的划分习题二一、单项选择题1、B2、D3、C4、B5、A二、填空题1、主2、C编译系统3、结构化4、程序5、面向对象方法6、.OBJ7、库函数8、直到型循环结构习题三一、单项选择题1、D2、B3、A4、C9、B10、C11、A12、D13、C17、B18、C19、C20、D21、A25、D26、A二、填空题1、补码2、10^-138~10^1 38、15~166、A7、B6、D7、B14、B15、C22、D8、C8、D16、A24、D、A3、实4、单目运算符、自右向左5、函数调用6、65,89习题四一、单项选择题1、D2、C3、D4、A5、D6、B7、A8、C9、B10、B二、填空题1、两, ;2、5.1690003、-200 2500、i=-200,j=2500回车、i=-200回车j=2500回车4、a=98,b=765.000000,c=4321.0000005、100 25.81 1.89234、100,25.81,1.89234、100回车25.81回车1.89234回车6、0,0,37、38、scanf(“%lf %lf %lf”,&a,&b,&c);9、13 13.000000 13.00000010、c=b-a;a=b+c;习题五一、单项选择题1、B2、D3、C4、B5、B6、D7、A8、B二、填空题1、1 、02、k!=03、if(x>4||xelse printf(“error!”); 4、if(((x>=1&&x=200&&x Printf(“%d”,x);5、16、17、10! Right!8、a=09、2,110、0习题六一、单项选择题9、D1、B2、C3、C4、B5、C6、B7、C8、A二、填空题1、无穷次2、83、205、3.66、*#*#*#$7、828、d=1.0 、k++、k9、!(x习题七一、单项选择题1、B2、D3、C4、C5、A二、填空题1、1 2 4 8 16 32 64 128 256 5122、a[age]++、i=18;i3、break、i==84、a[i]>b[i]、i3、j5、b[j]=0、b[j]=a[j][k]习题八一、单项选择题1、B3、C4、A5、A9、D10、B11、A12、C13、A二、填空题1、return1、return n+sum(n-1)2、return1、n*facto(n-1)习题九一、单项选择题1、D2、C3、D4、A5、C9、B10、C11、A13、B17、C18、A19、B20、C二、填空题1、int 、return z2、*p++3、’\0’、++4、p、max*q6、A7、D6、A7、C14、C15、B6、C7、C14、A15、D8、A8、D8、C习题十一、单项选择题1、D2、D3、A4、5、B6、A7、C8、B9、D10、11、C12、D13、D14、C二、填空题1、34 122、ARRAY a[10],b[10],c[10];3、2 34、ab、cd5、(*b).day、b->day6、adghi mnohi no7、(struct node *)、!=’\n’、p=top;8、p1=p1->next9、(struct list *)、(struct list *)、return (n)习题十一一、单项选择题1、A2、A3、B4、A5、B9、A10、B11、B12、B13、C17、D二、填空题1、ASCII(文本)、二进制2、pf=fopen(“A:\zk04\data xfile.dat”,”w”);3、fputc()、fputs()、fscanf()、fread()4、(后两个空)文件结束符、非0值7、B14、C15、D8、A16、A第二篇:C语言程序设计教程课后习题参考答案《C语言程序设计教程》课后习题参考答案习题1 1. (1)编译、链接.exe (2)函数主函数(或main函数)(3)编辑编译链接2.(1)-(5):DDBBC (6)-(10):ABBBC 3.(1)答:C语言简洁、紧凑,使用方便、灵活;C语言是高级语言,同时具备了低级语言的特征;C 语言是结构化程序设计语言,具有结构化的程序控制语句;C语言有各种各样的数据类型;C语言可移植性好;生成目标代码质量高,程序执行效率高。
C程序设计基础第二版(高等教育出版社)习题答案
C程序设计基础第二版(高等教育出版社)习题答案习题一参考答案第1章C++语言概述一、选择题:1.D2.A3.D4.A5.A6.D7.C8.B9.B二、改后如下:#include//预处理指令,原程序缺少“#”号intmain()//定义主函数,原程序缺少返回值类型“int”(也可为“void”){cout<运行后输出:HelloWorld!三、简答题1、解答:在较大或较复杂的C++程序中,加上相关的注释信息,解释相应的程序,可提高程序的可读性。
2、C++语言有两种注释:(1)C++型注释。
从“//”开始,该行中所有字符都被作为注释处理。
(2)C型注释。
“/某”表示注释的开始,“某/”表示注释的结束,在“/某”、“某/”之间的字符均被视作注释。
3、C型注释主要用于大块注释,而且C++型注释可以嵌在C型注释中。
四、程序设计题1、编写一个程序,在屏幕上输出“IcanueC++”。
解:#includeintmain(){cout<运行后输出:IcanueC++2、参照例题,设计一个C++程序,输入两门课程的成绩,求总成绩。
解:#includemain(){int1,2,um;cout<cin>>1>>2;um=1+2;cout<}3、参照例题,设计一个C++程序,输入a、b两个整数,输出其中的最小数。
解:#includevoidmain(void){intmin(int某,inty);inta,b,m;cout<>a>>b;m=min(a,b);cout<intmin(int某,inty){intz;if(某z=y;returnz;}习题二参考答案第2章数据类型和表达式一、选择题1.C2.B3.B4.B5.A6B7.D8.C9.D11.D12..A13.D14.A15.B16.B17.B18.C19.A二、填空题1、基本类型导出类型用户定义类型2、63、iotream#include<iotream>4、一个int型数据占用的字节数5、210.C6、77、1008、?:9、falefaletrue10、(50<某)&&(某<=100)或!((50>某)||(某>100))11、(m)某100+((m/10))某10+m/10012、非零13、114、字母数字下划线15、4.5三、问答题1.C++语言中用数值“1”表示“真”,数值“0”表示“假”。
C语言程序设计 (何钦铭 颜晖 著) 高等教育出版社第八章 课后答案
}
习题8-7
/*输入5个字符串,按由小到大的顺序输出。*/
/*指针和数组及存储单元-选择排序算法*/
#include <stdio.h>
#include <string.h>
void main(void)
{
char s[5][80],t[80];
int i,j,index;
/*输入5个字符串到数组s*/
{
index=i;
for (j = i+1; j < n; j++ )
if (a[j] < a[index])index=j;/*比较大小,记录最小元的下标*/
swap(&a[i], &a[index]);/*交换最小元与a[i]的值*/
}
}
/*定义函数swap,实现两个数交换*/
void swap (int *px, int *py)
scanf("%d",&x);
/*调用find函数,在数组a中查找xห้องสมุดไป่ตู้在位置*/
result=find(a,10,x);
/*输出查找结果*/
if(result==-1)printf("Not found.\n");
else printf("The position is %d\n",result);
void mcopy(char *s,char *t,int m);
void main()
{
char s[80],t[80];
int m;
/*输入一个字符串*/
printf("Enter a string : ");
高等教育出版社c语言程序设计实践教程习题参考答案
高等教育出版社c语言程序设计实践教程习题参考答案C语言是一门功能强大的编程语言,广泛应用于计算机科学和软件开发领域。
高等教育出版社出版的C语言程序设计实践教程是一本备受学生和程序员欢迎的教材。
本文将为读者提供C语言程序设计实践教程中的习题参考答案,以帮助读者更好地掌握C语言的编程技巧和实践能力。
第一章 C语言基础知识1.1 变量和数据类型C语言中的变量用于存储和操作数据。
变量的类型决定了变量可以存储的数据的种类和范围。
C语言中常见的数据类型包括整型、浮点型、字符型等。
以下是一些习题的参考答案:习题 1:```c#include <stdio.h>int main() {int num = 10;float factor = 1.5;char letter = 'A';printf("num = %d\n", num);printf("factor = %f\n", factor); printf("letter = %c\n", letter); return 0;}```习题 2:```c#include <stdio.h>int main() {int a = 10;int b = 20;int temp;temp = a;a = b;b = temp;printf("交换后:\n");printf("a = %d\n", a);printf("b = %d\n", b);return 0;```1.2 运算符和表达式C语言提供了多种常见的运算符,如算术运算符、赋值运算符、比较运算符等。
通过使用运算符,我们可以对数据进行各种操作和计算。
以下是一些习题的参考答案:习题 1:```c#include <stdio.h>int main() {int a = 10;int b = 3;int result;result = a % b;printf("result = %d\n", result);return 0;}```习题 2:#include <stdio.h>int main() {int a = 10;int b = 5;printf("结果为:%d\n", a > b ? a : b);return 0;}```第二章 C程序控制结构2.1 顺序结构C语言中的顺序结构就是按照程序代码的顺序执行。
C语言程序设计实例教程第2版习题答案作者李红第4章选择结构程序设计
4.3 课后习题4.3.1 项目练习一.练习目的1.进一步巩固选择结构程序设计语句的使用2.进一步巩固break语句的使用方法3.提高编程和调试程序的能力二.练习内容1.接受用户输入的三种商品的价格。
如果购买的三种商品中至少有一种商品的价格大于50 或者三种商品的总额大于100,则折扣率为15%,否则折扣率为0,计算并显示用户应付的钱数。
#include "stdio.h"main(){float a,b,c,price;scanf("%f%f%f",&a,&b,&c);price=a+b+c;if((a>50)||(b>50)||(c>50)||(price>100))price=price*0.85;printf("%.2f",price);getch();}2.判断所输入的一个年份是否为闰年。
#include "stdio.h"main(){int year,flag=0;scanf("%d",&year);if((year%4==0)&&(year%100!=0)||(year%400==0))flag=1;if(flag==1)printf("%d年是闰年!",year);elseprintf("%d年不是闰年!",year);getch();}3.利用if结构编写程序,输入x值,求解以下分段函数的y值。
当x<1时,y=x+1;当1≤x<10,y=2x+5;当x≥10时,y=x2+8。
#include "stdio.h"main(){int x,y;scanf("%d",&x);if(x<1) y=x+1;else if(x>=10) y=x*x+8;else y=2*x+5;printf("%d",y);getch();}4.编写一个程序,根据用户输入的期末考试成绩,输出相应的成绩评定信息。
《C语言程序设计》(苏小红)-课后习题答案-高等教育出版社
2.2#include<stdio.h>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<stdio.h>main(){int a=12,b=3;float x=18.5,y=4.6;}3.1(2)#include<stdio.h>main(){int x=32,y=81,p,q;p=x++;q=--y;printf("%d %d\n",p,q);printf("%d %d\n",x,y);}3.2#include<stdio.h>main(){int x,b0,b1,b2,s;printf("Inputx:");scanf("%d",&x);b2=x/100;b1=(x-b2*100)/10;//或(x%100)/10;或x/10%10;b0=x%10;s=b0*100+b1*10+b2;printf("s=%d\n",s);}3.3#include<stdio.h>#include<math.h>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<stdio.h>#include<math.h>main(){float a, b, c;double x, y;printf("Input a, 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);}习题44.1(1)#include<stdio.h>main(){char c1='a',c2='b',c3='c';}4.1(2)#include<stdio.h>main(){int a=12,b=15;}4.1(3)#include<stdio.h>main(){int a,b;printf("%d,%d\n",a,b);}4.2#include<stdio.h>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<stdio.h>main(){float a;printf("Input a:");scanf("%f",&a);if(a>=0){a=a;printf("a=%f\n",a);}else{a=-a;printf("a=%f\n",a);}}5.2#include<stdio.h>main(){int a;printf("Input a:");scanf("%d", &a);if(a%2==0){printf("a是偶数");}else{printf("a是奇数");}}5.3#include<stdio.h>#include<math.h>main(){float a,b,c,s,area;printf("Input a, b, c:");scanf("%f %f %f", &a, &b, &c);if(a+b>c&&a+c>b&&b+c>a){s=(a+b+c)/2;area=(float)sqrt(s*(s-a)*(s-b)*(s-c));printf("area=%f\n",area);}else{printf("不是三角形");}}5.4#include<stdio.h>#include<math.h>main(){float a,b,c,x,y;printf("Inputa,b,c:");scanf("%f,%f,%f",&a,&b,&c);if(a==0){printf("该方程不是一元二次方程\n");}if(b*b-4*a*c>0){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);}else if(b*b-4*a*c==0){x=-b/(2*a);y=-b/(2*a);printf("x=%f,y=%f\n",x,y);}else{printf("该方程无实根\n");}}5.5#include<stdio.h>main(){int year,flag;printf("Input a year:");scanf("%d",&year);if(year%4==0&&year%400!=0||year%400==0){flag=1;}else{flag=0;}if(flag==1){printf("%d is a leap year!\n",year);}else{printf("%d is not a leap year!\n",year);}}5.6#include<stdio.h>main(){int year,flag;printf("Input a year:");scanf("%d",&year);flag=year%400==0||year%4==0&&year%100!=0?1:0;if(flag==1&&flag!=0){printf("%d is a leap year!\n",year);}else{printf("%d is not a leap year!\n",year);}}5.7#include<stdio.h>main(){char ch;printf("Inputch:");scanf("%c",&ch);if(ch>='a'&&ch<='z'){ch=getchar();ch=ch-32;printf("%c,%d\n",ch,ch);}else if(ch>='A'&&ch<='Z'){ch=getchar();ch=ch+32;printf("%c,%d\n",ch,ch);}else{printf("%c",ch);}}5.8#include<stdio.h>main(){char ch;printf("Inputch:");scanf("%c",&ch);if(ch>=48&&ch<=57){printf("ch是数字字符\n");}else if(ch>=65&&ch<=90){printf("ch是大写字母\n");}else if(ch>=97&&ch<=122){printf("ch是小写字母\n");}else if(ch==32){printf("ch是空格\n");}else{printf("ch是其他字符\n");}}5.9#include<stdio.h>main(){int score,grade;printf("Input score:");scanf("%d",&score);grade=score/10;if(score<0||score>100){printf("Input error\n");}if(score>=90&&score<=100){printf("%d--A\n",score);}else if(score>=80&&score<90){printf("%d--B\n",score);}else if(score>=70&&score<80){printf("%d--C\n",score);}else if(score>=60&&score<70){printf("%d--D\n",score);}else if(score>=0&&score<60){printf("%d--E\n",score);}}5.10#include<stdio.h>main(){int year,month;printf("Input year,month:");scanf("%d,%d",&year,&month);if(month>12||month<=0){printf("error month\n");}else{switch(year,month){case12:case10:case8:case7:case5:case3:case1:printf("31天\n");break;case11:case9:case6:case4:printf("30天\n");break;case2:if(year%4==0&&year!=0||year%400==0){printf("29天\n");}else{printf("28天\n");}break;default:printf("Input error\n");}}}6.1(1)#include<stdio.h>main(){int i,j,k;char space='';for(i=1;i<=4;i++){for(j=1;j<=i;j++){printf("%c",space);}for(k=1;k<=6;k++){printf("*");}printf("\n");}}6.1(2)#include<stdio.h>main(){int k=4,n;for(n=0;n<k;n++){if(n%2==0)continue;k--;}printf("k=%d\n,n=%d\n",k,n);}6.1(3)#include<stdio.h>main(){int k=4,n;for(n=0;n<k;n++){if(n%2==0)break;k--;}printf("k=%d,n=%d\n",k,n);}6.2(1)#include<stdio.h>main(){int i,sum=0;for(i=1;i<=101;i++){sum=sum+i;}printf("sum=%d\n",sum);}6.2(2)#include<stdio.h>main(){long i;long term,sum=0;for(i=1;i<=101;i=i+2){term=i*(i+1)*(i+2);sum=sum+term;}printf("sum=%ld\n",sum);}6.2(4)#include<stdio.h>#include<math.h>main(){int n=1;float term=1.0,sign=1,sum=0;while(term<=-1e-4||term>=1e-4){term=1.0/sign;sum=sum+term;sign=sign+n;n++;}printf("sum=%f\n",sum);}6.2(5)#include<stdio.h>#include<math.h>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)=%f,count=%d\n",sum,count);}6.3#include<stdio.h>main(){int x=1,find=0;while(!find){if(x%2==1&&x%3==2&&x%5==4&&x%6==5&&x%7==0){printf("x=%d\n",x);find=1;x++;}}}/*int x,find=0;for(x=1;!find;x++){if(x%2==1&&x%3==2&&x%5==4&&x%6==5&&x%7==0){printf("x=%d\n",x);find=1;}}}*/6.4#include<stdio.h>main(){int i,n;long p=1,m=1;printf("Input n:");scanf("%d",&n);for(i=1;i<=n;i++){p=i*i;m=i*i*i;printf("p=%d,m=%d\n",i,p,i,m);}}6.5#include<stdio.h>main(){float c,f;for(c=-40;c<=110;c=c+10){f=9/5*c+32;printf("f=%f\n",f);}}6.6#include<stdio.h>#include<math.h>main(){int n;double c=0.01875,x;do{x=x*pow(1+c,12)-1000;n++;}while(x>0);printf("x=%d\n",x);}6.7#include<stdio.h>main(){int n=0;float a=100.0,c;printf("Inputc:");scanf("%f",&c);do{a=a*(1+c);n++;}while(a<=200);printf("n=%d\n",n);}6.8#include<stdio.h>#include<math.h>main(){int n=1,count=1;double sum=1,term=1;while(fabs(term)>=1e-5){term=pow(-1,count)*(1.0/(n+2));sum=sum+term;n=n+2;count++;}sum=4*sum;printf("sum=%f,count=%d\n",sum,count); }6.9#include<stdio.h>#include<math.h>main(){int n=1,count=1;double sum=1,term=1;while(fabs(term)>=1e-5){term=term*(1.0/n);sum=sum+term;n++;count++;}printf("sum=%f,count=%d\n",sum,count);}6.10#include<stdio.h>#include<math.h>main(){int x;for(x=100;x<=999;x++){if(x==pow(x/100,3)+pow(x/10%10,3)+pow(x%10,3))printf("x=%d\n",x);}}6.11#include<stdio.h>main(){int i=0,n;long sum=0,term=1;printf("Inputn:");scanf("%d",&n);do{i++;term=term*i;sum=sum+term;}while(sum<n);printf("%d\n",i-1);}6.12#include<stdio.h>main(){int i,n,m,count=0,sum=0;printf("Input n:");scanf("%d",&n);for(i=1;i<=n;i++){printf("Inputm:");scanf("%d",&m);if(m>0){sum=sum+m;count++;}else{break;}printf("sum=%d,count=%d\n",sum,count);}}6.13#include<stdio.h>main(){int i,n,m,count=0,sum=0;printf("Inputn:");scanf("%d",&n);for(i=1;i<=n;i++){printf("Inputm:");scanf("%d",&m);if(m>0||m<0){sum=sum+m;count++;}else{break;}printf("sum=%d,count=%d\n",sum,count);}}6.14#include<stdio.h>main(){int x,y,z;for(x=0;x<=17;x++){for(y=0;y<=25;y++){3*x+2*y+z==50;z=30-x-y;if(3*x+2*y+z==50&&x+y+z==30)printf("x=%d,y=%d,z=%d\n",x,y,z);}}}6.15#include<stdio.h>main(){int x,y;for(x=0;x<=98;x++){y=98-x;2*x+4*y==386;if(x+y==98&&2*x+4*y==386){printf("x=%d,y=%d\n",x,y);}}}6.16#include<stdio.h>main(){int x,y,z;for(x=0;x<=20;x++){for(y=0;y<=33;y++){3*y+5*x+z/3.0==100;z=100-x-y;if(5*x+3*y+z/3.0==100&&z+x+y==100){printf("x=%d,y=%d,z=%d\n",x,y,z);}}}}6.17#include<stdio.h>main(){int x,y,z;for(x=1;x<=9;x++){for(y=1;y<=17;y++){10*x+5*y+z==100;z=50-x-y;if(10*x+5*y+z==100&&x+y+z==50&&z>0){printf("x=%d,y=%d,z=%d\n",x,y,z);}}}}7.1#include<stdio.h>int Square(int i){return i*i;}int main(){int i=0;i=Square(i);for(;i<3;i++){static int i=1;i+=Square(i);printf("%d,",i);}printf("%d\n",i);return0;}7.2#include<stdio.h>int hour,minute,second;void update(){second++;if(second==60){second=0;minute++;}if(minute==60){minute=0;hour++;}if(hour==24)hour=0;}void display(){printf("%d,%d,%d\n",hour,minute,second); }void delay(){int t;for(t=0;t<100000000;t++);}int main(){int i;void updaye(),display(),delay();for(i=0;i<1000000;i++){update();display();delay();}return0;}7.3#include<stdio.h>int GetMax(int a,int b);int main(){int x,y,max;printf("Inputx,y:");scanf("%d,%d",&x,&y);max=GetMax(x,y);printf("max=%d\n",max);return0;}int GetMax(int m,int n){if(m>=n)return m;elsereturn n;}7.4#include<stdio.h>int LCM(int n,int m);int main(){int a,b;printf("Inputa,b:");scanf("%d,%d",&a,&b);printf("%d\n",LCM(a,b));return0;}int LCM(int n,int m){int x;int find=0;for(x=1;!find;x++){if(x%n==0&&x%m==0){find=1;}}return x-1;}7.5#include<stdio.h>long Fact(int n);int main(){int m,a;printf("Inputm:");scanf("%d",&m);for(a=1;a<=m;a++){printf("%d!=%ld\n",a,Fact(a));}return0;}long Fact(int n){int i;long result=1;for(i=2;i<=n;i++)result*=i;return result;}7.6#include<stdio.h>long Fact(int n);int main(){int m;long ret;printf("Inputm:");scanf("%d",&m);ret=Fact(m);printf("ret=%d\n",ret);return0;}long Fact(int n){int i;long result=1,sum=0;for(i=2;i<=n;i++){result*=i;sum=sum+result;}return sum;}7.7(1)#include<stdio.h>int Gcd(int a,int b);int main(){int m,n;printf("Inputm,n:");scanf("%d,%d",&m,&n);printf("%d\n",Gcd(m,n));return0;}int Gcd(int a,int b){int t,min,find=0;min=a<b?a:b;t=min;for(t=min;!find;t--){a%t==0;b%t==0;if(a%t==0&&b%t==0)return t;}find=1;}7.7(2)#include<stdio.h> int Gcd(int a,int b);int main(){int m,n;printf("Inputm,n:");scanf("%d,%d",&m,&n);printf("%d\n",Gcd(m,n));return0;}int Gcd(int a,int b){int r,temp;r=a%b;if(r==0)return b;elsedo{temp=b;b=r;a=temp;r=a%b;}while(r!=0);return b;}8.1(1)#include<stdio.h> void Func(int x){x=2;}int main(){int x=10;Func(x);printf("%d",x);return0;}8.1(2)#include<stdio.h> void Func(int b[]){int j;for(j=0;j<4;j++){b[j]=j;}}int main(){static int a[]={5,6,7,8},i;Func(a);for(i=0;i<4;i++){printf("%d",a[i]);}return0;}8.2(1)int PositiveNum(int a[],int n) {int i,count=0;for(i=0;i<n;i++){if(a[i]>0)count++;}return0;}8.2(2)void Fib(long f[],long n){int i;f[0]=0;f[1]=1;for(i=2;i<n;i++){f[i]=f[i-1]+f[i-2];}}8.2(3)#include<stdio.h>int main(){int a[10],n,max,min,maxPos,minPos;for(n=0;n<10;n++){scanf("%d",&a[n]);}max=min=a[0];maxPos=minPos=0;for(n=0;n<10;n++){if(a[n]>max){max=a[n];maxPos=n;}else if(a[n]<min){min=a[n];minPos=n;}}printf("max=%d,pos=%d\n",max,maxPos);printf("min=%d,pos=%d\n",min,minPos);return0;}8.3void DivArray(int*pArray,int n){int i;for(i=0;i<n;i++){pArray[i]/=pAttay[0];}}8.4#include<stdio.h>#define N40int FailNum(int score[],int n);void ReadScore(int score[],int n);int main(){int score[N],n;printf("Input n:");scanf("%d",&n);ReadScore(score,n);FailNum(score,n);printf("FailNum students are%d\n",FailNum(score,n));return0;}int FailNum(int score[],int n){int i,count=0;for(i=0;i<n;i++){if(score[i]<60)count++;}return count;}void ReadScore(int score[],int n){int i;printf("Input score:");for(i=0;i<n;i++){scanf("%d",&score[i]);}}8.5#include<stdio.h>#define N40int HighAver(int score[],int n);int ReadScore(int score[]);int main(){int score[N],n;n=ReadScore(score);printf("HighAver students are%d\n",HighAver(score,n));return0;}int HighAver(int score[],int n){int i,count=0,sum=0,aver;for(i=0;i<n;i++){sum=sum+score[i];aver=sum/n;}for(i=0;i<n;i++){if(score[i]>aver)count++;}return count;}int ReadScore(int score[]){int i=-1;do{i++;printf("Input score:");scanf("%d",&score[i]);}while(score[i]>=0);return i;}8.6#include<stdio.h>#define N40int ReadScore(int score[],long num[]);int FindMax(int score[],long num[],int n);int main(){int score[N],n,max,num,t;long num[N];t=FindMax(score,n);max=score[t];num=num[t];printf("max=%d,num=%d\n",max,num);return0;}int ReadScore(int score[],long num[]){int i=-1;do{i++;printf("Input student's ID and score:");scanf("%ld%d",&num[i],&score[i]);}while(num[i]>0&&score[i]>=0);return i;}int FindMax(int score[],long num[],int n){int max,i;max=score[0];for(i=1;i<n;i++){if(score[i]>max)max=score[i];}return i;}8.7#include<stdio.h>#define N40int Read(int a[]);int ChangeArry(int a[],int n);int main(){int a[N],n;printf("%d\n",ChangeArry(a,n));return0;}int Read(int a[]){int i;for(i=0;i<9;i++){printf("Input a:");scanf("%d",&a[i]);return i;}int ChangeArry(int a[],int n){int max,min,i,maxpos,minpos,term;max=min=score[0];for(i=1;i<n;i++){if(a[i]>max)max=score[i];maxpos=n;else(a[i]<min)min=a[i];minpos=n;temp=maxpos;maxpos=minpos;minpos=temp;}return a[i];}8.10#include<stdio.h>#define N5void main(){int a[N][N];int sum=0;int i,j;printf("请输入一个%d*%d的矩阵\n",N,N);for(i=0;i<N;i++){for(j=0;j<N;j++){scanf("%d",&a[i][j]);}}for(i=0;i<N;i++){for(j=0;j<N;j++){if(i==j||i+j==N-1)sum=sum+a[i][j];}}printf("对角线元素之和为:%d\n",sum);}8.11#include<stdio.h>#define N2#define M3void main(){int a[N][M],b[N][M];int i,j,sum1=0,sum2=0,sum=0;printf("请输入一个%d*%d的矩阵\n",N,M);for(i=0;i<N;i++){for(j=0;j<M;j++){scanf("%d",&a[i][j]);sum1=sum1+a[i][j];}}printf("请输入一个%d*%d的矩阵\n",N,M);for(i=0;i<N;i++){for(j=0;j<M;j++){scanf("%d",&b[i][j]);sum2=sum2+a[i][j];}}sum=sum1+sum2;printf("sum=%d\n",sum);}8.12#include<stdio.h>#define N7void YangHui(int a[N][N],int n);void main(){int i,j,n,b[N][N];printf("Input n:");scanf("%d",&n);YangHui(b,n);for(i=0;i<n;i++){for(j=0;j<=i;j++){printf("%4d",b[i][j]);}printf("\n");}}void YangHui(int a[N][N],int n){int i,j;for(i=0;i<n;i++){a[i][0]=1;a[i][i]=1;}for(i=2;i<n;i++){for(j=1;j<i;j++)a[i][j]=a[i-1][j-1]+a[i-1][j];}}8.13#include<stdio.h>#define N12void main(){int i;int f[N]={1,1};printf("%d:%d\n",0,f[0]);printf("%d:%d\n",1,f[1]);for(i=2;i<N;i++){f[i]=f[i-1]+f[i-2];printf("%d:%d\n",i+1,f[i]);}}8.14#include<stdio.h>#include<stdlib.h>#include<time.h>#define N6000void main(){int a[N],i;int b1=0,b2=0,b3=0,b4=0,b5=0,b6=0;float p1,p2,p3,p4,p5,p6;srand(time(NULL));for(i=0;i<N;i++){a[i]=rand()%6+1;printf("%3d",a[i]);if(a[i]==1)b1++;if(a[i]==2)b2++;if(a[i]==3)b3++;if(a[i]==4)b4++;if(a[i]==5)b5++;if(a[i]==6)b6++;p1=(float)(b1/6000.0);p2=(float)(b2/6000.0);p3=(float)(b3/6000.0);p4=(float)(b4/6000.0);p5=(float)(b5/6000.0);p6=(float)(b6/6000.0);}printf("骰子出现1的概率:%.2f\n",p1);printf("骰子出现2的概率:%.2f\n",p2);printf("骰子出现3的概率:%.2f\n",p3);printf("骰子出现4的概率:%.2f\n",p4);printf("骰子出现5的概率:%.2f\n",p5);printf("骰子出现6的概率:%.2f\n",p6);}9.1(1)void Swap(int*x,int*y){int*pTemp;//*pTemp未进行初始化,*pTemp指向哪里位置*pTemp=*x;*x=*y;*y=*pTemp;}9.1(2)void Swap(int*x,int*y){int*pTemp;pTemp=x;x=y;y=pTemp;}//错误,x,y没有指向类型9.2#include<stdio.h>int main(){int a[]={1,2,3,4,5};int*p=a;printf("%d,%d,%d,%d,%d,%d,%d\n",*p,*(++p),(*p)++,*p,*p--,--(*p),*p);return0;}9.3#include<stdio.h>void Swap(int*x,int*y);int main(){int a[6]={1,2,3,4,5,6},b[6]={11,22,33,44,55,66};int i;printf("Before Array\n");for(i=0;i<6;i++){printf("%d\n",a[i]);}for(i=0;i<6;i++){printf("%d\n",b[i]);}for(i=0;i<6;i++){Swap(&a[i],&b[i]);}printf("After Array\n");for(i=0;i<6;i++){printf("%d\n",a[i]);}for(i=0;i<6;i++){printf("%d\n",b[i]);}return0;}void Swap(int*x,int*y){int temp;temp=*x;*x=*y;*y=temp;}9.4#include<stdio.h>#define N10void FindMaxMin(int a[],int*pMaxa,int*pMaxnum,int*pMina,int*pMinnum);int main(){int a[N];int i,maxa,mina,maxnum,minnum;printf("Input a:");for(i=0;i<10;i++){scanf("%d",&a[i]);}FindMaxMin(a,&maxa,&maxnum,&mina,&minnum);printf("maxa=%d,maxnum=%d,mina=%d,minnum=%d\n",maxa,maxnum,mina,minnum);return0;}void FindMaxMin(int a[],int*pMaxa,int*pMaxnum,int*pMina,int*pMinnum){int i;*pMaxa=a[0];*pMina=a[0];*pMaxnum=0;*pMinnum=0;for(i=0;i<10;i++){if(a[i]>*pMaxa){*pMaxa=a[i];*pMaxnum=i;}else if(a[i]<*pMina){*pMina=a[i];*pMinnum=i;}}}10.1void MYStrcpy(char*dstStr,const char*srcStr){while((*dstStr++=*srcStr++)!='\0'){}}10.2#include<stdio.h>main(){char*a="main(){char*a=%c%s%c;printf(a,34,a,34);}";printf(a,34,a,34);} 10.3(1)unsigned int MyStrlen(char*p)main(){unsigned int len;len=0;for(;*pa!='\0';pa++){len++;}return len;}10.3(2)unsigned int MyStrlen(char s[]){char*p=s;while(*p!='\0'){p++;}return(p-*p);}10.3(3)int MyStrcmp(char*p1,char*p2){for(;*p1==*p2;p1++,p2++){if(*p1=='\0')return-1;}return1;}10.3(4)int MyStrcmp(char s[],char t[]){int i;for(i=0;s[i]==t[i];i++){if(s[i]=='\0')return0;}return i;}10.3(5)#include<stdio.h>#include<string.h>int main(){char password[7]="secret";char userInput[81];printf("Input Password:");scanf("%s",userInput);if(strcmp(userInput,password)==0)printf("Coorrect password!Welcome to the system...\n");else if(strcmp(userInput,password)<0)printf("Invalid password!user input<password...\n");elseprintf("Invalid password!user input>password...\n");return0;}10.4#include<stdio.h>#define N24unsigned int CountLetter(char str[]);int main(){char a[N];printf("Input a letter:\n");gets(a);printf("The length of the letter is:%d\n",CountLetter(a));return0;}unsigned int CountLetter(char str[]){char*p=str;int c=0,flag=0;while(*p!='\0'){if(*p!='')flag=1;else if(flag==1){c++;flag=0;}p++;}return c+1;}12.1#include<stdio.h>typedef struct data{int year;int month;int day;}DATA;typedef struct work{char a[14];char b[10];char c[6];}WORK;typedef struct student{char studentName[10];char studentSex;DATA birthday;WORK profession;}STUDENT;int main(){STUDENT stu={"王刚",'M',{1991,5,19},{"信息工程系","学习人员","学生"}};printf("stu:%2s%3c%6d/%02d/%02d%4s,%4s,%4s\n",stu.studentName,stu.studentSex,st u.birthday.year,stu.birthday.month,stu.birthday.day,stu.profession.a,stu.profession.b,stu.profession.c);return0;}12.4#include<stdio.h>#include<string.h>#define N10typedef struct people{char peopleName[10];}PEOPLE;int main(){int sum1=0,sum2=0,sum3=0,sum4=0;int i;char name[N][N];PEOPLE stu[5]={"zhang","li","wang"};printf("Enter their name:\n");for(i=0;i<10;i++){gets(name[i]);if(strcmp(name[i],stu[0].peopleName)==0)sum1++;else if(strcmp(name[i],stu[1].peopleName)==0) sum2++;else if(strcmp(name[i],stu[2].peopleName)==0) sum3++;elsesum4++;}printf("stu:%4s%2d\n",stu[i].peopleName,sum[j]);printf("stu:%4s%2d\n",stu[1].peopleName,sum2);printf("stu:%4s%2d\n",stu[2].peopleName,sum3);printf("%2d\n",sum4);return0;}。
c语言程序设计教程课后题及模拟题参考答案
main()
{ int x,y;
scanf(“%d,%d”,&x,&y);
if (x*x+y*y>1000)
printf(“%d\n”,(x*x+y*y)/100);
else
printf(“%d\n”,x+y);
}
3.输入一个整数,判断它能否同时被3、5、7整除,并输出“yes”或“no”字样。
a1=a%10 ; a=a/10;
a2=a%10; a=a/10;
a3=a%10;
printf(“%d%d%d\n”, a1, a2, a3);
}
4.编写程序,读入3个double型数据,求它们的平均值,保留此平均值小数点后1位数,对小数点后第2位数进行四舍五入,最后输出结果。
#include "stdio.h"
7. (1)10,6,4 (2)6,9,15 (3)3,60,83
8. 55
习题3
3
1-5BDABC6-10ADCAC 11-12BB
3
1.3
2.0261
3.0x10
4. 2, 1互换a,b的值
5.6.6
6.–003
8.7
7.5.0,4,c=3<Enter>
8.i=10,j=20<Enter>
9.(1) 65
#include "stdio.h"
main()
{ float a, b, h, s;
a=10;b=20;
h=5;s=(a+b)*h/2;
printf("s=%f\n" , s );
《C语言程序设计》 课后习题答案 高等教育出版社
《C语言程序设计》课后习题答案高等教育出版社《C语言程序设计》课后习题答案高等教育出版社第一章:C语言概述1. C语言的特点C语言是一种以处理底层任务和系统编程为目标的高级编程语言。
其特点包括语法简洁、执行效率高、可移植性强等。
第二章:C语言基本数据类型1. C语言中的基本数据类型C语言中的基本数据类型包括整型、字符型、浮点型等。
整型可以进一步细分为有符号整型和无符号整型。
第三章:C语言运算符1. C语言中的运算符C语言中常见的运算符包括算术运算符、赋值运算符、关系运算符、逻辑运算符等。
这些运算符用于执行各种数学和逻辑操作。
第四章:C语言控制语句1. C语言中的条件语句C语言提供了if语句和switch语句来实现条件判断。
if语句用于执行基于布尔表达式的条件分支,而switch语句用于根据不同的值执行不同的代码块。
第五章:C语言函数1. C语言中的函数定义和调用函数是C语言中的基本模块,用于封装可重用的代码。
函数定义包括函数返回类型、函数名、参数列表和函数体等部分。
第六章:C语言数组1. C语言中的数组定义和使用数组是一组相同类型的数据元素的集合。
C语言中可以使用数组来存储和操作大量数据。
第七章:C语言指针1. C语言中的指针概念指针是一种变量,它存储了内存地址。
通过指针,可以直接访问和修改对应内存地址中的数据。
第八章:C语言字符串1. C语言中的字符串操作字符串是由一系列字符组成的数据类型。
C语言通过字符数组来表示和操作字符串。
第九章:C语言结构体1. C语言中的结构体定义和使用结构体是一种自定义的复合数据类型,它可以包含多个不同类型的成员变量。
第十章:C语言文件操作1. C语言中的文件读写操作文件操作是一种重要的数据输入和输出方式。
C语言提供了一系列函数来实现文件的读写操作。
总结:通过解答以上习题,我们可以更好地掌握C语言的各个方面,提升我们的编程能力和解决问题的能力。
希望本文对读者有所帮助。
c语言程序设计指导实验教程-高等教育出版社-实验四-参考答案文库
cÓïÑÔ³ÌÐòÉè¼ÆÖ¸µ¼ÊµÑé½Ì³Ì-¸ßµÈ½ÌÓý³ö°æÉç-ʵÑéËÄ-²Î¿¼´ð°¸ÎÄ¿â.txtÁ½ÈËÖ®¼äµÄ¸ÐÇé¾ÍÏñ֯ëÒ£¬½¨Á¢µÄʱºòÒ»ÕëÒ»Ïߣ¬Ð¡ÐĶøÂþ³¤£¬²ð³ýµÄʱºòÖ»ÒªÇáÇáÒ»À-¡£¡£¡£¡£ÊµÑé4.1.1#include <stdio.h>int main(void){int m,n,j,k;do{printf("Input m:");scanf("%d",&m);printf("Input n:");scanf("%d",&n);}while(m<0||n<0);j=m;while(j%n!=0)j=j+m;k=(m*n)/j;printf("×îС¹«±¶ÊýÊÇ%d\n×î´ó¹«±¶ÊýÊÇ%d\n",j,k);return 0;}ʵÑé4.1.2#include<stdio.h>#include<math.h>int main(void){int denominator,flag;double item,pi,eps;printf("Input eps:\a");scanf("%lf",&eps);flag=1;denominator=1;item=1.0;pi=0;while(fabs(item)>=eps){item=flag*1.0/denominator;pi=pi+item;flag=-flag;denominator=denominator+3;}printf("s=%.6f\n\a",pi);return 0;}ʵÑé4.1.3#include<stdio.h>int main(void){int num,sum;sum=0;printf("Input integers:\a");scanf("%d",&num);while(num>0){if(num%2!=0){sum=sum+num;}else{sum=sum;}scanf("%d",&num);}printf("The sum of the odd number is%d\n\a",sum);return 0;}ʵÑé4.1.4#include<stdio.h>int main (void){int n,count;printf("Input an integer:");scanf("%d",&n);count=0;if(n<0) n=-n;do{n=n/10;count++;}while(n!=0);printf("count=%d\n\a",count);return 0;}ʵÑé4.1.5#include<stdio.h>int main (void){int i,number,min,n;printf("Input n:");scanf("%d",&n);printf("Input numbers:");scanf("%d",&number);min=number;for(i=1;i<n;i++){scanf("%d",&number);if(min>number){min=number;}}printf("Min=%d\n\a",min);return 0;}ʵÑé4.1.6#include<stdio.h>int main(void){int n,count,sum,number;printf("Input an integer:");scanf("%d",&number);count=0;sum=0;if(number<0) n=-n;do{n=number%10;sum=sum+n;number=number/10;count++;}while(number!=0);printf("count=%d,sum=%d\n\a",count,sum);return 0;}ʵÑé4.1.7#include<stdio.h>int main(void){int i,n;double sum,demominator,numerator;printf("Input n:");scanf("%d",&n);sum=0;numerator=2;demominator=1;for(i=1;i<n;i++){sum=sum+numerator/demominator;numerator=numerator+demominator;demominator=numerator-demominator;}printf("s=%.2f\n\a",sum);return 0;ʵÑé4.1.8#include<stdio.h>int main(void){int a,n,i,partsum,sum;printf("Input a,n:");scanf("%d%d",&a,&n);sum=0;partsum=0;for(i=n;i>0;i--){partsum=a*i;sum=sum+partsum;a=a*10;}printf("s=%d\n\a",sum);return 0;}ʵÑé4.1.9#include<stdio.h>#include<math.h>int main (void){int n,i;double h,H,distance;printf("Input height:");scanf("%lf",&h);printf("Input n:");scanf("%d",&n);H=0;distance=h;for(i=1;i<=n;i++){distance=distance+2*(h/pow(2,i));}H=h/pow(2,n);printf("distance=%.1f\nheight=%.1f\n\a",distance,H);return 0;ʵÑé4.2.2#include<stdio.h>int main(void){int m,n,i,number,count;printf("Input m:");scanf("%d",&m);printf("Input n:");scanf("%d",&n);count=0;for(number=m;number<=n;number++){ for(i=2;i<=number/2;i++)if(number%i==0) break;if(i>number/2){printf("%d ",number);count++;if(count%6==0)printf("\n");}}printf("\n");return 0;}ʵÑé4.2.3#include<stdio.h>int main (void){int fen1,fen2,fen5,money,count;printf("Input money:");scanf("%d",&money);count=0;for(fen5=1;fen5<=money/5;fen5++){ for(fen2=1;fen2<=money/2;fen2++)for(fen1=1;fen1<=money;fen1++){if(5*fen5+2*fen2+1*fen1==money){printf("fen5:%d,fen2:%d,fen1:%d\n\a",fen5,fen2,fen1);count++;}}}printf("count=%d\n\a",count);return 0;}¡¢ÊµÑé4.2.4#include<stdio.h>int main(void){int m,n,i,number,a,b,c,n1;printf("Input m:");scanf("%d",&m);printf("Input n:");scanf("%d",&n);for(number=m;number<=n;number++){a=number%10;n1=number/10;b=n1%10;c=n1/10;if(a*a*a+b*b*b+c*c*c==number){printf("%6d(%d*%d*%d+%d*%d*%d+%d*%d*%d=%d)\n\a",number,a,a,a,b,b,b,c,c,c,numb er);}}printf("\n");return 0;}ʵÑé4.2.6#include<stdio.h>double prime(int number);int main(void){int m,n,i,number,count;printf("Input m:");scanf("%d",&m);printf("Input n:");scanf("%d",&n);count=0;for(number=m;number<=n;number++){if(number%2!=0) continue;else{for(i=3;i<=number-3;i++){if(prime(i)>0&&prime(number-i)>0)printf("%d=%d+%d ",number,i,number-i);count++;if(count%5==0)printf("\n");}}}printf("\n");return 0;}double prime(int number){int i;for(i=2;i<=number/2;i++){if(number%i==0)number=0;if(i>number/2)number=number;}return number;}。
高等教育出版社出版社c语言程序设计实践教程习题参考答案
高等教育出版社出版社C语言实践教程习题参考答案4.2 练习题p58-59一、选择题1~5.DBADC 6~7.AB二、填空题1./* */ 或// 2.主函数或mian()3.函数的首部和函数体4.编译和连接5.; 6.传统流程图和N-S图5.2 练习题p64-70一、选择题1~5.ADBBC 6~10.CDABB 11~15.DBBCB 16~20.BDDCD 21~25.BADCC 26~30 BCDAB二、填空题1.102,10 2.#define 符号常量常量3.x>20&&x<30||x<-100 4.15.n=1 6.-47.a=1,b= ,c=2 8.c=A9. n1=%d\nn2=%d\n10. (1)a+b>c&&a+c>b&&b+c>a (2)ch>='a'&&ch<='z'||ch>='A'&&ch<='Z'11.7 12.013.1111 0000 14.8,46.2 练习题p76-84一、选择题1~5.ACDCA 6~10.BADBC 11~15.AACBC 二、填空题1.10 2.y=1 x%i= =03.** * ** * * * ** * * * * * ** * * * ** * **4.1 5.136.. (cx=getchar( ))!=-1 front=cx; 7.m%n8.4 9.(1)*p (2)px=&x (3)py=&y 三、读程序,写结果1.-1 2.3,1,-1 3.a=16,y=604.x=12,y=4 5.59四、程序设计题1.#include <stdio.h>void main(){float a,b,c,min;scanf("%f,%f,%f",&a,&b,&c);min=a;if (min>b) min=b;if (min>c) min=c;printf("%f",min);}2.方法1:#include <stdio.h>void main(){int a,b,c;for (a=1;a<=9;a++)for (b=0;b<=9;b++)for (c=0;c<=9;c++)if ((a*a*a+b*b*b+c*c*c)==1099) printf("%d%d%d\n",a,b,c);}方法2:#include <stdio.h>void main(){int a,b,c,sum;for (a=100;a<=999;a++){sum=0;b=a;while(b!=0){c=b%10;sum+=c*c*c;b=b/10;}if(sum==1099)printf("%5d",a);}}3.#include <stdio.h>void main(){int i,j,k;for (i=1;i<=6;i++){for (j=1;j<=20-3*i;j++) printf(" ");for (k=1;k<=i;k++) printf("%3d",k);for (k=i-1;k>0;k--) printf("%3d",k);printf("\n");}}4.#include <stdio.h>void main(){float x,fmax,fmin;scanf("%f",&x);fmax=fmin=x;while (x>=0){if (x>fmax)fmax=x;elseif (x<fmin)fmin=x;scanf("%f",&x);}printf("\nmax=%f,min=%f",fmax,fmin);}7.2 练习题p89-96一、选择题1~5.BCCAD 6~10.CDDCD11~15.DDABC 16~17.BBAAD21~26.BDCDDC二、填空题1.(1)’a’(2)’\o’2.(1)a[k] (2)a[k] (3)a[k] 3.6 4.A B C D EA5.1,6 6.k=p;7.i<j 8.189.4,some string *test 10. you&me三、读程序,写结果1.5 4 8 2 0 2.10 24 15 63.love china! H w l4.66 66 555v. Front四、程序设计题1.#include <stdio.h>void main(){int i,j,a[10],k;for(i=0;i<=9;i++)scanf("%d",&a[i]);for(i=0,j=9;i<=j;i++,j--){k=a[i];a[i]=a[j];a[j]=k;}for(i=0;i<=9;i++)printf("%d",a[i]);}2.#include <stdio.h>#include <ctype.h>void main(){char i,a[80];scanf("%s",a);for(i=0;i<strlen(a);i++)if (islower(a[i]))a[i]=a[i]-32;elseif (isupper(a[i]))a[i]=a[i]+32;printf("%s",a);}3.#include <stdio.h>void main(){char a[80],i;gets(a);for(i=strlen(p)-1;i>=0;i--)putchar(a[i]);}4.#include <stdio.h>void main(){int a[3][6],i,j;int d,d1=0,d2=0,x,x1=0,x2=0;for(i=0;i<3;i++)for(j=0;j<6;j++){scanf("%d",&a[i][j]);if (i= =0&&j= =0) x=d=a[i][j];if (d<a[i][j) {d=a[i][j];d1=i;d2=j;}else if (x>a[i][j]) {x=a[i][j];x1=i;x2=j;}p++;}printf("d=%d,d1=%d,d2=%d\n",d,d1,d2);printf("x=5d,x1=%d,x2=%d\n",x,x1,x2);}5.#include <stdio.h>void main(){int a[10],i,d,d1=0,x,x1=0;for(i=0;i<10;i++,p++){scanf("%d",&a[i]);if (i= =0) {d=a[i];x=a[i];}if (d<a[i]) {d=a[i];d1=i;}else if (x>a[i]) {x=a[i];x1=i;}}a[d1]=a[0];a[0]=d;a[x1]=a[9];a[9]=x;for(i=9;i>=0;i--,p--)printf("%d,",*p);}8.2练习题p106-114一、选择题1~5.AABBB 6~10.ADCAA11~15.ABBAB 16~17.BC二、填空题1.12 2.(1)Itis3.(1)n*fun(n-1) (2)fun(k) 4.s[i]-t[i]5.(1)n%m (2)i-1 (3)x[d]; 6.(1)x=n; (2)n%10 (3) x 7.m= f(a,4)+f(b,4)-f(a+b,3) 8.a=1,b=1;三、读程序,写结果1.s=7 2.s=373.bij12四、程序设计题1.int nian(int y){if((y%4==0)&&(y%100!=0)||(y%400==0)) return 1;else return 0;}2.void fun(float a[],float b[]){int i;float s=0;b[0]=b[1]=a[0];for(i=0;i<n;i++){s=s+a[i];if (b[0]<a[i]) b[0=a[i];else if (b[1]>a[i]) b[1]=a[i];}b[2]=s/n;}3.int cout(char str[],char sub[]){int i,j,k;int count=0;for(i=0;str[i];i++)for(j=i,k=0;sub[k]= =str[j];k++,j++)if(sub[k+1]=='\0'){count++;break;}return count;}4.#include "stdio.h"void pp(int m){int j;if(j=m/10) pp(j);putchar(n%10+'0');}void main(){int n;scanf("%d",&n);if(n<0){putchar('-');n=-n;}pp(n);}5.#include "stdio.h"int add(int n){int r,s=0;do{r=n%10;s+=r;n=n/10;}while(n>0);return s;}void main(){int a;scanf("%d",&a);printf("%d", add(a));}6.#include <math.h>#include "stdio.h"void prime(int n){int i,f=1;for(i=2;i<=sqrt(n);i++)if(n%i= =0){f=0;break;}}void main(){int m;scanf("%d",&m);if(prime(m)) printf("%d", m);else printf("不是素数");}7.参考程序int total(char s[ ],char ch){int i=0,n=0;while(s[i])if(s[i++]==ch) n++;return(n);}#include "stdio.h"void main(){char s[20],ch;gets(s);ch=getchar();printf("%d", total(s,ch));}8.void move(char *x,int n,int m) {int i,j;for(j=0;j<m;j++){char w=*(x+n-1);for(i=0;i<n-1;i++)*(x+n-1-i)=*(x+n-2-i);*x=w;}}9.void print(char *string) {char *str=string;if(*str=='\0')return;while(*str)str++;putchar(*--str);*str='\0';print(string);}。
《C语言程序设计》(苏小红) 课后习题参考答案 高等教育出版社
printf("y=%f\n",y);printf("z=%f\n",z);}3.1(1)main(){}3.1(2)main(){}3.2main(){int x,b0,b1,b2,s;printf("Inputx:");scanf("%d",&x);b2=x/100;b1=(x-b2*100)/10;//或(x%100)/10;或x/10%10;b0=x%10;s=b0*100+b1*10+b2;printf("s=%d\n",s);}3.3#include<stdio.h>#include<math.h>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.4main(){}习题44.1(1)main(){}4.1(2)main(){}4.1(3)main(){}4.2#include<stdio.h>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.1main(){{}{}}5.2main(){intscanf("%d", &a);if(a%2==0){printf("a是偶数");}else{printf("a是奇数");}}5.3#include<stdio.h>#include<math.h>main(){float a,b,c,s,area;printf("Input a, b, c:");scanf("%f %f %f", &a, &b, &c);if(a+b>c&&a+c>b&&b+c>a){s=(a+b+c)/2;area=(float)sqrt(s*(s-a)*(s-b)*(s-c));printf("area=%f\n",area);}{}}5.4main(){{}{}else if(b*b-4*a*c==0){x=-b/(2*a);y=-b/(2*a);printf("x=%f,y=%f\n",x,y);}else{printf("该方程无实根\n");}}5.5#include<stdio.h>main(){int year,flag;printf("Input a year:");scanf("%d",&year);if(year%4==0&&year%400!=0||year%400==0) {flag=1;}else{}{}{}}5.6main(){int{}else{printf("%d is not a leap year!\n",year);}}5.7#include<stdio.h>main(){char ch;printf("Inputch:");scanf("%c",&ch);if(ch>='a'&&ch<='z'){ch=getchar();ch=ch-32;printf("%c,%d\n",ch,ch);}else if(ch>='A'&&ch<='Z'){ch=getchar();ch=ch+32;printf("%c,%d\n",ch,ch);}{}}5.8#include<stdio.h>main(){char ch;printf("Inputch:");scanf("%c",&ch);if(ch>=48&&ch<=57){printf("ch是数字字符\n");grade=score/10;if(score<0||score>100){printf("Input error\n");}if(score>=90&&score<=100){printf("%d--A\n",score);}else if(score>=80&&score<90){printf("%d--B\n",score);}else if(score>=70&&score<80){printf("%d--C\n",score);}else if(score>=60&&score<70){printf("%d--D\n",score);}case3:case1:printf("31天\n");break;case11:case9:case6:case4:printf("30天\n");break;case2:if(year%4==0&&year!=0||year%400==0){printf("29天\n");}else{printf("28天\n");}break;main(){int k=4,n;for(n=0;n<k;n++){if(n%2==0)continue;k--;}printf("k=%d\n,n=%d\n",k,n);}6.1(3)#include<stdio.h>main(){int k=4,n;for(n=0;n<k;n++){if(n%2==0)break;k--;}printf("k=%d,n=%d\n",k,n);main(){int n=1;float term=1.0,sign=1,sum=0;while(term<=-1e-4||term>=1e-4){term=1.0/sign;sum=sum+term;sign=sign+n;n++;}printf("sum=%f\n",sum);}6.2(5)#include<stdio.h>#include<math.h>main(){int n=1,count=1;float x;}}/*int x,find=0;for(x=1;!find;x++){if(x%2==1&&x%3==2&&x%5==4&&x%6==5&&x%7==0){printf("x=%d\n",x);find=1;}}}*/6.4#include<stdio.h>main(){int i,n;long p=1,m=1;printf("Input n:");scanf("%d",&n);x=x*pow(1+c,12)-1000;n++;}while(x>0);printf("x=%d\n",x);}6.7#include<stdio.h>main(){int n=0;float a=100.0,c;printf("Inputc:");scanf("%f",&c);do{a=a*(1+c);n++;}while(a<=200);printf("n=%d\n",n);}term=term*(1.0/n);sum=sum+term;n++;count++;}printf("sum=%f,count=%d\n",sum,count); }6.10#include<stdio.h>#include<math.h>main(){int x;for(x=100;x<=999;x++){if(x==pow(x/100,3)+pow(x/10%10,3)+pow(x%10,3))printf("x=%d\n",x);}}{sum=sum+m;count++;}else{break;}printf("sum=%d,count=%d\n",sum,count);}}6.13#include<stdio.h>main(){int i,n,m,count=0,sum=0;printf("Inputn:");scanf("%d",&n);for(i=1;i<=n;i++){printf("x=%d,y=%d,z=%d\n",x,y,z);}}}6.15#include<stdio.h>main(){int x,y;for(x=0;x<=98;x++){y=98-x;2*x+4*y==386;if(x+y==98&&2*x+4*y==386){printf("x=%d,y=%d\n",x,y);}}}6.16#include<stdio.h>{10*x+5*y+z==100;z=50-x-y;if(10*x+5*y+z==100&&x+y+z==50&&z>0){printf("x=%d,y=%d,z=%d\n",x,y,z);}}}}7.1#include<stdio.h>int Square(int i){return i*i;}int main(){int i=0;i=Square(i);hour=0;}void display(){printf("%d,%d,%d\n",hour,minute,second); }void delay(){int t;for(t=0;t<100000000;t++);}int main(){int i;void updaye(),display(),delay();for(i=0;i<1000000;i++){update();int main(){int a,b;printf("Inputa,b:");scanf("%d,%d",&a,&b);printf("%d\n",LCM(a,b));return0;}int LCM(int n,int m){int x;int find=0;for(x=1;!find;x++){if(x%n==0&&x%m==0){find=1;}long Fact(int n);int main(){int m;long ret;printf("Inputm:");scanf("%d",&m);ret=Fact(m);printf("ret=%d\n",ret);return0;}long Fact(int n){int i;long result=1,sum=0;for(i=2;i<=n;i++){result*=i;sum=sum+result;find=1;}7.7(2)#include<stdio.h> int Gcd(int a,int b);int main(){int m,n;printf("Inputm,n:");scanf("%d,%d",&m,&n);printf("%d\n",Gcd(m,n));return0;}int Gcd(int a,int b){int r,temp;r=a%b;if(r==0)return b;else{b[j]=j;}}int main(){static int a[]={5,6,7,8},i;Func(a);for(i=0;i<4;i++){printf("%d",a[i]);}return0;}8.2(1)int PositiveNum(int a[],int n) {int i,count=0;for(i=0;i<n;i++){{if(a[n]>max){max=a[n];maxPos=n;}else if(a[n]<min){min=a[n];minPos=n;}}printf("max=%d,pos=%d\n",max,maxPos);printf("min=%d,pos=%d\n",min,minPos);return0;}8.3void DivArray(int*pArray,int n){int i;count++;}return count;}void ReadScore(int score[],int n){int i;printf("Input score:");for(i=0;i<n;i++){scanf("%d",&score[i]);}}8.5#include<stdio.h>#define N40int HighAver(int score[],int n);int ReadScore(int score[]);int main(){printf("Input score:");scanf("%d",&score[i]);}while(score[i]>=0);return i;}8.6#include<stdio.h>#define N40int ReadScore(int score[],long num[]); int FindMax(int score[],long num[],int n);int main(){int score[N],n,max,num,t;long num[N];t=FindMax(score,n);max=score[t];num=num[t];printf("max=%d,num=%d\n",max,num);return0;}int main(){int a[N],n;printf("%d\n",ChangeArry(a,n));return0;}int Read(int a[]){int i;for(i=0;i<9;i++){printf("Input a:");scanf("%d",&a[i]);return i;}int ChangeArry(int a[],int n){int max,min,i,maxpos,minpos,term;max=min=score[0];for(j=0;j<N;j++){scanf("%d",&a[i][j]);}}for(i=0;i<N;i++){for(j=0;j<N;j++){if(i==j||i+j==N-1)sum=sum+a[i][j];}}printf("对角线元素之和为:%d\n",sum); }8.11#include<stdio.h>#define N2#define M3void main()#define N7void YangHui(int a[N][N],int n);void main(){int i,j,n,b[N][N];printf("Input n:");scanf("%d",&n);YangHui(b,n);for(i=0;i<n;i++){for(j=0;j<=i;j++){printf("%4d",b[i][j]);}printf("\n");}}void YangHui(int a[N][N],int n){}8.14#include<stdio.h>#include<stdlib.h>#include<time.h>#define N6000void main(){int a[N],i;int b1=0,b2=0,b3=0,b4=0,b5=0,b6=0;float p1,p2,p3,p4,p5,p6;srand(time(NULL));for(i=0;i<N;i++){a[i]=rand()%6+1;printf("%3d",a[i]);if(a[i]==1)b1++;if(a[i]==2)b2++;*pTemp=*x;*x=*y;*y=*pTemp;}9.1(2)void Swap(int*x,int*y) {int*pTemp;pTemp=x;x=y;y=pTemp;}//错误,x,y没有指向类型9.2#include<stdio.h>int main(){int a[]={1,2,3,4,5};int*p=a;printf("%d,%d,%d,%d,%d,%d,%d\n",*p,*(++p),(*p)++,*p,*p--,--(*p),*p);return0;for(i=0;i<6;i++){printf("%d\n",b[i]);}return0;}void Swap(int*x,int*y){int temp;temp=*x;*x=*y;*y=temp;}9.4#include<stdio.h>#define N10void FindMaxMin(int a[],int*pMaxa,int*pMaxnum,int*pMina,int*pMinnum); int main(){int a[N];{*pMina=a[i];*pMinnum=i;}}}10.1void MYStrcpy(char*dstStr,const char*srcStr){while((*dstStr++=*srcStr++)!='\0'){}}10.2#include<stdio.h>main(){char*a="main(){char*a=%c%s%c;printf(a,34,a,34);}";printf(a,34,a,34);} 10.3(1)unsigned int MyStrlen(char*p)main(){unsigned int len;{int i;for(i=0;s[i]==t[i];i++){if(s[i]=='\0')return0;}return i;}10.3(5)#include<stdio.h>#include<string.h>int main(){char password[7]="secret";char userInput[81];printf("Input Password:");scanf("%s",userInput);if(strcmp(userInput,password)==0)printf("Coorrect password!Welcome to the system...\n");else if(strcmp(userInput,password)<0){c++;flag=0;}p++;}return c+1;}12.1#include<stdio.h>typedef struct data{int year;int month;int day;}DATA;typedef struct work{char a[14];char b[10];int sum1=0,sum2=0,sum3=0,sum4=0;int i;char name[N][N];PEOPLE stu[5]={"zhang","li","wang"};printf("Enter their name:\n");for(i=0;i<10;i++){gets(name[i]);if(strcmp(name[i],stu[0].peopleName)==0) sum1++;else if(strcmp(name[i],stu[1].peopleName)==0) sum2++;else if(strcmp(name[i],stu[2].peopleName)==0) sum3++;elsesum4++;}printf("stu:%4s%2d\n",stu[i].peopleName,sum[j]);。
《C语言程序设计》(苏小红) 课后习题答案 高等教育出版社
2.2 #include<stdio.h>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<stdio.h>main(){int a=12,b=3;float x=18.5,y=4.6;printf("%f\n",(float)(a*b)/2);printf("%d\n",(int)x%(int)y);}3.1(2)#include<stdio.h>main(){int x=32,y=81,p,q;p=x++;q=--y;printf("%d %d\n",p,q); printf("%d %d\n",x,y); }3.2#include<stdio.h>main(){int x,b0,b1,b2,s;printf("Inputx:");scanf("%d",&x);b2=x/100;b1=(x-b2*100)/10;//或(x%100)/10;或x/10%10; b0=x%10;s=b0*100+b1*10+b2;printf("s=%d\n",s);}3.3#include<stdio.h>#include<math.h>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<stdio.h>#include<math.h>main(){float a, b, c;double x, y;printf("Input a, 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 Array 4.1(1)#include<stdio.h>main(){char c1='a',c2='b',c3='c';printf("a%cb%cc%c\n",c1,c2,c3);}4.1(2)main(){int a=12,b=15;printf("a=%d%%,b=%d%%\n",a,b);}#include<stdio.h>main(){int a,b;printf("%d,%d\n",a,b);}4.2#include<stdio.h>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<stdio.h>main(){float a;printf("Input a:");scanf("%f",&a);if(a>=0){a=a;printf("a=%f\n",a);}else{a=-a;printf("a=%f\n",a);}}5.2#include<stdio.h>main(){int a;printf("Input a:");scanf("%d", &a);if(a%2==0){printf("a是偶数");}else{printf("a是奇数");}}5.3#include<stdio.h>#include<math.h>main(){float a,b,c,s,area;printf("Input a, b, c:");scanf("%f %f %f", &a, &b, &c);if(a+b>c&&a+c>b&&b+c>a){s=(a+b+c)/2;area=(float)sqrt(s*(s-a)*(s-b)*(s-c));printf("area=%f\n",area);}else{printf("不是三角形");}}5.4#include<stdio.h>#include<math.h>main(){float a,b,c,x,y;printf("Inputa,b,c:");scanf("%f,%f,%f",&a,&b,&c);if(a==0){printf("该方程不是一元二次方程\n");}if(b*b-4*a*c>0){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);else if(b*b-4*a*c==0){x=-b/(2*a);y=-b/(2*a);printf("x=%f,y=%f\n",x,y);}else{printf("该方程无实根\n");}}5.5#include<stdio.h>main(){int year,flag;printf("Input a year:");scanf("%d",&year);if(year%4==0&&year%400!=0||year%400==0){flag=1;}else{flag=0;}if(flag==1){printf("%d is a leap year!\n",year);}else{printf("%d is not a leap year!\n",year);}}5.6#include<stdio.h>main(){int year,flag;printf("Input a year:");scanf("%d",&year);flag=year%400==0||year%4==0&&year%100!=0?1:0;if(flag==1&&flag!=0)printf("%d is a leap year!\n",year);}else{printf("%d is not a leap year!\n",year);}}5.7#include<stdio.h>main(){char ch;printf("Inputch:");scanf("%c",&ch);if(ch>='a'&&ch<='z'){ch=getchar();ch=ch-32;printf("%c,%d\n",ch,ch);}else if(ch>='A'&&ch<='Z'){ch=getchar();ch=ch+32;printf("%c,%d\n",ch,ch);}else{printf("%c",ch);}}5.8#include<stdio.h>main(){char ch;printf("Inputch:");scanf("%c",&ch);if(ch>=48&&ch<=57){printf("ch是数字字符\n");}else if(ch>=65&&ch<=90){printf("ch是大写字母\n");}else if(ch>=97&&ch<=122){printf("ch是小写字母\n");}else if(ch==32){printf("ch是空格\n");}else{printf("ch是其他字符\n");}}5.9#include<stdio.h>main(){int score,grade;printf("Input score:");scanf("%d",&score);grade=score/10;if(score<0||score>100){printf("Input error\n");}if(score>=90&&score<=100){printf("%d--A\n",score);}else if(score>=80&&score<90){printf("%d--B\n",score);}else if(score>=70&&score<80){printf("%d--C\n",score);}else if(score>=60&&score<70){printf("%d--D\n",score);}else if(score>=0&&score<60){printf("%d--E\n",score);}}5.10#include<stdio.h>main(){int year,month;printf("Input year,month:");scanf("%d,%d",&year,&month);if(month>12||month<=0){printf("error month\n");}else{switch(year,month){case12:case10:case8:case7:case5:case3:case1:printf("31天\n");break;case11:case9:case6:case4:printf("30天\n");break;case2:if(year%4==0&&year!=0||year%400==0){printf("29天\n");}else{printf("28天\n");}break;default:printf("Input error\n");}}}6.1(1)#include<stdio.h>main(){int i,j,k;char space='';for(i=1;i<=4;i++){for(j=1;j<=i;j++){printf("%c",space);}for(k=1;k<=6;k++){printf("*");}printf("\n");}}6.1(2)#include<stdio.h>main(){int k=4,n;for(n=0;n<k;n++){if(n%2==0)continue;k--;}printf("k=%d\n,n=%d\n",k,n);}6.1(3)#include<stdio.h>main(){int k=4,n;for(n=0;n<k;n++){if(n%2==0)break;k--;}printf("k=%d,n=%d\n",k,n);}6.2(1)#include<stdio.h>main(){int i,sum=0;for(i=1;i<=101;i++){sum=sum+i;}printf("sum=%d\n",sum);}6.2(2)#include<stdio.h>main(){long i;long term,sum=0;for(i=1;i<=101;i=i+2){term=i*(i+1)*(i+2);sum=sum+term;}printf("sum=%ld\n",sum);}6.2(4)#include<stdio.h>#include<math.h>main(){int n=1;float term=1.0,sign=1,sum=0;while(term<=-1e-4||term>=1e-4){term=1.0/sign;sum=sum+term;sign=sign+n;n++;}printf("sum=%f\n",sum);}6.2(5)#include<stdio.h>#include<math.h>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)=%f,count=%d\n",sum,count);}6.3#include<stdio.h>main(){int x=1,find=0;while(!find){if(x%2==1&&x%3==2&&x%5==4&&x%6==5&&x%7==0){printf("x=%d\n",x);find=1;x++;}}}/*int x,find=0;for(x=1;!find;x++){if(x%2==1&&x%3==2&&x%5==4&&x%6==5&&x%7==0){printf("x=%d\n",x);find=1;}}}*/6.4#include<stdio.h>main(){int i,n;long p=1,m=1;printf("Input n:");scanf("%d",&n);for(i=1;i<=n;i++){p=i*i;m=i*i*i;printf("p=%d,m=%d\n",i,p,i,m);}}6.5#include<stdio.h>main(){float c,f;for(c=-40;c<=110;c=c+10){f=9/5*c+32;printf("f=%f\n",f);}}6.6#include<stdio.h>#include<math.h>main(){int n;double c=0.01875,x;do{x=x*pow(1+c,12)-1000;n++;}while(x>0);printf("x=%d\n",x);}6.7#include<stdio.h>main(){int n=0;float a=100.0,c;printf("Inputc:");scanf("%f",&c);do{a=a*(1+c);n++;}while(a<=200);printf("n=%d\n",n);}6.8#include<stdio.h>#include<math.h>main(){int n=1,count=1;double sum=1,term=1;while(fabs(term)>=1e-5){term=pow(-1,count)*(1.0/(n+2));sum=sum+term;n=n+2;count++;}sum=4*sum;printf("sum=%f,count=%d\n",sum,count); }6.9#include<stdio.h>#include<math.h>main(){int n=1,count=1;double sum=1,term=1;while(fabs(term)>=1e-5){term=term*(1.0/n);sum=sum+term;n++;count++;}printf("sum=%f,count=%d\n",sum,count);}6.10#include<stdio.h>#include<math.h>main(){int x;for(x=100;x<=999;x++){if(x==pow(x/100,3)+pow(x/10%10,3)+pow(x%10,3))printf("x=%d\n",x);}}6.11#include<stdio.h>main(){int i=0,n;long sum=0,term=1;printf("Inputn:");scanf("%d",&n);do{i++;term=term*i;sum=sum+term;}while(sum<n);printf("%d\n",i-1);}6.12#include<stdio.h>main(){int i,n,m,count=0,sum=0;printf("Input n:");scanf("%d",&n);for(i=1;i<=n;i++){printf("Inputm:");scanf("%d",&m);if(m>0){sum=sum+m;count++;}else{break;}printf("sum=%d,count=%d\n",sum,count);}}6.13#include<stdio.h>main(){int i,n,m,count=0,sum=0;printf("Inputn:");scanf("%d",&n);for(i=1;i<=n;i++){printf("Inputm:");scanf("%d",&m);if(m>0||m<0){sum=sum+m;count++;}else{break;}printf("sum=%d,count=%d\n",sum,count);}}6.14#include<stdio.h>main(){int x,y,z;for(x=0;x<=17;x++){for(y=0;y<=25;y++){3*x+2*y+z==50;z=30-x-y;if(3*x+2*y+z==50&&x+y+z==30)printf("x=%d,y=%d,z=%d\n",x,y,z);}}}6.15#include<stdio.h>main(){int x,y;for(x=0;x<=98;x++){y=98-x;2*x+4*y==386;if(x+y==98&&2*x+4*y==386){printf("x=%d,y=%d\n",x,y);}}}6.16#include<stdio.h>main(){int x,y,z;for(x=0;x<=20;x++){for(y=0;y<=33;y++){3*y+5*x+z/3.0==100;z=100-x-y;if(5*x+3*y+z/3.0==100&&z+x+y==100){printf("x=%d,y=%d,z=%d\n",x,y,z);}}}}6.17#include<stdio.h>main(){int x,y,z;for(x=1;x<=9;x++){for(y=1;y<=17;y++){10*x+5*y+z==100;z=50-x-y;if(10*x+5*y+z==100&&x+y+z==50&&z>0){printf("x=%d,y=%d,z=%d\n",x,y,z);}}}}7.1#include<stdio.h>int Square(int i){return i*i;}int main(){int i=0;i=Square(i);for(;i<3;i++){static int i=1;i+=Square(i);printf("%d,",i);}printf("%d\n",i);return0;}7.2#include<stdio.h>int hour,minute,second;void update(){second++;if(second==60){second=0;minute++;}if(minute==60){minute=0;hour++;}if(hour==24)hour=0;}void display(){printf("%d,%d,%d\n",hour,minute,second); }void delay(){int t;for(t=0;t<100000000;t++);}int main(){int i;void updaye(),display(),delay();for(i=0;i<1000000;i++){update();display();delay();}return0;}7.3#include<stdio.h>int GetMax(int a,int b);int main(){int x,y,max;printf("Inputx,y:");scanf("%d,%d",&x,&y);max=GetMax(x,y);printf("max=%d\n",max);return0;}int GetMax(int m,int n){if(m>=n)return m;elsereturn n;}7.4#include<stdio.h>int LCM(int n,int m);int main(){int a,b;printf("Inputa,b:");scanf("%d,%d",&a,&b);printf("%d\n",LCM(a,b));return0;}int LCM(int n,int m){int x;int find=0;for(x=1;!find;x++){if(x%n==0&&x%m==0){find=1;}}return x-1;}7.5#include<stdio.h>long Fact(int n);int main(){int m,a;printf("Inputm:");scanf("%d",&m);for(a=1;a<=m;a++){printf("%d!=%ld\n",a,Fact(a));}return0;}long Fact(int n){int i;long result=1;for(i=2;i<=n;i++)result*=i;return result;}7.6#include<stdio.h>long Fact(int n);int main(){int m;long ret;printf("Inputm:");scanf("%d",&m);ret=Fact(m);printf("ret=%d\n",ret);return0;}long Fact(int n){int i;long result=1,sum=0;for(i=2;i<=n;i++){result*=i;sum=sum+result;}return sum;}7.7(1)#include<stdio.h>int Gcd(int a,int b);int main(){int m,n;printf("Inputm,n:");scanf("%d,%d",&m,&n);printf("%d\n",Gcd(m,n));return0;}int Gcd(int a,int b){int t,min,find=0;min=a<b?a:b;t=min;for(t=min;!find;t--){a%t==0;b%t==0;if(a%t==0&&b%t==0)return t;}find=1;}7.7(2)#include<stdio.h> int Gcd(int a,int b);int main(){int m,n;printf("Inputm,n:");scanf("%d,%d",&m,&n);printf("%d\n",Gcd(m,n));return0;}int Gcd(int a,int b){int r,temp;r=a%b;if(r==0)return b;elsedo{temp=b;b=r;a=temp;r=a%b;}while(r!=0);return b;}8.1(1)#include<stdio.h> void Func(int x){x=2;}int main(){int x=10;Func(x);printf("%d",x);return0;}8.1(2)#include<stdio.h> void Func(int b[]){int j;for(j=0;j<4;j++){b[j]=j;}}int main(){static int a[]={5,6,7,8},i;Func(a);for(i=0;i<4;i++){printf("%d",a[i]);}return0;}8.2(1)int PositiveNum(int a[],int n) {int i,count=0;for(i=0;i<n;i++){if(a[i]>0)count++;}return0;}8.2(2)void Fib(long f[],long n){int i;f[0]=0;f[1]=1;for(i=2;i<n;i++){f[i]=f[i-1]+f[i-2];}}8.2(3)#include<stdio.h>int main(){int a[10],n,max,min,maxPos,minPos;for(n=0;n<10;n++){scanf("%d",&a[n]);}max=min=a[0];maxPos=minPos=0;for(n=0;n<10;n++){if(a[n]>max){max=a[n];maxPos=n;}else if(a[n]<min){min=a[n];minPos=n;}}printf("max=%d,pos=%d\n",max,maxPos);printf("min=%d,pos=%d\n",min,minPos);return0;}8.3void DivArray(int*pArray,int n){int i;for(i=0;i<n;i++){pArray[i]/=pAttay[0];}}8.4#include<stdio.h>#define N40int FailNum(int score[],int n);void ReadScore(int score[],int n);int main(){int score[N],n;printf("Input n:");scanf("%d",&n);ReadScore(score,n);FailNum(score,n);printf("FailNum students are%d\n",FailNum(score,n));return0;}int FailNum(int score[],int n){int i,count=0;for(i=0;i<n;i++){if(score[i]<60)count++;}return count;}void ReadScore(int score[],int n){int i;printf("Input score:");for(i=0;i<n;i++){scanf("%d",&score[i]);}}8.5#include<stdio.h>#define N40int HighAver(int score[],int n);int ReadScore(int score[]);int main(){int score[N],n;n=ReadScore(score);printf("HighAver students are%d\n",HighAver(score,n));return0;}int HighAver(int score[],int n){int i,count=0,sum=0,aver;for(i=0;i<n;i++){sum=sum+score[i];aver=sum/n;}for(i=0;i<n;i++){if(score[i]>aver)count++;}return count;}int ReadScore(int score[]){int i=-1;do{i++;printf("Input score:");scanf("%d",&score[i]);}while(score[i]>=0);return i;}8.6#include<stdio.h>#define N40int ReadScore(int score[],long num[]);int FindMax(int score[],long num[],int n);int main(){int score[N],n,max,num,t;long num[N];t=FindMax(score,n);max=score[t];num=num[t];printf("max=%d,num=%d\n",max,num);return0;}int ReadScore(int score[],long num[]){int i=-1;do{i++;printf("Input student's ID and score:");scanf("%ld%d",&num[i],&score[i]);}while(num[i]>0&&score[i]>=0);return i;}int FindMax(int score[],long num[],int n){int max,i;max=score[0];for(i=1;i<n;i++){if(score[i]>max)max=score[i];}return i;}8.7#include<stdio.h>#define N40int Read(int a[]);int ChangeArry(int a[],int n);int main(){int a[N],n;printf("%d\n",ChangeArry(a,n));return0;}int Read(int a[]){int i;for(i=0;i<9;i++){printf("Input a:");scanf("%d",&a[i]);return i;}int ChangeArry(int a[],int n){int max,min,i,maxpos,minpos,term;max=min=score[0];for(i=1;i<n;i++){if(a[i]>max)max=score[i];maxpos=n;else(a[i]<min)min=a[i];minpos=n;temp=maxpos;maxpos=minpos;minpos=temp;}return a[i];}8.10#include<stdio.h>#define N5void main(){int a[N][N];int sum=0;int i,j;printf("请输入一个%d*%d的矩阵\n",N,N);for(i=0;i<N;i++){for(j=0;j<N;j++){scanf("%d",&a[i][j]);}}for(i=0;i<N;i++){for(j=0;j<N;j++){if(i==j||i+j==N-1)sum=sum+a[i][j];}}printf("对角线元素之和为:%d\n",sum);}8.11#include<stdio.h>#define N2#define M3void main(){int a[N][M],b[N][M];int i,j,sum1=0,sum2=0,sum=0;printf("请输入一个%d*%d的矩阵\n",N,M);for(i=0;i<N;i++){for(j=0;j<M;j++){scanf("%d",&a[i][j]);sum1=sum1+a[i][j];}}printf("请输入一个%d*%d的矩阵\n",N,M);for(i=0;i<N;i++){for(j=0;j<M;j++){scanf("%d",&b[i][j]);sum2=sum2+a[i][j];}}sum=sum1+sum2;printf("sum=%d\n",sum);}8.12#include<stdio.h>#define N7void YangHui(int a[N][N],int n);void main(){int i,j,n,b[N][N];printf("Input n:");scanf("%d",&n);YangHui(b,n);for(i=0;i<n;i++){for(j=0;j<=i;j++){printf("%4d",b[i][j]);}printf("\n");}}void YangHui(int a[N][N],int n){int i,j;for(i=0;i<n;i++){a[i][0]=1;a[i][i]=1;}for(i=2;i<n;i++){for(j=1;j<i;j++)a[i][j]=a[i-1][j-1]+a[i-1][j];}}8.13#include<stdio.h>#define N12void main(){int i;int f[N]={1,1};printf("%d:%d\n",0,f[0]);printf("%d:%d\n",1,f[1]);for(i=2;i<N;i++){f[i]=f[i-1]+f[i-2];printf("%d:%d\n",i+1,f[i]);}}8.14#include<stdio.h>#include<stdlib.h>#include<time.h>#define N6000void main(){int a[N],i;int b1=0,b2=0,b3=0,b4=0,b5=0,b6=0;float p1,p2,p3,p4,p5,p6;srand(time(NULL));for(i=0;i<N;i++){a[i]=rand()%6+1;printf("%3d",a[i]);if(a[i]==1)b1++;if(a[i]==2)b2++;if(a[i]==3)b3++;if(a[i]==4)b4++;if(a[i]==5)b5++;if(a[i]==6)b6++;p1=(float)(b1/6000.0);p2=(float)(b2/6000.0);p3=(float)(b3/6000.0);p4=(float)(b4/6000.0);p5=(float)(b5/6000.0);p6=(float)(b6/6000.0);}printf("骰子出现1的概率:%.2f\n",p1);printf("骰子出现2的概率:%.2f\n",p2);printf("骰子出现3的概率:%.2f\n",p3);printf("骰子出现4的概率:%.2f\n",p4);printf("骰子出现5的概率:%.2f\n",p5);printf("骰子出现6的概率:%.2f\n",p6);}9.1(1)void Swap(int*x,int*y){int*pTemp;//*pTemp未进行初始化,*pTemp指向哪里位置*pTemp=*x;*x=*y;*y=*pTemp;}9.1(2)void Swap(int*x,int*y){int*pTemp;pTemp=x;x=y;y=pTemp;}//错误,x,y没有指向类型9.2#include<stdio.h>int main(){int a[]={1,2,3,4,5};int*p=a;printf("%d,%d,%d,%d,%d,%d,%d\n",*p,*(++p),(*p)++,*p,*p--,--(*p),*p);return0;}9.3#include<stdio.h>void Swap(int*x,int*y);int main(){int a[6]={1,2,3,4,5,6},b[6]={11,22,33,44,55,66};int i;printf("Before Array\n");for(i=0;i<6;i++){printf("%d\n",a[i]);}for(i=0;i<6;i++){printf("%d\n",b[i]);}for(i=0;i<6;i++){Swap(&a[i],&b[i]);}printf("After Array\n");for(i=0;i<6;i++){printf("%d\n",a[i]);}for(i=0;i<6;i++){printf("%d\n",b[i]);}return0;}void Swap(int*x,int*y){int temp;temp=*x;*x=*y;*y=temp;}9.4#include<stdio.h>#define N10void FindMaxMin(int a[],int*pMaxa,int*pMaxnum,int*pMina,int*pMinnum);int main(){int a[N];int i,maxa,mina,maxnum,minnum;printf("Input a:");for(i=0;i<10;i++){scanf("%d",&a[i]);}FindMaxMin(a,&maxa,&maxnum,&mina,&minnum);printf("maxa=%d,maxnum=%d,mina=%d,minnum=%d\n",maxa,maxnum,mina,minnum);return0;}void FindMaxMin(int a[],int*pMaxa,int*pMaxnum,int*pMina,int*pMinnum){int i;*pMaxa=a[0];*pMina=a[0];*pMaxnum=0;*pMinnum=0;for(i=0;i<10;i++){if(a[i]>*pMaxa){*pMaxa=a[i];*pMaxnum=i;}else if(a[i]<*pMina){*pMina=a[i];*pMinnum=i;}}}10.1void MYStrcpy(char*dstStr,const char*srcStr){while((*dstStr++=*srcStr++)!='\0'){}}10.2#include<stdio.h>main(){char*a="main(){char*a=%c%s%c;printf(a,34,a,34);}";printf(a,34,a,34);} 10.3(1)unsigned int MyStrlen(char*p)main(){unsigned int len;len=0;for(;*pa!='\0';pa++){len++;}return len;}10.3(2)unsigned int MyStrlen(char s[]){char*p=s;while(*p!='\0'){p++;}return(p-*p);}10.3(3)int MyStrcmp(char*p1,char*p2){for(;*p1==*p2;p1++,p2++){if(*p1=='\0')return-1;}return1;}10.3(4)int MyStrcmp(char s[],char t[]){int i;for(i=0;s[i]==t[i];i++){if(s[i]=='\0')return0;}return i;}10.3(5)#include<stdio.h>#include<string.h>int main(){char password[7]="secret";char userInput[81];printf("Input Password:");scanf("%s",userInput);if(strcmp(userInput,password)==0)printf("Coorrect password!Welcome to the system...\n");else if(strcmp(userInput,password)<0)printf("Invalid password!user input<password...\n");elseprintf("Invalid password!user input>password...\n");return0;}10.4#include<stdio.h>#define N24unsigned int CountLetter(char str[]);int main(){char a[N];printf("Input a letter:\n");gets(a);printf("The length of the letter is:%d\n",CountLetter(a));return0;}unsigned int CountLetter(char str[]){char*p=str;int c=0,flag=0;while(*p!='\0'){if(*p!='')flag=1;else if(flag==1){c++;flag=0;}p++;}return c+1;}12.1#include<stdio.h>typedef struct data{int year;int month;int day;}DATA;typedef struct work{char a[14];char b[10];char c[6];}WORK;typedef struct student{char studentName[10];char studentSex;DATA birthday;WORK profession;}STUDENT;int main(){STUDENT stu={"王刚",'M',{1991,5,19},{"信息工程系","学习人员","学生"}};printf("stu:%2s%3c%6d/%02d/%02d%4s,%4s,%4s\n",stu.studentName,stu.studentSex,st u.birthday.year,stu.birthday.month,stu.birthday.day,stu.profession.a,stu.profession.b,stu.profession.c);return0;}12.4#include<stdio.h>#include<string.h>#define N10typedef struct people{char peopleName[10];}PEOPLE;int main(){int sum1=0,sum2=0,sum3=0,sum4=0;int i;char name[N][N];PEOPLE stu[5]={"zhang","li","wang"};printf("Enter their name:\n");for(i=0;i<10;i++){gets(name[i]);if(strcmp(name[i],stu[0].peopleName)==0)sum1++;else if(strcmp(name[i],stu[1].peopleName)==0) sum2++;else if(strcmp(name[i],stu[2].peopleName)==0) sum3++;elsesum4++;}printf("stu:%4s%2d\n",stu[i].peopleName,sum[j]);printf("stu:%4s%2d\n",stu[1].peopleName,sum2);printf("stu:%4s%2d\n",stu[2].peopleName,sum3);printf("%2d\n",sum4);return0;}。
C语言程序设计(高教版何钦铭主编)课后答案
前7章绝大部分都有。
水平有限(只学了3三个月),仅供参考。
/*计算2^i的前n项和*/#incl ude<s tdio.h>#i nclud e<mat h.h>int m ain(v oid){in t i,n;do ublesum;prin tf("E ntern:");sca nf("%d",&n);s um=0;for(i=1;i<=n;i++){su m=sum+pow(2,i);}print f("su m=%.0f\n",sum);ret urn 0;}/*输入若干个学生的成绩,计算平均分,并统计个等级成绩的个数。
成绩分为5个等级:分别为A(90~100)、B(80~89)、C(70~79)、D(60~69)、E(0~59)*/#in clude<stdi o.h>int m ain(v oid){in t gra de,A,B,C,D,E,n;dou ble t otal;pri ntf("Enter grad e:\n");s canf("%d",&grad e);A=B=C=D=E=0;t otal=0;n=0;while(grad e>=0){i f(gra de<=59)E++;el se if(grad e<=69)D++;els e if(grade<=79)C++;else if(g rade<=89)B++;elseA++;n++;tot al=to tal+g rade;sc anf("%d",&grade);}if(n==0){p rintf("gra de av erage=0\n");print f("A=0\nB=0\nC=0\nD=0\nE=0\n");}else{p rintf("gra de av erage=%.2f\n",t otal/n);prin tf("A=%d\n B=%d\nC=%d\nD=%d\nE=%d\n",A,B,C,D,E);}ret urn 0;}/*输入职工的月薪sala ry,输出应交的个人所得税ta x*/#inclu de<st dio.h>int main(void){int s alary;do ubletax;prin tf("E ntersalar y:");sca nf("%d",&s alary);i f(sal ary<=850)tax=0;elseif(sa lary<=1350)t ax=5.0*(sa lary-850)/100;else if(s alary<=2850)tax=10.0*(salar y-850)/100;el se if(sala ry<=5850)tax=15.0*(sal ary-850)/100;elsetax=20.0*(sal ary-850)/100;print f("ta x=%.2f\n",tax);ret urn 0;}/*计算1-2/3+3/5-4/7+5/9-6/11+…*/#i nclud e<std io.h>intmain(void){i nt i,n,fz,fm,fl ag; /*fz分子,fm分母,flag符号*/doubl e ite m,sum; /*item是第n项的值,sum存放计算结果*/print f("En ter n:");scan f("%d",&n);fz=fm=f lag=1;su m=0;for(i=1;i<=n;i++){ite m=1.0*flag*fz/f m;sum=s um+it em;fz++;f m=fm+2;flag=-flag;}prin tf("s um=%.2f\n",sum);re turn0;}/*输出21世纪所有的闰年。
普通高等教育教材——C语言程序设计教程——课后习题答案
else
printf("you input number is error!");
}
9、void main()
{
int n;
printf("please input the number:");
scanf("%d",&n);
if(n>=100 && n <= 999)
printf("%d%d%d",n%10,(n/10)%10,n/100);
6、scanf("%d,%d,%c,%c",&a1,&a2,&c1,&c2);
7、printf("a+b=%d\n",a+b);
printf("a-b=%d\n",a-b);
printf("a*b=%d\n",a*b);
printf("a/b=%d\n",a/b);
printf("(float)a/b=%f\n",(float)a/b);
不合法标识符:a*b2、8stu、D.K.Jon、if、ave#xy、#_DT5、C.D
5、F2:将当前编辑器中文件存盘
F10:调用主菜单
F4:程序运行到光标所在行
Ctrl+F9:当前编辑环境下,进行编译、连接且运行程序;
Alt+F5:将窗口切换到DOS下,查看程序运行结果
6、
(1):
********************
(11):0
(12):20
(13):0
6、(3)、(5)
c语言程序设计指导实验教程-高等教育出版社-参考答案-实验三
printf("The number of C (70-79):%d\n\a",C);
printf("The number of D (60-69):%d\n\a",D);
printf("The number of E (0-59):%d\n\a",E);
return 0;
}
}
3.7
#include<stdio.h>
#include<math.h>
int main (void)
{
int a,b,c,i;
double L,s;
float area;
for(i=1;i<=100;i++){
printf("Enter 3 sides of the triangle:");
return 0;
}
3.4
#include <stdio.h>
int main (void)
{
char ch;
int i;
for(i=1;i<=5;i++){
printf("Input Grade(A-E):");
ch=getchar();
switch(ch){
default:price=0.0;break;
}
printf("price=%.2lf\n\a",price);
}
printf("Thanks\n\a");
c语言程序设计教程(第2版)课后题及模拟题参考答案
c语⾔程序设计教程(第2版)课后题及模拟题参考答案c语⾔程序设计教程(第2版)课后题及模拟题参考答案习题1 (4)1-1 填空题 (4)1-2 思考题 (4)1-3 编程题 (5)习题2 (6)2-1 单选题 (6)2-2 思考题 (6)习题3 (7)3-1 选择题 (7)3-2 填空题 (7)3-3 编程题 (8)习题4 (11)4-1单选题 (11)4-2填空题 (11)4-3 编程题 (11)习题5 (16)5-1单选题 (16)5-2填空题 (16)5-3 编程题 (16)习题6 (22)6-1单选题 (22)6-2填空题 (22)习题7 (25)7-1单选题 (25)7-2填空题 (25)7-3 编程题 (25)习题8 (26)8-1单选题 (26)8-2填空题 (26)8-3 编程题 (26)习题9 (30)9-1单选题 (30)9-2填空题 (30)9-3 编程题 (30)习题10 (38)10-1单选题 (38)10-2填空题 (38)10-3 编程题 (38)习题11 (41)11-1单选题 (41)11-2填空题 (41)习题12 (42)12-1单选题 (42)12-2 填空题 (42)实验1 熟悉Visual C++6.0可视化集成开发环境 (43)实验2 顺序结构程序设计 (43)实验3 选择结构程序设计 (43)实验4 循环结构程序设计 (44)实验5 函数 (47)实验6 数组 (54)实验7 指针 (58)实验8 结构体和共⽤体 (61)实验9 ⽂件 (63)实验10 综合编程 (64)模拟试卷(⼀)参考答案 (65)模拟试卷(⼆)参考答案 (67)习题11-1 填空题1. 函数2. 主函数main(),主函数main()3. 主函数main()4. 函数⾸部,函数体5. {, }6. /*, */7. 顺序结构,选择结构,循环结构8. .c, .obj, .exe1-2 思考题1. 结构化程序设计是指:为使程序具有⼀个合理的结构以保证程序正确性⽽规定的⼀套如何进⾏程序设计的原则。
《C语言程序设计》(苏小红) 课后习题参考答案 高等教育出版社
《C语言程序设计》(苏小红) 课后习题参考答案高等教育出版社《C语言程序设计》(苏小红) 课后习题参考答案一、基础知识题1. C语言是一种通用的、面向过程的程序设计语言。
它在20世纪70年代由贝尔实验室的Dennis M. Ritchie开发。
2. C程序的基本组成部分有:预处理指令、函数、变量和语句。
3. C语言的数据类型包括:整型、浮点型、字符型、枚举型和指针型等。
4. C语言的运算符包括:算术运算符、关系运算符、逻辑运算符和位运算符等。
5. C语言的控制语句包括:顺序结构、分支结构和循环结构等。
6. C语言的函数由函数头和函数体组成,其中函数头包括函数的返回类型、函数名和参数列表。
7. C语言的数组是一种存储相同类型数据元素的连续存储区域。
8. C语言中的字符串是由字符组成的字符数组。
二、程序设计题1. 编写一个程序,输入一个整数n,计算并输出1到n之间的所有正整数的平方和。
```c#include<stdio.h>int main() {int n;int sum = 0;printf("请输入一个整数n:");scanf("%d", &n);for (int i = 1; i <= n; i++) {sum += i * i;}printf("1到%d之间的所有正整数的平方和为:%d\n", n, sum);return 0;}```2. 编写一个程序,输入一个正整数n,判断该数是否为素数(只能被1和自身整除)。
```c#include<stdio.h>int isPrime(int n) {if (n <= 1)return 0;for (int i = 2; i <= n / 2; i++) {if (n % i == 0)return 0;}return 1;}int main() {int n;printf("请输入一个正整数n:"); scanf("%d", &n);if (isPrime(n))printf("%d是素数\n", n);elseprintf("%d不是素数\n", n);return 0;}```3. 编写一个程序,输入一个正整数n,输出n的阶乘。
C语言程序设计实例教程(第三版)练习题答案1
练习1一、选择题1、一个C程序是由(B)。
A.一个主程序和若干子程序组成B.一个或多个函数组成C.若干过程组成D.若干子程序组成解析:C语言称为函数式语言,其程序是由至少一个主函数main,和其他更多的函数组成,答案选B;2、一个C程序的执行是从( A )。
A.main函数开始,直到main函数结束B.第一个函数开始,直到最后一个函数结束C.第一个语句开始,直到最后一个语句结束D.main函数开始,直到最后一个函数结束解析:C语言执行都是从主函数main函数开始,不论main函数放置的位置,答案选A;3、C语言语句的结束符是(B)。
A.回车符B.分号C.句号D.逗号解析:C语言语句是以分号结束,答案选B;4、下面标识符中( D )不是C语言的关键字。
A.char B.goto C.case D.include解析:char 声明字符型变量关键字,case 为switch结构中开关语句分支关键字,goto为无条件跳转语句关键字,在C语言中include不属于关键字,属于预处理命令,故答案选D;5、以下说法正确的是(A)A.C程序的注释可以出现在程序的任何位置,它对程序的编译和运行不起任何作用。
B.C程序的注释只能是一行。
C.C程序的注释不能是中文文字信息。
D.C程序的注释中存在的错误会被编译器检查出来。
解析:在C程序中注释只是起到增加程序的可读性,编译器并不会对注释进行检查,且C程序中存在多行注释。
故答案选A6、以下说法正确的是(D)A.C程序中的所有标识符都必须小写B.C程序中关键字必须小写,其他标识符不区分大小写C.C程序中所有标识符都不区分大小写D.C程序中关键字必须小写,其他标识符区分大小写。
解析:C程序中的关键字均是小写定义的,且标识符定义可以使用大写和小写,但是严格区分大小写;故答案选D。
7、设x、y均为float型变量,则以下不合法的赋值语句是(B)A.++x ; B.y=(x%2)/10 ; C.x*=y+8; D.x=y=0 ;解析:运算符%要求两端的操作数必须为整型,而在答案B中%号两端的操作数为x和2,x为实型,所以是错误的。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
附录习题参考答案附录习题参考答案方法2:#include <stdio.h>void main(){int a,b,c,sum;for (a=100;a<=999;a++){sum=0;b=a;while(b!=0){c=b%10;sum+=c*c*c;b=b/10;}if(sum==1099)printf("%5d",a);}}3.#include <stdio.h>void main(){int i,j,k;for (i=1;i<=6;i++){for (j=1;j<=20-3*i;j++) printf(" ");for (k=1;k<=i;k++) printf("%3d",k);for (k=i-1;k>0;k--) printf("%3d",k);printf("\n");}}4.#include <stdio.h>void main(){float x,fmax,fmin;scanf("%f",&x);fmax=fmin=x;while (x>=0){if (x>fmax)fmax=x;elseif (x<fmin)fmin=x;scanf("%f",&x);}printf("\nmax=%f,min=%f",fmax,fmin);}7.2 练习题p89-96一、选择题1~5.BCCAD 6~10.CDDCD11~15.DDABC 16~17.BBAAD21~26.BDCDDC二、填空题1.(1)’a’ (2)’\o’2.(1)a[k] (2)a[k] (3)a[k]3.6 4.A B C D EA5.1,6 6.k=p;7.i<j 8.189.4,some string *test 10. you&me三、读程序,写结果1.5 4 8 2 0 2.10 24 15 63.love china! H w l4.66 66 555v. Front四、程序设计题1.#include <stdio.h>void main(){int i,j,a[10],k;for(i=0;i<=9;i++)scanf("%d",&a[i]);for(i=0,j=9;i<=j;i++,j--){k=a[i];a[i]=a[j];a[j]=k;}for(i=0;i<=9;i++)printf("%d",a[i]);}2.#include <stdio.h>#include <ctype.h>void main(){char i,a[80];scanf("%s",a);for(i=0;i<strlen(a);i++)if (islower(a[i]))a[i]=a[i]-32;else附录习题参考答案if (isupper(a[i]))a[i]=a[i]+32;printf("%s",a);}3.#include <stdio.h>void main(){char a[80],i;gets(a);for(i=strlen(p)-1;i>=0;i--)putchar(a[i]);}4.#include <stdio.h>void main(){int a[3][6],i,j;int d,d1=0,d2=0,x,x1=0,x2=0;for(i=0;i<3;i++)for(j=0;j<6;j++){scanf("%d",&a[i][j]);if (i= =0&&j= =0) x=d=a[i][j];if (d<a[i][j) {d=a[i][j];d1=i;d2=j;}else if (x>a[i][j]) {x=a[i][j];x1=i;x2=j;}p++;}printf("d=%d,d1=%d,d2=%d\n",d,d1,d2);printf("x=5d,x1=%d,x2=%d\n",x,x1,x2);}5.#include <stdio.h>void main(){int a[10],i,d,d1=0,x,x1=0;for(i=0;i<10;i++,p++){scanf("%d",&a[i]);if (i= =0) {d=a[i];x=a[i];}if (d<a[i]) {d=a[i];d1=i;}else if (x>a[i]) {x=a[i];x1=i;}}a[d1]=a[0];a[0]=d;a[x1]=a[9];a[9]=x;for(i=9;i>=0;i--,p--)printf("%d,",*p);} 8.2练习题p106-114一、选择题1~5.AABBB 6~10.ADCAA11~15.ABBAB 16~17.BC二、填空题1.12 2.(1)Itis3.(1)n*fun(n-1) (2)fun(k) 4.s[i]-t[i]5.(1)n%m (2)i-1 (3)x[d];6.(1)x=n; (2)n%10 (3) x7.m= f(a,4)+f(b,4)-f(a+b,3) 8.a=1,b=1;三、读程序,写结果1.s=7 2.s=37 3.bij12四、程序设计题1.int nian(int y){if((y%4==0)&&(y%100!=0)||(y%400==0)) return 1;else return 0;}2.void fun(float a[],float b[]){int i;float s=0;b[0]=b[1]=a[0];for(i=0;i<n;i++){s=s+a[i];if (b[0]<a[i]) b[0=a[i];else if (b[1]>a[i]) b[1]=a[i];}b[2]=s/n;}3.int cout(char str[],char sub[]){int i,j,k;int count=0;for(i=0;str[i];i++)for(j=i,k=0;sub[k]= =str[j];k++,j++)if(sub[k+1]=='\0'){count++;break;}return count;}附录习题参考答案4.#include "stdio.h"void pp(int m){int j;if(j=m/10) pp(j);putchar(n%10+'0');}void main(){int n;scanf("%d",&n);if(n<0){putchar('-');n=-n;}pp(n);}5.#include "stdio.h"int add(int n){int r,s=0;do{r=n%10;s+=r;n=n/10;}while(n>0);return s;}void main(){int a;scanf("%d",&a);printf("%d", add(a));}6.#include <math.h>#include "stdio.h"void prime(int n){int i,f=1;for(i=2;i<=sqrt(n);i++)if(n%i= =0){f=0;break;}}void main(){int m;scanf("%d",&m);if(prime(m)) printf("%d", m);else printf("不是素数");}7.参考程序int total(char s[ ],char ch){int i=0,n=0;while(s[i])if(s[i++]==ch) n++;return(n);}#include "stdio.h"void main(){char s[20],ch;gets(s);ch=getchar();printf("%d", total(s,ch));}8.void move(char *x,int n,int m) {int i,j;for(j=0;j<m;j++){char w=*(x+n-1);for(i=0;i<n-1;i++)*(x+n-1-i)=*(x+n-2-i);*x=w;}}9.void print(char *string){char *str=string;if(*str=='\0')return;while(*str)str++;putchar(*--str);*str='\0';print(string);}。