发布时间 : 星期五 文章计算机二级c语言上机100题更新完毕开始阅读c0c450b9c77da26925c5b00c
素中,右上三角元素置为0。例如,若N=3,有下列矩阵: 1 2 3 4 5 6
7 8 9计算结果为: 1 0 0 6 5 0
10 14 9
#define N 4 void fun(int (*t)[N]) { int i, j; for(i=1; i /**********found**********/ t[i][j] =t[i][j]+t[j][i]; /**********found**********/ t[j][i]=0;}}} 50.2:改错 Fun:计算函数F(x,y,z)=(x+y)/(x-y)+(z+y)/(z-y)的值。其中x和y的值不等,z和y的值不等。如当x的值为9、y的值为11、z的值为15时,函数值为-3.50。 #include /************found************/ #define FU(m,n) ((m)/(n)) float fun(float a,float b,float c) { float value; value=FU(a+b,a-b)+FU(c+b,c-b); /************found************/ return (value);} 50.3:设计 规定输入的字符串中只包含字母和*号,函数fun的功能是:将字符串中的前导*号全部删除,中间和尾部的*号不删除。如当字符串中的内容为:*******A*BC*DEF*G****,删除后,字符串中的内容应当是:A*BC*DEF*G****,在编写函数时,不得使用C语言提供的字符串函数。 void fun( char *a ) {int j=0; char *p = a ; while(*p == '*') p++ ; while(*p) { a[j++] = *p ; p++;} a[j]=0 ;} 51.1:填空 Fun:计算出形参s所指字符串中包含的单词个数,作为函数值返回,为便于统计,规定各单词之间用空格隔开。如形参s所指的字符串为:This is a C language program,函数的返回值为6 。 int fun(char *s) { int n=0, flag=0; while(*s!='\\0') { if(*s!=' ' && flag==0) { n++ ; flag=1;} if (*s==' ') flag= 0 ; s++ ;} return n;} 51.2:改错 Fun:从n(形参)个学生的成绩中统计出低于平均分的学生人数,此人数由函数值返回,平均分存放在形参aver所指的存储单元中。如输入8名学生的成绩:80.5 60 72 90.5 98 51.5 88 64 ,则低于平均分的学生人数为:4(平均分为:75.5625)。 #define N 20 int fun ( float *s, int n, float *aver ) { float ave, t = 0.0 ; int count = 0, k, i ; for ( k = 0 ; k < n ; k++ ) /**************found**************/ t += s[k] ; ave = t / n ; for ( i = 0 ; i < n ; i++ ) if ( s[ i ] < ave ) count++ ; /**************found**************/ *aver = ave ; return count ;} 51.3:设计 Fun:功能是求出数组的最大元素在数组中的下标并存放在k所指的存储单元中。如输入如下整数:876 675 896 101 301 401 980 431 451 777 ,则输出结果为6,980 int fun(int *s, int t, int *k) {int i, pos = 0, max = *s ; for(i = 1 ; i < t ; i++) { if(max < *(s + i)) { max = *(s + i) ; pos = i ;}} *k = pos ;} 52.1:填空 Fun:将N×N矩阵中元素的值按列右移一个位置,右边被移出矩阵的元素绕回左边。如N=3,有下列矩阵: 计算结果为: 1 2 3 3 1 2 4 5 6 6 4 5 7 8 9 9 7 8 #define N 4 void fun(int (*t)[N]) { int i, j, x; /**********found**********/ for(i=0; i { /**********found**********/ x=t[i][N-1] ; for(j=N-1; j>=1; j--) t[i][j]=t[i][j-1]; /**********found**********/ t[i][0]=x;}} 52.2:改错 Fun:计算并输出下列级数的前N项之和SN,直到SN?1大于 q 21{ double f, t; int n; f = 1.0 + x; /**********found**********/ t = x; n = 1; do { n++; /**********found**********/ t *= (-1.0)*x/n; f += t; } /**********found**********/ while (fabs(t) >= 1e-6); return f;} 53.2:改错 Fun:求整数x的y次方的低3位值。如整数5的6次方为15625,此值的低3位值为625。 long fun(int x,int y,long *p ) { int i; long t=1; /**************found**************/ for(i=1; i<=y; i++) t=t*x; *p=t; /**************found**************/ t=t00; return t;} 53.3:设计 Fun:计算并输出当x<0.97时下列多项式的值,直到Sn?Sn?1?0.000001为止。 Sn?1?0.5x?0.5(0.5?1)2!x?2为止,q ?32?43???的值通过形参传入。 N?1NSN?,如q的值为50.0,则函数 值为:49.394948。 double fun( double q ) { int n; double s,t; n = 2; s = 2.0; while (s<=q) {t=s; /************found************/ s=s+(n+1.)/n; n++;} printf(\ /************found************/ return t;} 52.3:设计 Fun:求Fibonacci数列中大于t的最小的一个数,结果由函数返回。其中Fibonacci数列F(n)定义为:F(0)=0,F(1)=1,F(n)=F(n-1)+F(n-2),如当t=1000时,函数值为1597。 #include f(x)?1?x?x20.5(0.5?1)(0.5?2)3!x???30如在主函数中从键盘给x输入0.21后,输出为:s=1.100000. #include double sn=1; /* 累计数*/ double xn=1,xn1=0; /*x的n值,以及x的n-1值;*/ 计 算 n?2 while(fabs(xn-xn1)>=0.000001)/*绝对值是否满足条件*/ {xn=xn*x*(0.5-n+1)/n; /*表达式分解以后 n?12!?x3!?x4!???(?1)x3n?(n?1)!?(?1)xnxn=(xn-1)*x*(0.5-n+1)/n*/ 41 n+=1; n! sn+=xn; /*sn累加上xn*/} return(sn);} 54.1:填空 Fun:计算出带有头结点的单向链表中各结点数据域中值之和作为函数值返回。 #include 直到 xnn!?10。若x=2.5,函数值为1.917975. ?6#include #define N 8 typedef struct list { int data; struct list *next; } SLIST; SLIST *creatlist(int *); void outlist(SLIST *); int fun( SLIST *h) { SLIST *p; int s=0; p=h->next; while(p) { /**********found**********/ s+= p->data; /**********found**********/ p=p->next; } return s;} 54.2:改错 Fun:将s所指字符串中出现的与t1所指字符串相同的子串全部替换成t2所指字符串,所形成的新串放在w所指的数组中。要求t1和t2所指字符串的长度相同。如当s所指字符串中内容为 :“abcdabfab”t1所指子串中的内容为:“ab”,t2所指子串中的内容为:“99”时,结果在w所指的数组中的内容应为:“99cd99f99”。 #include int fun (char *s, char *t1, char *t2 , char *w) {int i; char *p , *r, *a; strcpy( w, s ); while ( *w ) { p = w; r = t1; /************found************/ while ( *r ) if ( *r == *p ) { r++; p++; } else break; if ( *r == '\\0' ) { a = w; r = t2; while ( *r ){ /************found************/ *a = *r; a++; r++;} w += strlen(t2) ;} else w++;}} 54.3:设计 Fun:将s所指字符串中下标为偶数的字符删除,串中剩余字符形成的新串放在t所指数组中。如当s所指字符串中的内容为“ABCDEFGHIJK”,在t所指数组中的内容应是:“BDFHJ”。 #include void fun(char *s, char t[]) {int i, j = 0 ; for(i = 1 ; i < strlen(s); i+=2) t[j++] = s[i] ; t[j] = 0 ;} 55.1:填空 Fun:判断形参s所指字符串是否是“回文”(Palindrome),若是,函数返回值为1;不是,函数返回值为0。“回文”是正读和反读都一样的字符串(不区分大小写字母)。如LEVEL和Level是“回文”,而LEVLEV不是“回文”。 #include /**********found**********/ lp= s ; rp=s+strlen(s)-1; while((toupper(*lp)==toupper(*rp)) && (lp