文档详情

全国计算机等级试二级c语言上机题汇编.doc

发布:2017-03-05约2.98万字共61页下载文档
文本预览下载声明
全国计算机等级考试二级c语言上机题汇编 发表时间:2008-02-26??编辑:江昌华??来源:??网络 1、 编一个函数fun char *s ,函数的功能是把字符串中的内容逆置。 例子如:字符串中原有的内容为:abcdefg,则调用该函数后,串中的内容为:gfedcba 。 试题程序: #include #include #include #define N 81 /*注:该题的算法是先分别找出字符串的两头,然后同时逐一往中间移动,每移动一次 都进行两字符的位置对换,直到中间字符(用s+i s+n-1-i来控制)。由于s+i中一个 地址,因此要注意把它的内容取出再进行换位。即先进行取内容运算*) */ fun char *s int i 0,t,n strlen s ; for ;s+i s+n-1-i;i++ t * s+i ;* s+i * s+n-1-i ;* s+n-1-i t; main char a[N]; clrscr ; printf Enter a string: ; gets a ; printf The original string is: ;puts a ; fun a ; printf \n ; printf The string after modified: ; puts a ; 2、 写程序,实现矩阵(3行3列)的转置(即行列互换)。 例如,输入一面的矩阵: 程序输出: 试题程序: #include #include /*这题的关键在于进行行列下标转换的算法,由矩阵的对称性我们不难看出在进行行列 互换时a[j]在好是与a[j][i]互换,因而只要我位让程序走完矩阵的左上角即可(用 for i 0;i 2;i++ 再套for j i+1;j 3;j++ 来完成左上角的走动。*/ int fun int array[3][3] int i,j,t; for i 0;i 2;i++ for j i+1;j 3;j++ t array[i][j];array[i][j] array[j][i];array[j][i] t; main int i,j; int array[3][3] 100,200,300 , 400,500,600 , 700,800,900 ; clrscr ; for i 0;i 3;i++ for j 0;j 3;j++ printf %7d,array[i][j] ; printf \n ; fun array ; printf Converted array:\n ; for i 0;i 3;i++ for j 0;j 3;j++ printf %7d,array[i][j] ; printf \n ; 3、 请编一个函数fun int *a,int n,int *odd,int *even ,函数的功能是分别求出数 组中所有奇数之和以及所有偶数之和。形参n给了数组中数据的个数:利用指针odd返回 奇数之和,利用指针even返回偶娄这和。 例如:数组中的值依次为:1,8,2,3,11,6;则利用指针odd返回奇数之和24;利用 指针even返回偶数之和8。 试题程序。 #include #include #define N 20 /*注:该题的算法是:用for 循环一步一步地找元素,用if !a[i]%2 来判断是否是奇 数,%运算是求余运算,当对2求余为1时表示原数为奇数,否则为偶数。*/ fun int *a,int n,int *odd,int *even int i; *even 0;*odd 0; for i 0;i n;i++ if ! a[i]%2 *even+ a[i]; else *odd+ a[i]; main int a[N] 1,9,2,3,11,6 ,i,n 6,odd,even; clrscr ; printf The original data is:\n ; for i 0;i n;i++ printf %5d,* a+i ; printf \n\n ; fun a,n,odd,even ; printf The sum of odd numbers:%d\n,odd ; printf The sum of even number:%d\n,even ; 4、 要求程序的功能是:把20个随机数存入一个数组,然后输出该数组中的最小值。其 中确定最小值的下标的操作在fun函数中实现,请给出该函数的定义。 试题程序。 #include #include #define VSIZE 20 int vector[VSIZE]; /*注:
显示全部
相似文档