C语言习题题库(作答完整)_第1页
C语言习题题库(作答完整)_第2页
C语言习题题库(作答完整)_第3页
C语言习题题库(作答完整)_第4页
C语言习题题库(作答完整)_第5页
已阅读5页,还剩43页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、程序填空 共2题 第1题/*-【程序填空】-功能:编程求某年第n天的日期。用数组表示月天数。-*/#include <stdio.h>#include <stdlib.h>main() int y,m,f,n; int a12=31,28,31,30,31,30,31,31,30,31,30,31; printf("y,n="); scanf("%d,%d",&y,&n); /*SPACE*/ f=y%4=0&&y%100!=0【|】y%400=0; /*SPACE*/ a1【+=】f; if(n&

2、lt;1|n>365+f) printf("error!n");exit(0); /*SPACE*/ for(m=1;m【>】am-1;n-=am-1,m+); printf("y=%d,m=%d,d=%dn",y,m,n);第2题 /*-【程序填空】-题目:下列程序从键盘输入所需数据,求出z的值并输出,要求输出结果保留2位小数。-*/#include <stdio.h>/*SPACE*/ 【#include<math.h>】 main() int x;double y,z;/*SPACE*/ scanf("

3、【%d%lf】",&x,&y); z=2*x*sqrt(y);/*SPACE*/ printf("z=【%lf】",z); 程序改错 共1题 第1题 /*- 【程序改错】-功能:将s所指字符串中的字母转换为按字母序列的后续字母(但 Z转换为A, z转换为a),其它字符不变。-*/#include <stdio.h>#include <ctype.h>#include <conio.h>void fun (char *s)/函数定义 /*FOUND*/ while(*s!=0)/字符串结尾标志为0 if(*s>

4、;='A' && *s <= 'Z' | *s >= 'a' && *s<='z') if(*s='Z') *s='A' else if(*s='z') *s='a' else /*FOUND*/ *s += 1;/s为字符指针,而*s为指针所指的字符 /*FOUND*/ s+ main() char s80; printf("n Enter a string with length < 80. :nn

5、 "); gets(s); printf("n The string : nn "); puts(s); fun ( s );/函数调用 printf ("nn The Cords :nn "); puts(s);程序填空 共2题 第1题/*-【程序填空】-功能:计算圆周率的近似值。-*/#include <stdio.h>#include <math.h>main() int s,n; /*SPACE*/ double 【pi】,t; t=1;pi=0;n=1;s=1; /*SPACE*/ while(【fabs(t)

6、】>=2e-6)/fabs()为求绝对值函数 pi+=t;n+=2;s=-s;t=s/n; /*SPACE*/ pi*=【4】; printf("pi=%.6fn",pi);第2题 /*-【程序填空】-功能:输入一奇数n,打印由1->n*n构成的魔方矩阵。魔方矩阵的 行列及对角线的和都相等。 魔方矩阵:8 1 6 3 5 7 4 9 2-*/#include <stdio.h>#include <stdlib.h>#define MAXSiZE 20void main(void) int matrixMAXSiZEMAXSiZE; int

7、 count; int row; int column; int n; char line100; printf("nOdd n Magic Square Generator"); printf("n="); printf("nnn Please -> "); gets(line); n = atoi(line); if (n > MAXSiZE) printf("n* ERROR * n should be <= %d", MAXSiZE); else if (n % 2 = 0) printf

8、("n* ERROR * n must be an odd integer"); else row = 0; column = n/2; for (count = 1; count <= n*n; count+) matrixrowcolumn = count; /*SPACE*/ if (【count/n】 = 0) row+; else /*SPACE*/ row= (row = 【0】) ? n - 1 : row - 1; /*SPACE*/ column = (column = 【n-1】) ? 0 : column + 1; printf("nn

9、Magic Square of n %d :nn", n); for (row = 0; row < n; row+) for (column = 0; column < n; column+) printf("%4d", matrixrowcolumn); printf("n"); 程序改错 共1题/*- 【程序改错】-功能:求1到10的阶乘的和。-*/#include <stdio.h>float fac(int n);/函数声明main() int i; float s=0; float fac(int n);/函

10、数声明应放在主函数之前,这句放错位置了,删了吧,也可以不改,可以运行,但良好的习惯就是改 /*FOUND*/ for(i=1;i<10;i+) /*FOUND*/ s+=fac(i);/函数调用 printf("%fn",s);float fac(int n)/函数定义/*FOUND*/ float y=1;/int改为float,否则会造成数据丢失,当然也可以不改,毕竟你们没学到 int i; for(i=1 ;i<=n;i+) y=y*i; /*FOUND*/ return y;程序填空 共2题 第1题/*-【程序填空】-功能:删除字符串中的指定字符,字符串

11、和要删除的字符均由键盘 输入。-*/#include <stdio.h>main() char str80,ch; int i,k=0; /*SPACE*/ gets(【str】);/输入字符串 放入str ch=getchar(); /*SPACE*/ for(i=0;【stri】;i+)/循环到字符串结束为止 if(stri!=ch) /假如没找到就将原字符赋值过去 找到要删除的就跳过继续找 /*SPACE*/ 【strk=stri】;/在同一个数组中操作,后面的字符覆盖了前面要删除的位置 k+; /*SPACE*/ 【strk=0】;/在新字符串结尾处加结束符 puts(st

12、r);/输出 第2题/*-【程序填空】-功能:输入一个整数,计算它可能是哪两个整数的平方和,并打印 结果数据。 如:34是5和3或3和5的平方和。-*/#include <stdio.h> /* for i/O functions */#include <stdlib.h> /* for atoi() */#include <math.h> /* for sqrt() */void main(void) int given; /* the given number */ int row, column; /* row and column indicator

13、s*/ int count; /* number of solutions */ char line100; printf("nRepresenting a Given Number as the Sum of Two Squares"); printf("n=n"); printf("nAn integer Please -> "); gets(line); given = atoi(line); printf("nCount X Y"); printf("n- - -"); row =

14、 1; /* starts from far enough */ column = (int) (sqrt(double) given) + 0.5); count = 0; /* so solution yet */ while (row <= given && column > 0) /* scan down. */ if (row*row + column*column = given) /*SPACE*/ 【count+】; printf("n%5d%7d%7d", count, row, column); row+; column-;

15、else if (row*row + column*column > given) /*SPACE*/ 【column-】; else /*SPACE*/ 【row+】; if (count = 0) printf("nnSorry, NO ANSWER found."); else printf("nnThere are %d possible answers.",count);程序改错 共1题 第1题 /*- 【程序改错】-功能:计算并输出k以内最大的10个能被13或17整除的自然数之和。 k的值由主函数传入。例如:若k的值为500,则函数值为

16、4622。-*/#include <stdio.h>int fun(int k) int m=0,mc=0; /*FOUND*/ while (k>=2)&&(mc<10) /*FOUND*/ if(k%13=0)|(k%17=0) m=m+k; mc+; /*FOUND*/ k-; /*FOUND*/ return m;void main() printf("%dn",fun(500);程序填空 共2题 第1题 /*-【程序填空】-功能:当输入“2,5”的时候输出为“2 5 5”-*/#include <stdio.h>

17、#define max 100main() int fmax,i,j,k,m; scanf("%d,%d",&k,&m); /*SPACE*/ for(i=0;i<=【2】;i+) fi=0; /*SPACE*/ f【k-1】=1; for(i=k;i<=m;i+) /*SPACE*/ for(j=i-k;j<=i-1;j+) fi【=1+】fj; printf("%d%10d%10dn",k,m,fm);第2题 /*-【程序填空】-功能:识别输入的字符串,每个单词输出一行-*/#include <stdio.h&

18、gt;#include <string.h>void main() int c; int inspace; /*SPACE*/ 【inspace=0】; while(c = getchar() != 'n') if(c = ' ' | c = 't' | c = 'n') /*SPACE*/ if(【inspace=0】) inspace = 1; putchar('n'); else inspace = 0; /*SPACE*/ 【putchar(c)】; 程序改错 共1题 第1题 /*- 【程序改错

19、】-功能:用选择法对数组中的n个元素按从小到大的顺序进行排序。-*/#include <stdio.h>#define N 20 void fun(int a, int n) int i, j, t, p; for (j = 0 ;j < n-1 ;j+) /*FOUND*/ p = j; for (i = j;i < n; i+) /*FOUND*/ if(ai >ap) /*FOUND*/ p=i; t = ap ; ap = aj ; aj = t; main() int aN=9,6,8,3,-1,i, m = 5; printf("排序前的数据

20、:") ; for(i = 0;i < m;i+) printf("%d ",ai); printf("n"); fun(a,m); printf("排序后的数据:") ; for(i = 0;i < m;i+) printf("%d ",ai); printf("n");程序填空 共2题 第1题 /*-【程序填空】-功能:产生10个30,90区间上的随机整数,然后对其用选择法 进行由小到大的排序。-*/#include <stdio.h>#include<

21、;stdlib.h>main() /*SPACE*/ int t; int i,j,k; int a10; for(i=0;i<10;i+) ai=rand()%61+30; for(i=0;i<9;i+) /*SPACE*/ k=i; for(j=i+1;j<10;j+) /*SPACE*/ if(ak>aj) k=j; if(k!=i) t=ak; ak=ai; ai=t; /*SPACE*/ for(i=0;i<10;i+ ) printf("%5d",ai); printf("n");第2题 /*-【程序填空】

22、-功能:输入一正整数n、打印1-n能够组成的所有自然数集合 (包含空集)。-*/#include <stdio.h>#include <stdlib.h>#define MAXSiZE 20#define LOOP 1void main(void) int setMAXSiZE; int n, i; int position; char line100; printf("nAll Possible Subsets Generation by Lexical Order"); printf("n="); printf("n

23、nNumber of Elements in the Set -> "); gets(line); n = atoi(line); printf("n"); position = 0; setposition = 1; while (LOOP) /*SPACE*/ printf("n%d", 【?】); for (i = 1; i <= position; i+) printf(",%d", seti); printf(""); if (setposition < n) /*SPACE*

24、/ set【?】 = setposition + 1; position+; else if (position != 0) set-position+; else /*SPACE*/ 【?】; 程序改错 第1题 /*- 【程序改错】-功能:求出以下分数序列的前n项之和。和值通过函数值返回main 函数。 2/1+3/2+5/3+8/5+13/8+21/13 例如:若n = 5,则应输出:8.391667。-*/#include <conio.h>#include <stdio.h> /*FOUND*/fun ( int n ) int a, b, c, k; doub

25、le s; s = 0.0; a = 2; b = 1; for ( k = 1; k <= n; k+ ) /*FOUND*/ s = (double)a / b; c = a; a = a + b; b = c; /*FOUND*/ return c;main( ) int n = 5; printf( "nThe value of function is: %lfn", fun ( n ) );程序填空 共2题 第1题 /*-【程序填空】-功能:输出结果为:* * * * * * * * * * * * * * * * * * * * -*/#include &

26、lt;stdio.h>main() /*SPACE*/ static char 【?】='*','*','*','*','*' int i,j,k; char space=' ' for(i=0;i<5;i+) printf("n"); for(j=1;j<=3*i;j+) /*SPACE*/ printf("%1c",【?】); /*SPACE*/ for(k=0;k<【?】;k+) printf("%3c",ak

27、); printf("n");第2题 /*-【程序填空】-功能:给出一个正整数,找到一组连续的数,使之累加和等于给 定的正整数。输出存在多少组这样连续的数,及每组的左 右边界。例如:15=1+2+3+4+5 15=4+5+6 15=7+8 所以结果有3组值分别是1->5, 4->6, 7->8-*/#include <stdio.h>#include <stdlib.h>void main(void) long left, right; long sum; long GiVEN; int count = 0; char line10

28、0; printf("nConsecutive sum to a fixed given number"); printf("n=n"); printf("nYour number (> 0) please -> "); gets(line); GiVEN = atol(line); for (sum = 0, right = 1; sum < GiVEN; sum += right, right+) ; for (left = 1, right-; left <= GiVEN/2; ) if (sum >

29、; GiVEN) sum -= left; /*SPACE*/ 【?】; else if (sum = GiVEN) printf("n%ld = sum from %ld to %ld", GiVEN, left, right); /*SPACE*/ 【?】; /*SPACE*/ 【?】; sum += right; if (count > 0) printf("nnThere are %d solutions in total.", count); else printf("nnSorry, there is NO solution

30、at all.");程序改错 第1题 /*- 【程序改错】-功能:编写函数fun计算下列分段函数的值: x*20 x<0且x-3 f(x)= sin(x) 0x<10且x2及x3 x*x+x-1 其它-*/#include <math.h>#include <stdio.h>double fun(double x) /*FOUND*/ double y /*FOUND*/ if (x<0 | x!=-3.0) y=x*20; else if(x>=0 && x<10.0 && x!=2.0 &

31、;& x!=3.0) y=sin(x); else y=x*x+x-1; /*FOUND*/ return x;main() double x,f; printf("input x="); scanf("%f",&x); f=fun(x); printf("x=%f,f(x)=%fn",x,f);程序填空 共2题 第1题 /*-【程序填空】-功能:有n个整数,使其前面各数顺序向后移m个位置,最后m个数 变成最前面的m个数-*/#include <stdio.h>/*SPACE*/move(【?】)int n

32、,m,array20; int *p,array_end; /*SPACE*/ array_end=*(【?】); for(p=array+n-1;p>array;p-) /*SPACE*/ *p=*(【?】); *array=array_end; m-; if(m>0) move(array,n,m);main() int number20,n,m,i; printf("the total numbers is:"); scanf("%d",&n); printf("back m:"); scanf("

33、%d",&m); for(i=0;i<n-1;i+) scanf("%d,",&numberi); scanf("%d",&numbern-1); /*SPACE*/ move(【?】); for(i=0;i<n-1;i+) printf("%d,",numberi); printf("%d",numbern-1);第2题 /*-【程序填空】-功能:打印出如下图案(菱形) * * * * * *-*/#include <stdio.h>main() int i,j,k; /*SPACE*/ for(i=0;【?】;i+) for(j=0;j<=4-i;j+) printf(" "); /*SPACE*/ for(k=1;k<=【?】;k+) printf("*"); printf("n"); /*SPACE*/ for(【?】;j<3;j+) for(k=0;k<j+3;k+) printf(" "); for(k=0;k<

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论