华农C语言教材习题答案
- 格式:docx
- 大小:17.93 KB
- 文档页数:13
c程序设计语言(第2版新版)习题解答C程序设计语言(第2版新版)是计算机编程领域的经典教材,由Brian W. Kernighan和Dennis M. Ritchie共同编写,后者也是C语言的共同发明者。
这本书不仅介绍了C语言的基本语法和编程技巧,还包括了丰富的习题,帮助读者更好地理解和掌握C语言。
以下是对书中部分习题的解答。
第1章引言习题1-1:编写一个C程序,打印出“Hello, World!”。
```c#include <stdio.h>int main() {printf("Hello, World!\n");return 0;}```习题1-2:修改上述程序,使其打印出不同的字符串。
```c#include <stdio.h>int main() {printf("Welcome to C Programming!\n");return 0;}```第2章数据类型及其运算习题2-1:编写一个程序,计算两个整数的和,并打印结果。
```c#include <stdio.h>int main() {int a, b, sum;a = 5;b = 10;sum = a + b;printf("The sum of %d and %d is %d\n", a, b, sum);return 0;}```习题2-2:编写一个程序,计算两个浮点数的乘积。
```c#include <stdio.h>int main() {float x, y, product;x = 3.14159;y = 2.718;product = x * y;printf("The product of %.5f and %.5f is %.5f\n", x, y,product);return 0;}```第3章控制语句习题3-1:编写一个程序,打印从1到10的整数。
2022年华中农业大学公共课《C语言》科目期末试卷B(有答案)一、填空题1、为建立如图10-5所示的存储结构(即每个结点两个域,data是数据域,next是指向结点的指针域),请将定义补充完整。
struct s{char data;_______}node;2、C语言源程序文件的后缀是;经过编译后,生成文件的后缀是;经过连接后,生成文件的后缀是3、结构化程序由________、________ 、________ 3种基本结构组成。
4、C语言所提供的基本数据类型包括:单精度型、双精度型、_______、_______和_______。
5、若有定义语句:inte=1,f=4,g=2;float m=10.5,n=4.0,k;,则执行表达式k=(e+f)/g+sqrt((double)n)*1.2/g+m后k的值是_______。
6、设有char a,b;若要通过a&b运算屏蔽掉a中的其他位,只保留第2和第8位(右起为第1位),则b的二进制数是_______。
7、函数swap(arr,n)可对arr数组从第1个元素到第n个元素分别进行两两元素交换。
在运行调用函数中的如下语句后,a[o]和a[1]的值分别为_______,原因是_______。
a[0]=1;a[1]=2;swap(a,2);8、设有如下定义:#define SWAP(T,X,Y){T=X;X=Y;Y=T;}以下程序段将通过调用宏实现变量x和y内容的交换,请填空。
Double x=2.5,y=6.4,z;SWAP(_______);9、设有宏定义如下:#define MIN(x,y)(x)>(y)?(x):(y)#define T(x,y,r)x*r*y/4则执行以下语句后,s1的值为_______,s2的值为_______。
int a=1,b=3,c=5,s1,s2;s1=MIN(a=b,b-a);s2=T(a++,a*++b,a+b+c);10、下面程序段的运行结果是_______。
C语言课后习题标准答案第1章C语言概述一、简答题1.(1)语言简洁、紧凑,使用方便、灵活;(2)运算符丰富;(3)具有丰富的数据类型;(4)具有结构化的控制语句;(5)语法限制不太严格,程序设计自由度大;(6)C语言允许直接访问物理地址,能进行位(bit)操作,能实现汇编语言的大部分功能,可以直接访问硬件;(7)生成目标代码质量高,程序执行效率高;(8)用C语言写的程序可移植性好,基本上不作修改就能用于各种型号的计算机和各种操作系统。
2.顺序结构,分支结构,循环结构3.所谓算法就是为解决一个问题而采取的方法和步骤。
算法的特性:有穷性、确定性、输入、输出、可行性。
表示算法的方式:(1)用自然语言表示;(2)用流程图表示;(3)用N—S流程图表示;(4)用伪代码表示;(5)用计算机语言表示。
二、算法1.瓶子A里盛有醋瓶子B里盛有酱油那一个空瓶C将A中的醋倒入C将B中的酱油倒入A将C中的醋倒入B2.输入一个数放在a中max=a以下步骤重复9次:输入一个数放在a中如果a>max,max=b打印max的值3.如果ﻩﻩa<btemp=aﻩa=bﻩﻩb=temp如果c>aﻩtemp1=aﻩﻩtemp2=ba=cﻩb=temp1ﻩc=temp2否则ﻩ如果ﻩc>bﻩtemp=bﻩb=cﻩc=temp打印a,b,c的值4.i=1sum=0以下程序循环100遍:sum=sum+iﻩi=i+1打印sum的值5.如果(n除以3的余数为0并且n除以5的余数为0)n能够同时被3和5整除否则ﻩn不能够同时被3和5整除6.i=101以下语句循环50遍:ﻩj=2ﻩflag=1ﻩ当j<(i除以2的整数部分)时循环以下语句:ﻩﻩ如果i除以j的余数为零ﻩﻩﻩflag=0ﻩﻩﻩ退出该循环如果flag=1ﻩﻩﻩ打印i的值ﻩi=i+17.如果m<nﻩﻩtemp=mﻩﻩm=nn=tempi=nj=1做以下循环,直到m能够被i/j整除:如果i能够被j整除并且m能够被i/j整除i/j即是m和n的最大公约数ﻩ跳出循环否则ﻩj=j+1打印i/j的值8.data=b*b—4*a*c如果data>0否则如果data=0ﻩx1=x2=—b/2否则无实数解三、编程题1.main(){ﻩprintf(“##############################\n”);ﻩprintf(“How do youdo?\n”);ﻩprintf(“##############################\n”);}2.main(){adata b x2)(1+-=a data b x2)(2--=。
c语言程序设计基础教程课后答案C语言程序设计基础教程课后答案第一章:C语言概述1. 简述C语言的特点。
- C语言是一种结构化编程语言,具有高效、灵活、功能强大等特点。
- 它支持多种编程范式,如过程式编程、结构化编程、面向对象编程等。
- C语言具有较低的抽象级别,接近硬件,因此执行效率较高。
2. C语言的发展历史。
- C语言在1972年由丹尼斯·里奇(Dennis Ritchie)在贝尔实验室开发。
- 它最初是作为UNIX操作系统的系统编程语言而设计的。
第二章:C语言环境和基本语法1. 如何设置C语言的开发环境?- 选择一个C语言编译器,如GCC、MSVC等。
- 安装编译器并配置环境变量,以便在命令行中直接调用编译器。
2. C语言的基本语法结构是什么?- C语言的基本结构包括预处理指令、函数定义、变量声明和语句。
第三章:数据类型、运算符和表达式1. C语言中的基本数据类型有哪些?- 整型(int)、字符型(char)、浮点型(float和double)等。
2. 运算符的分类及其用法。
- 算术运算符:+、-、*、/、%等。
- 赋值运算符:=、+=、-=等。
- 比较运算符:==、!=、>、<等。
- 逻辑运算符:&&、||、!等。
第四章:控制语句1. 条件语句的用法。
- if语句用于基于条件执行代码块。
- switch语句用于多条件选择。
2. 循环语句的用法。
- while循环用于在满足条件时重复执行代码块。
- for循环用于已知迭代次数的循环。
- do-while循环先执行代码块再判断条件。
第五章:数组和字符串1. 数组的基本概念和使用。
- 数组是相同数据类型的元素集合,存储在连续的内存位置。
- 声明数组:int arr[10]; 表示一个包含10个整数的数组。
2. 字符串的表示和操作。
- 在C语言中,字符串是以空字符('\0')结尾的字符数组。
- 使用标准库函数如strcpy、strlen等操作字符串。
《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___。
实验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语言课后习题参考答案第一章:基础知识问题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循环。
5-1#include"stdio.h" void main(){char c1,c2;printf("c1:");c1=getchar();if(c1>='a'&&c1<='z') c2=c1-32;else if(c1>='A'&&c1<='Z') c2=c1+32;else c2=c1;printf("=>%c\n",c2);}5-2#include"stdio.h"void main(){char c;printf("c:");c=getchar();if(c>='A'&&c<='Z')if(c=='A') printf("没有前面的字母!");else if(c=='Z') printf("没有后面的字母!!");else printf("前面的字母=%c,后面的字母=%c",c-1,c+1);printf("\n");} 5-3#include"stdio.h"void main(){int s;char g;printf("s:");scanf("%d",&s);if(s>=0&&s<=100){switch(s/10){case 9:case 10: g='A';break;case 8: g='B';break;case 7: g='C';break;case 6: g='D';break;default: g='E';}printf("g=%c\n",g);}else printf("成绩不在百分制范围内!\n"); }5-4#include"stdio.h"void main(){int y,m,d,dok;int yy,mm,dd;printf("y,m,d:");scanf("%d,%d,%d",&y,&m,&d);switch(m){case 1: case 3: case 5: case 7: case 8: case 10: case 12:if(d>0&&d<=31) dok=4;else dok=0;break;case 4: case 6: case 9: case 11:if(d>0&&d<=30) dok=3;else dok=0;break;case 2: if(y%4==0&&y%100!=0||y%400==0)if(d>0&&d<=29)dok=2;else dok=0;elseif(d>0&&d<=28)dok=1;else dok=0;break;default: dok=0;}if(dok==0) printf("月份或日期不对!\n");else{switch(dok){case 1: if(d==28){yy=y;dd=1;mm=m+1;}else{yy=y;dd=d+1;mm=m;}break;case2:if(d==29){yy=y;dd=1;mm=m+1;}else{yy=y;dd=d+1;mm=m;}break;case3:if(d==30){yy=y;dd=1;mm=m+1;}else{yy=y;dd=d+1;mm=m;}break;case 4:if(d==31)if(m==12){yy=y+1;dd=1;mm=1;}else{yy=y;dd=1;mm=m+1;}else{yy=y;dd=d+1;mm=m;}break;}printf("Tomorrow:%d年%d月%d日\n",yy,mm,dd);}}5-5#include"stdio.h"void main(){int a,b,c,t;printf("a,b,c:");scanf("%d,%d,%d",&a,&b,&c);if(a+b>c&&b+c>a&&a+c>b){if(a>b){t=a;a=b;b=t;}if(a>c){t=a;a=c;c=t;}if(b>c){t=b;b=c;c=t;}if(a==b&&b==c) printf("等边三角形.");else if(a==b||b==c||a==c) printf("等腰角形.");else if(c*c==a*a+b*b) printf("直角三角形.");else printf("任意三角形.");printf("\n");}else printf("不能构成三角形!\n");}6-1#include"stdio.h"void main(){int a,b,m,n,k;printf("m,n:");scanf("%d,%d",&m,&n);a=m;b=n;k=a%b;while(k){a=b;b=k;k=a%b;}printf("%d\n",(m*n/b));}6-2#include"stdio.h"void main(){int n;int i,j,s;printf("n=");scanf("%d",&n);for(i=1;i<=n;i++){s=0;for(j=1;j<i;j++)if(i%j==0) s=s+j;if(s==i) {printf("%d:",i);for(j=1;j<i;j++)if(i%j==0) printf("%d ",j);printf("\n");}}} 6-3#include"stdio.h"#include"math.h"void main(){double x,y,zd,zx,x1,x2;zd=zx=50;for(x=0;x<=2;x=x+0.2){y=2*pow(x,3)-3*pow(x,4)+6*pow(x,5)-4*x+50;if(y>zd){ zd=y;x1=x;}if(y<zx){ zx=y;x2=x;}}printf("x=%f,max=%f\n",x1,zd);printf("x=%f,min=%f\n",x2,zx);}6-4#include"stdio.h"void main(){double x,sum=0,a,b,c;int i;printf("x=");scanf("%lf",&x);a=x;b=1.0;c=1.0;for(i=1;i<=10;i++){sum=sum+a/(b*c);a=a*x*x;b=b+2;c=c*i;}printf("y(%.2lf)=%lf\n",x,sum);}7-1/*选择排序*/#include"stdio.h"void main(){int i,j,n,k,temp;int a[10];printf("n(<10):");scanf("%d",&n);printf("Original:");for(i=0;i<n;i++) scanf("%d",&a[i]);for(i=0;i<n-1;i++){ /*趟*/k=i;for(j=i+1;j<n;j++) /*比较次数*/if(a[j]<a[k]) k=j;/*<升序,>?*/if(k!=i){temp=a[i];a[i]=a[k];a[k]=temp;}}printf("Ordered:");for(i=0;i<n;i++) printf("%d ",a[i]);printf("\n");}7-2#include"stdio.h"void main(){int a[3][3];int i,j,s=1;printf("Input:\n");for(i=0;i<3;i++)for(j=0;j<3;j++){scanf("%d",&a[i][j]);if(i==j) s=s*a[i][j];}printf("s=%d\n",s);} 7-3/*杨辉三角*/#include"stdio.h"void main(){int x[7][7];int i,j;for(i=0;i<7;i++) {x[i][0]=1;x[i][i]=1;}for(i=2;i<7;i++)for(j=1;j<i;j++)x[i][j]=x[i-1][j]+x[i-1][j-1];for(i=0;i<7;i++){for(j=0;j<=i;j++)printf("%3d",x[i][j]);printf("\n");}}7-4#include<stdio.h>#include<string.h>void main(){char str[21];int i,j;printf("str:");gets(str);for(i=0,j=strlen(str)-1;i<=j;i++,j--)if(str[i]!=str[j]) break;if(i>j) printf("%s是回文\n",str);else printf("%s不是回文\n",str);}7-5/*输入一维数组的10个元素,并将最小值与第1个数交换,最大值与最后一个数交换,然后输出交换后的结果*/#include<stdio.h>void main(){int a[10],i,zx,zd;printf("Input:\n");zx=zd=0;for(i=0;i<10;i++){scanf("%d",&a[i]);if(a[i]<a[zx]) zx=i;if(a[i]>a[zd]) zd=i;}if(zx!=0){int t;t=a[0];a[0]=a[zx];a[zx]=t;}if(zd!=9){int t;t=a[9];a[9]=a[zd];a[zd]=t;}for(i=0;i<10;i++)printf("%d ",a[i]);printf("\n");} 8-2#include"stdio.h"double xexp(double x,int n){double c;if(n==0) c=1.0;else c=x*xexp(x,n-1);return c;}void main(){int n;double x;printf("x:");scanf("%lf",&x);printf("n:");scanf("%d",&n);printf("Result=%g\n",xexp(x,n));}8-3#include"stdio.h"int find(int x[],int n,int y){int i,pos=-1;for(i=0;i<n;i++)if(x[i]==y) pos=i;return pos;}void main(){int a[10]={11,1,13,15,18,7,19,27,3,8};int i,y,pos;for(i=0;i<10;i++) printf("%d ",a[i]);printf("\ny:");scanf("%d",&y);pos=find(a,10,y);if(pos==-1) printf("Not found!\n");else printf("Position=%d\n",pos);}8-1#include"stdio.h"#include"conio.h" //getch()#include"stdlib.h" //srand(),rand(),system("cls") #include"time.h" //time()void main(){void init(int a[][5],int m,int n);void input(int a[][5],int m,int n);void output(int a[][5],int m,int n);int min(int b[][5],int m,int n);int a[5][5],ch='0';while(1){system("cls"); //清屏printf("1.初始化 2.键盘输入0.结束程序:");printf("\n");if(ch=='0') break;else if(ch=='1'){init(a,5,5);output(a,5,5);}else if(ch=='2'){input(a,5,5);output(a,5,5);}else printf("Error!\n");printf("Min element:%d\n",min(a,5,5));printf("按任意键继续!\n");getch();}}void init(int a[][5],int m,int n){int i,j;srand(time(0)); //time(0)表示以当前的时间做种子,增加每次运行的随机性for(i=0;i<5;i++)for(j=0;j<5;j++)a[i][j]=rand()%100; //随机数范围:0~32767,将它控制在0~99的范围}void input(int a[][5],int m,int n){int i,j;printf("Input Array 5X5:\n");for(i=0;i<m;i++)for(j=0;j<n;j++)scanf("%d",&a[i][j]);}void output(int a[][5],int m,int n){int i,j;printf("Output Array 5X5:\n");for(i=0;i<5;i++){for(j=0;j<5;j++)printf("%2d ",a[i][j]);printf("\n");}}int min(int b[][5],int m,int n){int i,j,zx;zx=b[0][0];for(i=0;i<m;i++)for(j=0;j<n;j++)if(i==j&&b[i][j]<zx) zx=b[i][j];return zx;}8-4#include"stdio.h"float pave,nave;void saver(float a[],int n){int i,z,f;float psum,nsum;psum=nsum=0.0;z=f=0;for(i=0;i<n;i++)if(a[i]<0) {nsum=nsum+a[i];z++;}else if(a[i]>0) {psum=psum+a[i];f++;}else continue;pave=(z!=0?psum/z:0.0);nave=(f!=0?nsum/f:0.0);}void main(){float a[10]={1.0,11.0,3.0,-1.5,-5.5,-2};saver(a,10);printf("pave=%.1f,nave=%.2f\n",pave,nave); } 8-5#include"stdio.h"#include"math.h"void p1(int a,int b){printf("has two equal roots:%\n",-b/(2*a));}void p2(int a,int b, int disc){float x1,x2;x1=(-b+sqrt(disc))/(2*a);x2=(-b-sqrt(disc))/(2*a);printf("Has distant real roots:% and %\n",x1,x2);}void p3(int a,int b, int disc){float real,image;real=-b/(2*a);image=sqrt(-disc)/(2*a);printf("Has complex roots:\n");printf("%+%8.4fi\n",real,image);printf("%-%8.4fi\n",real,image);}void main(){ int a,b,c,disc;printf("a,b,c:");scanf("%d,%d,%d",&a,&b,&c);disc=b*b-4*a*c;if(fabs(disc)<=1e-6) p1(a,b);else if(disc>1e-6) p2(a,b,disc);else p3(a,b,disc);}8-6#include"stdio.h"#include"stdlib.h"#include"conio.h"#include"time.h"void main(){void printaverage(int score[][5],int m,int n);void printname(int score[][5],int m,int n);int score[10][5];int i,j;srand(time(0));for(i=0;i<10;i++)for(j=0;j<5;j++)score[i][j]=50+rand()%50+1;printf("Output Students' score:\n");printf("Course 1 2 3 4 5\n");printf("-------------------------\n");for(i=0;i<10;i++){printf("No.%2d:",i+1);for(j=0;j<5;j++)printf("%3d ",score[i][j]);printf("\n");}printaverage(score,10,5);printname(score,10,5);} void printaverage(int score[][5],int m,int n){int i,j,sum;printf("\nAverage score:\n");for(i=0;i<m;i++){sum=0;for(j=0;j<n;j++)sum=sum+score[i][j];printf("No.%d:%.1f\n",i+1,sum/5.0);}}void printname(int score[][5],int m,int n){int i,j,max0,row0;for(j=0;j<5;j++){max0=score[0][j];for(i=0;i<10;i++)if(score[i][j]>max0){max0=score[i][j];row0=i;}printf("Course %d,max score=%d,student:No.%d\n",j+1,max0,row0+1);}}大学英语I词汇练习题Choose the best one to complete each sentence.1. She felt like ____C___ frustration, but she was determined not to lose her self-control.A. to cry out ofB. to cry forC. crying out ofD. crying for2. The method he used turned out to be ___A____ in improving the students' English.A. effectiveB. abilityC. responseD. explicit3. Measures had to be taken in face of the housing problem that ____C____ in the city.A. foundedB. raisedC. aroseD. produced4. Without electronic computers, much of today's advanced technology ___D____.A. haven't been achievedB. wouldn't be achievedC. hadn't been achievedD. wouldn't have been achieved5. Jane said to her husband, "Don't worry. There is no cause for __B______ about our daughter'sability to manage herself."A. careB. concernC. attentionD. love6. The tap won't ____A____, and there is water all over the floor.A. turn offB. turn downC. turn onD. turn up7. Wearing the right shoes and clothes ____B____ being fit can make all the difference.A. in additionB. as well asC. alsoD. too8. She didn't try to do anything for her daughter, and ____B____ it's too late now.A. in caseB. in any caseC. at this caseD. in case of9. The girl said she hated _____C___ he smiled at her.A. that wayB. this wayC. the wayD. all the way10. I'll call the hotel. I'll tell them we'll ____A____ tomorrow morning and stay there for two nights.A. check inB. check outC. check offD. check over11. My father didn't go to New York; the doctor suggested that he ___C____ there.A. not to goB. won'tC. not goD. not to go to12. She _____C____ him to find answers to her problems.A. learned fromB. came intoC. leaned onD. subjected to13. The large wings of that bird ______B_____ it to fly high and fast.A. makeB. enableC. forceD. realize14. If he ____D___ the policeman honestly, he would not have been arrested.A. would answerB. answerC. should answerD. had answered15. I intended ___A____ you last Sunday, but I had no time.A. to have called onB. calling onC. to be calling onD. to be called on16. If you try to learn too many things at a time, you may get ____D___.A. alarmedB. scaredC. surprisedD. confused17. I haven't read this book, and my brother hasn't ____A____.A. eitherB. neitherC. alsoD. too18. No sooner ____B_____ than the truck started off.A. his luggage was loadedB. had his luggage been loadedC. loaded his luggageD. his luggage was being loaded19. At the end of the game, the whole crowd ____C____ their feet and cheered wildly.A. emerged fromB. rose fromC. got toD. stood up20. Darren has decided to give ___D_____ football at the end of this season.A. inB. forC. offD. up21. Neither his friends nor his mother ____B____ his marriage to that girl.A. acceptB. acceptsC. agreeD. agrees22. Most people believe that he is quite _____A___ of lying to get out of trouble.A. capableB. enabledC. ableD. skilled23. We were told that most of our luggage would be _____B___ by sea.A. approachedB. transportedC. handledD. communicated24. The Broadcasting Museum also offers Saturday workshops to _____B___ children with the world of radio.A. contriveB. acquaintC. acquireD. admit25. Without a passport, leaving the country is ____D___.A. out of questionB. without questionC. in the questionD. out of the question26. She states her views very ___A______.A. definitelyB. definiteC. infiniteD. infinitely27. You need to ____D____ the employers that you can do the job.A. convictB. confuseC. confirmD. convince28.The little boy was so lucky to have ___B_____ the earthquake.A. surviveB. survived C .surviving D. survival29. It is ___B___ that later in life you will see each other again.A. likeB. likelyC. likedD. seemed30.Women’s social ____B____ has changed much over the years.A. stateB. statusC. statesD. statement31.The novel _____D____ the imagination of thousands of readers.A. caterB. cateredC. captureD. captured32.There are good chances of ______D______ working in this firm.A. prospectB. portionC. positionD. promotion33.If you are ______A_____ a night owl, having the first class start in the afternoon might be thebest thing that could ever happen to you.A. more ofB. more thanC. much moreD. more34. The conference discussed the fair ______A_____of income and wealth.A. distributionB. distributeC. contributionD. contribute35. I was so ___C__ in today's history lesson that I didn't understand anything.A. amazedB. confusingC. confusedD. amazing36. ___B___ in England, Anne Bradstreet both admired and imitated several English poets.A. Having born and educatedB. Born and educatedC. Since born and educatedD. To be born and educated37. You will see this product ___B___ wherever you go.A. to be advertisedB. advertisedC. advertiseD. advertising38. I never forget my __B__ with you for the first time.A. to meetB. meetingC. to have metD. having to be meeting39.She was so angry that she left like __B__ something at him.A. to throwB. throwingC. to have thrownD. having thrown40. We are ___C__ in different kinds of extracurricular activities on campus.A. having involvedB. to involveC. involvedD. involving41.Let me assure you that it was not done ______D_____.A. intensiveB. intensivelyC. intentionalD. intentionally42.This medicine can ____A____ the pain in no time.A. lessenB. lessC. looseD. loosen43.Advertising is often the most ____A____ method of product promotion.A. effectiveB. effectedC. emotionalD. efficient44.It is at 5 a.m. _C___ the train from Beijing to Guangzhou will arrive.A. forB. withC. thatD. when45.Things might have been much worse if the mother _B___ on her right to keep the baby.A. has been insistingB. had insistedC. would insistD. insisted46.Had he worked harder, he __B__ the exams.A. must have got throughB. would have got throughC. permitted are freshmenD. are permitted freshmen47.God knows, how can I ____A____ six exams a week?.A. endureB. tolerateC. bear D .suffer48.All their attempts ______C______ the child from the burning building were in vain.A. rescuedB. to have rescueC. to rescueD. in rescuing49,Could you find someone ______C_______ for me to play tennis with?A. to fitB. fitC. fittingD. suit50. The smooth space flight marks a big ______D______ for Chinese scientists.A. breakB. breakingC. breakupD. breakthrough51 Economic crises destroy the capitalist system, and they grow in size and ____A____.A. durationB. duringC. endureD. enduringpanies will have to do more than this if they are to ___B_____ the earthquake.A. survivedB. surviveC. survivesD. surviving53. The baby felt ____C____ by her parents.A. hateB. loveC. ignoredD. ignore54. There are good chances of ______D______ in this firm.A. moveB. motiveC. motionD. promotion55. She tried to ______D______ our friendship.A. underminedB. keptC. maintainedD. undermine56. The new airport will ______C___ tourism.A. helpedB. useC. facilitateD. benefited57. They were full of ____B____ when they saw my new car.A. jealousB. envyC. pitifulD. loves58. This medicine can ____A____ the pain in no time.A. lessenB. lessC. leastD. little59.He _______A_____ her on her last physics paper.A. complimentedB. complimentC. completeD. completed60. Mrs. Hill is _______B____ on Tom’s marrying Susan.A. awareB. keenC. kindD. keep61. Let me assure you that it was not done ______C_____.A. internationalB. purposeC. intentionallyD. intentional62.The novel _____D_____ the imagination of thousands of readers.A. seizeB. catchC. captureD. captured63. If you yell ___A____ someone, you should apologize.A. atB. outC. ofD. under64. He came over and ____B____ a piece of paper into my hand.A. shoveB. shovedC. shovesD. shoving65. The girl student whose father is a senior engineer used to ___A____ abroad.A. studyB. studyingC. studiesD. studied66.The doctor tried to help ____D____ the function of his limbs.A. storeB. storesC. restoresD. restore67. Women’s social ____B____ has changed much over the years.A. placeB. statusC. landD. work68. It was he ___A___ knew he didn’t need to be afraid to make mistakes at that moment.A. whoB. whomC. whichD. whose69. A dropped cigarette is being ____C____ for the fire.A. firedB. fireC. blamedD. blame70. Advertising is often the most _____D___ method of promotion.A. effectB. affectC. affectedD. effective71.In time you’ll _____A_____ the beauty of this language.A. appreciateB. appreciatesC. appreciatedD. appreciating72. ______B___ is his favorite toy is hard for the kid to decide.A. whoB. whichC. howD. where73. After dinner, I took a ____A____ around the park.A. strollB. divingC. runD. walking74. The boy really enjoyed the useful and ____B____ work in science.A. creationB. creativeC. createdD. creator75. Some professions are _____A_______ with people who have not grown up.A. stuffedB. stuffC. stiffD. stiffed76. Economic crises destroy the capitalist system, and they grow in size and ____A____.A. durationB. duringC. endureD. enduring77. The new airport will ____C_____ tourism.A. helpedB. useC. facilitateD. benifited78. Let me assure you that it was not done _____C______.A. internationalB. purposeC. intentionallyD. intentional79. The girl student whose father is a senior engineer used to ___A____ abroad.A. studyB. studyingC. studiesD. studied80. Advertising is often the most ____D____ method of promotion.A. effectB. affectC. affectedD. effective81. It was he ___A___ knew he didn’t need to be afraid to make mistakes at that moment.A. whoB. whomC. whichD. whosepanies will have to do more than this if they are to _____B___ the earthquake.A. survivedB. surviveC. survivesD. surviving83. The baby felt ____C____ by her parents.A. hateB. loveC. ignoredD. ignore84. If you yell ___A____ someone, you should apologize.A. atB. outC. ofD. under85. Mrs. Hill is ______B_____ on Tom’s marrying Susan.A. awareB. keenC. kindD. keep86. _____A____ college takes hard work!A. SurvivingB. SurviveC. SurvivesD. To surviving87. It is absolutely ____A___ for the human beings to do something to save the earth.A. necessaryB. necessarilyC. unnecessaryD. unnecessarily88. An employer is more ____D____ to choose the candidate with high grades.A. likeB. probablyC. possiblyD. likely89. I don’t like this young woman. She seems a social ___D____.A. flowerB. workerC. negotiatorD. butterfly90. How should this ___B____ girl see the coldness of the letter?A. blindB. innocentC. warmD. pretty91. Be careful not to ___B____ yourself in their local conflicts.A. attendB. involveC. take part inD. participate92. Home prices are holding ____C___ in most regions, and people live with steady income.A. flexibleB. fierceC. stableD. shortage93. He was ______C______ by a newspaper reporter at that time.A. being interviewB. interviewC. being interviewedD. to interview94. They were full of ____C____ when they saw my new car.A. enviousB. envilyC. envyD. envies95. ________C_________, it is a very stable pattern producing results through effort .A. ContraryB. ConverseC. On the contraryD. Diverse96. The teacher doesn’t permit _____B____ in classA. smokeB. smokingC. to smokeD. to have a smoke97. The man in the corner confessed to ___A____ a lie to the manager of the company.A. have toldB. be toldC. being toldD. having told98. I never forget __B____ you for the first time.A. to meetB. meetingC. to have metD. having to be meeting.99. _______A___ a man has, ___________ he wants.A. The more, the moreB. More, moreC. The more, moreD. More, the more100. I see no _____B____ of his success.A. possibleB. possibilityC. possibilitiesD. probable101. He could be ____B____ about everything else in the world, but not about Manet, his loving child.A. visualB. criticalC. favoriteD. essential102. Some see themselves as the provider of ideas, ____C____ others view their role as essentially managerial.A. whenB. thereforeC. whileD. otherwise103. The idea ____A____ to him so much that he took it without hesitation.A. appealedB. interestedC. drewD. attracted104. The teacher evaluated the performance of each of his students who ____B____ evaluated his performance.A. by turnB. in turnC. at turnD. on turn105. Disease _____A____ during the journey and many passengers had to be rushed to hospital for treatment.A. broke outB. broke downC. started offD. started out106. The products have been _____A_____ to strict tests before leaving the factory.A. subjectedB. adjustedC. objectedD. constricted107. Our television set is out of order. Could you come and _____C_____ it for us?A. see throughB. see outC. see toD. see over108. _____C_____ they have taken matters into their hands, the pace of events has quickened.A. As thatB. So thatC. Now thatD. For that109. Our neighbor said if we made more noise he would _____B____ us to the police.A. inform ofB. complain aboutC. report toD. care for110. People working in the government should not _____A______ business affairs that might affect their political decisions.A. engage inB. hope forC. choose betweenD. pick on111. It's not easy to ______B______ to the top in show business.A. make outB. make itC. make upD. make them112. Carter is in charge of the office while I'm ______C______.A. leavingB. on leavingC. on leaveD. on relief113. We must ____B____ our attention on the question of reducing our cost.A. payB. focusC. absorbD. promote 114. Snap judgments, if ___B_____, have usually been considered signs of immaturity or lack of common sense.A. taking seriouslyB. taken seriouslyC. take seriouslyD. to be taken seriously115. Being with his family for a few days, I gained one or two insights ____A____ the reason he behaves the way he does.A. intoB. ofC. onD. off116. ___D___ your age or knowledge of the language, you'll be 100% involved in your studies from the first lesson to the last.A. As a result ofB. In spiteC. Despite ofD. Regardless of117. He said he had caught a bad cold and told me to ____D____ him.A. stay atB. stay upC. stay inD. stay away from118. Good managing of a company ___A_____ great efforts.A. calls forB. calls outC. calls inD. calls at119. I've looked ___C_____ all my papers but I still can't find the contract.A. uponB. outC. throughD. in120. When I'm going out in the evening I use the bike if I can, ___B_____ the car.A. regardless ofB. more thanC. other thanD. rather than121. We can't speak ___A____ our teacher.A. too highly ofB. too high ofC. highly of tooD. to highly of122. No one in our class ____D___ in sports than he.A. are more interestedB. are much interestedC. is much interestedD. is more interested123. As a teacher you have to ____B____ your methods to suit the needs of slower children.A. enlargeB. adjustC. affectD. afford124. ________A___ the hotel before soldiers armed with pistols and clubs charged into the crowd.A. Scarcely had they leftB. Scarcely they had leftC. Scarcely they leftD. Scarcely left they125. In the film, the peaceful life of a monk _____D_____ the violent life of a murderer.A. is compared withB. is compared toC. is contrasted toD. is contrasted with126 I didn't realize the food problem was so _____B___ in this city; with winter coming, many people would starve to death without more help.A. essentialB. criticalC. explicitD. effective127. Female socialization places importance on getting along with others, _____B___ male socialization places importance on becoming independent.。
C语言程序设计课后习题1—8参考答案习题1参考答案一、简答题1、冯诺依曼计算机模型有哪几个基本组成部分?各部分的主要功能是什么?答:冯诺依曼计算机模型是由运算器、控制器、存储器、输入设备、输出设备五大功能部件组成的。
运算器又称算术逻辑部件,简称ALU,是计算机用来进行数据运算的部件。
数据运算包括算术运算和逻辑运算。
控制器是计算机的指挥系统,计算机就是在控制器的控制下有条不紊地协调工作的.存储器是计算机中具有记忆能力的部件,用来存放程序和数据.输入设备是用来输入程序和数据的部件。
输出设备正好与输入设备相反,是用来输出结果的部件。
2、简述计算机的工作原理。
答:计算机的工作原理可简单地概括为:各种各样的信息,通过输入设备,进入计算机的存储器,然后送到运算器,运算完毕把结果送到存储器存储,最后通过输出设备显示出来。
整个过程由控制器进行控制。
3、计算机软件系统分为哪几类?答:软件内容丰富,种类繁多,通常根据软件用途将其分为两大类:系统软件和应用软件。
系统软件是指管理、监控、维护计算机正常工作和供用户操作使用计算机的软件。
这类软件一般与具体应用无关,是在系统一级上提供的服务。
系统软件主要包括以下两类:一类是面向计算机本身的软件,如操作系统、诊断程序等。
另一类是面向用户的软件,如各种语言处理程序(像BC、VC等)、实用程序、字处理程序等。
在操作系统的基础上运行。
4、什么叫软件?说明软件与硬件之间的相互关系。
答:软件是指计算机程序及有关程序的技术文档资料。
两者中更为重要的是程序,它是计算机进行数据处理的指令集,也是计算机正常工作最重要的因素。
在不太严格的情况下,认为程序就是软件。
硬件与软件是相互依存的,软件依赖于硬件的物质条件,而硬件则需在软件支配下才能有效地工作.在现代,软件技术变得越来越重要,有了软件,用户面对的将不再是物理计算机,而是一台抽象的逻辑计算机,人们可以不必了解计算机本身,可以采用更加方便、更加有效地手段使用计算机。
注:答案仅供参考分期还款:#include <stdio.h>#include <math.h>int main(){double d, p,r;scanf("%lf%lf%lf",&d,&p,&r);printf("%.1f", log10(p/(p-d*r))/log10(1+r));return 0;}计算分段函数函数值:#include <stdio.h>int main(){double x;scanf("%lf",&x);if(x<1) printf("%.2f", x);else if(x>=1 && x<10) printf("%.2f", 2*x-1);else printf("%.2f", 3*x-11);}成绩等级评分:#include <stdio.h>#include <math.h>int main(){int score;scanf("%d", &score);if(score>100 || score<0)printf("error");else if(score>=90)printf("A");else if(score>=80)printf("B");else if(score>=70)printf("C");else if(score>=60)printf("D");elseprintf("E");return 0;}20秒后时间:#include <stdio.h>#include <stdlib.h>int main(){int h,m,s;scanf("%d:%d:%d", &h,&m,&s);s=s+20;if(s>59){s=s%60;m++;if(m>59){m=m%60;h++;if(h>23)h=0;}}printf("%02d:%02d:%02d\n", h, m, s);return 0;}分期还款(加强版):#include <stdio.h>#include <math.h>int main(){double d, p,r;scanf("%lf%lf%lf",&d,&p,&r);if(d==0)printf("0.0\n");else if(d*(1+r)-p>d)printf("God\n");else{printf("%.1f", log10(p/(p-d*r))/log10(1+r));}return 0;}字母分类统计:#include <stdio.h>#include <math.h>int main(){char ch;int nL=0, nN=0, nS=0, nO=0;while((ch=getchar())!='\n'){if(ch>='a'&&ch<='z'||ch>='A'&&ch<='Z')nL++;else if (ch>='0'&&ch<='9')nN++;else if (ch==' ')nS++;elsenO++;}printf("%d %d %d %d\n", nL, nN, nS, nO);return 0;}计算数列和:#include<stdio.h>main(){ int i,n;double a=2,b=1,s=0;scanf("%d",&n);for(i=1;i<=n;i++){ s=s+a/b;a=a+b;b=a-b;}printf("%.4f\n",s);}自由落体:#include <stdio.h>#include <math.h>int main(){double sum=-100, d=100;int i,n;scanf("%d", &n);for(i=1; i<=n; i++){sum+=2*d;d/=2;}printf("%.3f %.3f", sum, d);return 0;}迭代法求平方根:#include <stdio.h>#include <math.h>int main(){double a, x0, x1=1;scanf("%lf", &a);do{x0=x1;x1=1.0/2*(x1+a/x1);}while(fabs(x1-x0)>=1e-5);printf("%.4lf\n", x1);return 0;}打印三角形:#include <stdio.h>#define abs(x) ((x)>=0?(x):-(x))int main(){int n,i,j;scanf("%d", &n);for(i=-(n-1); i<=(n-1); i++){for(j=0; j<abs(i); j++)printf(" ");for(j=0; j<n-abs(i); j++)printf("*");printf("\n");}return 0;}勾股数:#include <stdio.h>#include <math.h>int main(){int i,j,k,n;scanf("%d", &n);for(i=1; i<=n; i++)for(j=i; j<=n; j++){k=(int)sqrt(i*i+j*j);if(k*k==i*i+j*j && k<=n)printf("%d %d %d\n", i,j,k);}return 0;}插入数据:int n,i;scanf("%d", &n);for(i=14; i>=0; i--)if(a[i]>n) a[i+1]=a[i];else break;a[i+1]=n;输出不同的数:#include"stdio.h"main(){ int a[20];int i,t,p=0;for(i=0;i<10;i++){ scanf("%d",&a[i]);for(t=0;t<i;t++)if(a[t]==a[i])break;if(t==i)printf("%d\n", a[i]);}}鞍点:#include <stdio.h>#include <stdlib.h>int main(){int i,j,a[3][4],flag=0,col,r;for(i=0;i<3;i++)for(j=0;j<4;j++)scanf("%d",&a[i][j]);for(i=0;i<3;i++){col=0;for(j=1;j<4;j++)if(a[i][j]>a[i][col]) col=j;for(j=0;j<4;j++){if(a[i][j]==a[i][col]){for(r=0;r<3;r++)if(a[r][j]<a[i][j]) break;if(r==3){printf("%d\n", a[i][j]);flag=1;}}}}if(!flag)printf("NO\n");return 0;}回文串:#include "stdio.h"#include "string.h"main(){ int i, j;char buf[100];scanf("%s",buf);for(i=0, j=strlen(buf)-1;i<j; i++, j--)if(buf[i]!=buf[j]) break;if(i>=j)printf("Y");elseprintf("N");}ASCII码值之和的差:#include <stdio.h>int sum(char *s){int t=0,i=0;while(s[i]!='\0')t+=s[i++];return t;}int main(){char s1[81],s2[82];gets(s1);gets(s2);printf("%d", sum(s1)-sum(s2));return 0;}所有数字之和:#include "stdio.h"int sum(int n){int ret=0;while(n>0){ret+=n%10;n/=10;}return ret;}main(){int n;scanf("%d", &n);printf("%d", sum(n));}元音字母:#include "stdio.h"void yuan(char *s,char *s2){while(*s!='\0'){if(*s=='a' ||*s=='o' ||*s=='e' ||*s=='i' ||*s=='u' || *s=='A' ||*s=='O' ||*s=='E' ||*s=='I' ||*s=='U'){*s2=*s; s2++;}s++;}*s2='\0';}main(){char str[81], str2[81];gets(str);yuan(str,str2);printf("%s", str2);}字符统计:#include <stdio.h>#include <string.h>#include <math.h>int nL=0, nN=0;int statistics(char *s){int ret=0;while(*s!='\0'){if(*s>='a'&&*s<='z'||*s>='A'&&*s<='Z')nL++;else if (*s>='0'&&*s<='9')nN++;else if (*s==' ')ret++;s++;}return ret;}int main(){char s[81];int nS;gets(s);nS = statistics(s);printf("%d %d %d\n", nL, nN, nS);return 0;}选择排序:#include <stdio.h>int sort(int a[], int n){int i,j,k,tmp;for(i=0;i<n-1; i++){k=i;for(j=i+1; j<n; j++)if(a[k]>a[j]) k=j;tmp=a[k];a[k]=a[i];a[i]=tmp;}}int main(){int a[10];int i;for(i=0; i<10; i++) scanf("%d", &a[i]);sort(a,10);for(i=0; i<10; i++) printf("%d\n", a[i]);return 0;}x的n次方:#include <stdio.h>int F(int x, int n){if(n==1) return x;else return F(x, n-1)*x;}int main(){int x, n;scanf("%d%d", &x, &n);printf("%d", F(x, n));return 0;}行列互换:#include <stdio.h>void s *a, int *b){int temp;temp=*a;*a=*b;*b=temp;}void col(int a[][4], int i, int j){int k;for(k=0; k<4; k++)s[k][i], &a[k][j]);}void row(int a[][4], int i, int j){int k;for(k=0; k<4; k++)s[i][k], &a[j][k]);}int main(){int a[4][4];int i,j;for(i=0; i<4; i++)for(j=0; j<4; j++)scanf("%d", &a[i][j]);col(a,0,2);row(a,0,2);col(a,1,3);row(a,1,3);col(a,0,3);row(a,0,3);for(i=0; i<4; i++){for(j=0; j<4; j++)printf("%d ", a[i][j]);printf("\n");}return 0;}学生信息统计:#include <stdio.h>void average(double a[][5], int n) {int i,j;double sum;for(i=0; i<n; i++){sum=0;for(j=0; j<5; j++)sum+=a[i][j];printf("%.2f ", sum/5);}printf("\n");}void average2(double a[][5], int n){int i,j;double sum;for(j=0; j<5; j++){sum=0;for(i=0; i<n; i++)sum+=a[i][j];printf("%.2f ", sum/n);}printf("\n");}void top(double a[][5], int n){int i,j; double max;for(j=0; j<5; j++){max=a[0][j];for(i=1; i<n; i++)if(a[i][j]>max) max = a[i][j];printf("%.2f ", max);}printf("\n");}int main(){double a[10][5];int i, j;for(i=0; i<10; i++)for(j=0; j<5; j++)scanf("%lf", &a[i][j]);average(a,10);average2(a,10);top(a,10);return 0;}一年的第几天:#include <stdio.h>struct DATE{int year;int month;int day;};int days(struct DATE date){int day,sum;if(date.year%4==0&&date.year%100!=0||date.year%400==0) {day=29;}else{day=28;};switch(date.month){case 1:sum=0;break;case 2:sum=31;break;case 3:sum=31+day;break;case 4:sum=62+day;break;case 5:sum=92+day;break;case 6:sum=123+day;break;case 7:sum=153+day;break;case 8:sum=184+day;break;case 9:sum=215+day;break;case 10:sum=245+day;break;case 11:sum=276+day;break;case 12:sum=306+day;break;};return(sum+date.day);}int main(){struct DATE d;scanf("%d-%d-%d", &d.year, &d.month, &d.day);printf("%d", days(d));}学生成绩表:#include <stdio.h>struct data{int num;char name[20];double score[3];double average;};int main(){int i,j;struct data stu[10],tmp;for(i=0; i<10; i++){scanf("%d%s%lf%lf%lf", &stu[i].num, stu[i].name, stu[i].score, stu[i].score+1, stu[i].score+2);stu[i].average=stu[i].score[0]+stu[i].score[1]+stu[i].score[2]; } for(i=0; i<9; i++)for(j=0; j<9-i; j++){if(stu[j].average<stu[j+1].average){tmp=stu[j];stu[j]=stu[j+1];stu[j+1]=tmp;} }for(i=0; i<10; i++){printf("%d %s %.0lf %.0lf %.0lf\n", stu[i].num, stu[i].name, stu[i].score[0], stu[i].score[1], stu[i].score[2]);}return 0;}。