第16页共75页 t=b: b=a: a=t maino int a clrscr o printf( Enter a, b: ) scanf ("%d%d", &a, &b) fun(&a, &b) printf( a=%d b=%dn, a, b) 19、下列给定程序中,函数fun的功能是:从低位开始取出长整型变量s中偶数位上的数, 次构成一个新数放在t中。例如,当s中的数为7654321时,t是的数为642 请改正程序中的错误,使程序能得出正确的结果。 注意,不要改多main函数,不得增行或删行,也不得更改程序的结构! 试题程序 #include <conio. h> #include <stdio. h> /*水*水***** found水**冰水* void fun (long s, long t) (long s1=10 s/=10 while (s<0) {s=s/100; t=S%10*s1+*t SI=sl*10 maino c⊥rscl printf("nPlease enter s: ) scanf( %ld", &s) fun(s, &t) printf ("The result %1dn", t) 20、N个有序整数数列已放在一堆数组中,下列给定程序中,函数fumn的功能是:利用折半 査找算法找整数m再数组中的位置。若找到,则返回其下标值;反之,则返回-1 折半查找的基本算法是:每次查找前先确定数组中待查的范围:10W和high(low<high),然 后把m与中间位置(mid)中元素的值进行比较。如果m的值大于中间位置元素中的值,则下 次的查找范围放在中间位置之后的元素中;反之,下一次的查找范围落在中间位置之前的 元素中。直到1ow>high,查找结束。 唯C动力(htp:/www.clearner.net)版权所有
第 16 页 共 75 页 唯 C 动力(http://www.clearner.net)版权所有 t=b;b=a;a=t; } main() {int a,b; clrscr(); printf("Enter a,b:");scanf("%d%d",&a,&b); fun(&a,&b); printf("a=%d b=%dn",a,b); } 19、下列给定程序中,函数 fun 的功能是:从低位开始取出长整型变量 s 中偶数位上的数, 一次构成一个新数放在 t 中。例如,当 s 中的数为 7654321 时,t 是的数为 642。 请改正程序中的错误,使程序能得出正确的结果。 注意,不要改多 main 函数,不得增行或删行,也不得更改程序的结构! 试题程序 #include <conio.h> #include <stdio.h> /**********found***********/ void fun (long s, long t) {long sl=10; s/=10; *t=s%10; /**********found***********/ while (s<0) {s=s/100; *t=s%10*sl+*t; sl=sl*10; } } main() {long s,t; clrscr(); printf("nPlease enter s:");scanf("%ld",&s); fun(s,&t); printf("The result is : %ldn",t); } 20、N 个有序整数数列已放在一堆数组中,下列给定程序中,函数 fun 的功能是:利用折半 查找算法找整数 m 再数组中的位置。若找到,则返回其下标值;反之,则返回-1。 折半查找的基本算法是:每次查找前先确定数组中待查的范围:low 和 high(low<high),然 后把 m 与中间位置(mid)中元素的值进行比较。如果 m 的值大于中间位置元素中的值,则下 一次的查找范围放在中间位置之后的元素中;反之,下一次的查找范围落在中间位置之前的 元素中。直到 low>high,查找结束
第17页共75页 请改正程序中的错误,使程序能得出正确的结果 注意,不要改多main函数,不得增行或删行,也不得更改程序的结构 试题程序 tdi. h> #definen 10 void fun (int a[, int m) lint low=0, high=N-1, mid while(low<=high) imid=(low+high)/2 if (m<a [mid]) /*******fOnd水***冰水**/ else l m>=a lml low-mid+1 else return (mid) maino {inti,aN]={-3,4,7,9,13,45,67,89,100,180},k,m; printf("a数组中的数据如下:") for(i=0: i<N; i++) printf(%d", a[]) printf(Enter m: ) scanf("%d", &m) k=fun(a, m) if(k>=0) printf (" m=%d, index=%d\n", m, k) else printf ("Not be found! \n") 21、给定程序是建立一个带头结点的单向链表,并用随机函数为各结点数据域赋值。函数fun 的作用是求出单向链表结点(不包括头结点)数据域中的最大值,并且作为函数值返回。 请改正函数fun中指定部位的错误,使它能得出正确的结果。 注意:不要改动main函数,不得增行或删行,也不得更改程序的结构! 试题程序 #include <stdio. h #include <conio. h> #include <stdlib. h> typedef struct struct aa next 1 NODE NODE *h) 唯C动力(htp/www.clearner.net)版权所有
第 17 页 共 75 页 唯 C 动力(http://www.clearner.net)版权所有 请改正程序中的错误,使程序能得出正确的结果。 注意,不要改多 main 函数,不得增行或删行,也不得更改程序的结构! 试题程序 #include <stdio.h> #define N 10 /**********found***********/ void fun(int a[],int m) {int low=0,high=N-1,mid; while(low<=high) {mid=(low+high)/2; if (m<a[mid]) high=mid-1; /**********found***********/ else if (m>=a[mid]) low=mid+1; else return(mid); } return(-1); } main() {int i,a[N]={-3,4,7,9,13,45,67,89,100,180},k,m; printf("a 数组中的数据如下:"); for(i=0;i<N;i++) printf("%d",a[i]); printf("Enter m:");scanf("%d",&m); k=fun(a,m); if(k>=0) printf("m=%d,index=%d\n",m,k); else printf("Not be found!\n"); } 21、给定程序是建立一个带头结点的单向链表,并用随机函数为各结点数据域赋值。函数 fun 的作用是求出单向链表结点(不包括头结点)数据域中的最大值, 并且作为函数值返回。 请改正函数 fun 中指定部位的错误, 使它能得出正确的结果。 注意: 不要改动 main 函数, 不得增行或删行, 也不得更改程序的结构! 试题程序: #include <stdio.h> #include <conio.h> #include <stdlib.h> typedef struct aa { int data; struct aa *next; } NODE; fun ( NODE *h )
18页共75页 NODE / p=h->next if(p->data)max max=p->data /*****水*水fOnd*****/ p=p->next 7/p=h->next return max outresult(int s, FIle *pf) fprintf (pf, \nThe max in link: %d\n, s) NODE *creatlink(int n, int m) NODE*h,郫,*s,* h=p=(NODE *)malloc(sizeof (NODE)): h->data=9999 for(i=l: i-n: i++) I s=(NODE *)malloc(sizeof (NODE)) s->data=rand o%m: s->next=p->next p->next=NULL return h outlink(NODE *h, FILE pf) I NODE *p fprintf (pf, " \nTHE LIST: \n\n HEAD") while(p) t fprintf(pf, "->%d, p>data): p=p->next: fprintf(pf,"Ⅶn") maino I NODE * head int m head=creatlink(12, 100) outlink(head, stdout) printf ("\nTHE RESULT \n"): outresult(m, stdout) 唯C动力(htp/www.clearner.net)版权所有
第 18 页 共 75 页 唯 C 动力(http://www.clearner.net)版权所有 { int max=-1; NODE *p; /***********found**********/ p=h->next; //p=h ; while(p) { if(p->data>max ) max=p->data; /***********found**********/ p=p->next; //p=h->next ; } return max; } outresult(int s, FILE *pf) { fprintf(pf,"\nThe max in link : %d\n",s);} NODE *creatlink(int n, int m) { NODE *h, *p, *s, *q; int i, x; h=p=(NODE *)malloc(sizeof(NODE));h->data=9999; for(i=1; i<=n; i++) { s=(NODE *)malloc(sizeof(NODE)); s->data=rand()%m; s->next=p->next; p->next=s; p=p->next; } p->next=NULL; return h; } outlink(NODE *h, FILE *pf) { NODE *p; p=h->next; fprintf(pf,"\nTHE LIST :\n\n HEAD "); while(p) { fprintf(pf,"->%d ",p->data); p=p->next; } fprintf(pf,"\n"); } main() { NODE *head; int m; clrscr(); head=creatlink(12, 100); outlink(head , stdout); m=fun(head); printf("\nTHE RESULT :\n"); outresult(m, stdout);
第19页共75页 22、下列给定的程序中,函数fun的功能是:根据整形参m,计算如下公式的值 y=1+1/(2×2)+1/3×3)+1/4×4)+…+1/m×m) 例如若m中的值为5,则应输出:1463611 请改正程序中的错误,使它能得到正确的结果 注意不要改动main函数不得增行或删行,也不得更改程序的结构! 试题程序 #include<conio h> #include<stdio h> double fun(int m) /*****水**fOⅡnd******/ double y=1.0 for(i=2;<m;H++) /*****水*水fOⅡnd******/ +=1(*1) return(y) i int n=5 corsaro printf("n The result is %1f\n", fun(n)); 23、下列给定的程序中,函数fun的功能是:按以下递归公式求函数值。 10 fun(n)=+ un(n-1)+2(n>1) 例如,当给n输入5时,函数值为18;当n输入3时,函数值为14, 请改正程序中的错误,使它能得出正确的结果 注意不要改动main函数不得增行或删行,也不得更改程序的结构 试题程序 #include <stdio. h fun(n) /*本*客* found*幸本*容* f(n=1) 唯C动力(htp:/www.clearner.net)版权所有
第 19 页 共 75 页 唯 C 动力(http://www.clearner.net)版权所有 } 22、下列给定的程序中,函数 fun 的功能是:根据整形参 m,计算如下公式的值. y=1+1/(2×2)+1/(3×3)+1/(4×4)+……+1/(m×m) 例如,若 m 中的值为 5,则应输出:1.463611 请改正程序中的错误,使它能得到正确的结果. 注意:不要改动 main 函数,不得增行或删行,也不得更改程序的结构! 试题程序: #include<conio.h> #include<stdio.h> double fun(int m) { /***********found**********/ double y =1.0; int i; for(i=2;i<m;i++) /***********found**********/ y+=1 /(i*1); return(y); } main() { int n=5; clrscr(); printf(“\nThe result is %1f\n”, fun(n)); } 23、下列给定的程序中,函数 fun 的功能是:按以下递归公式求函数值。 ┏10 (n=1) fun(n)= ┫ ┺ fun(n-1)+2 (n>1) 例如,当给 n 输入 5 时,函数值为 18;当 n 输入 3 时,函数值为 14, 请改正程序中的错误,使它能得出正确的结果. 注意:不要改动 main 函数,不得增行或删行,也不得更改程序的结构。 试题程序: #include <stdio. h> /***********found**********/ fun(n) { int c; /***********found**********/ if(n=1) c=10;
第20页共75页 c=fn(n-1)+2; return(c) printf("Enter n ) scanf("%d",&n) printf(" The result %d\nin, fun(n)) 24、下列给定程序中,函数fun的功能是:从s所指字符串中,找出t所指子串的个数作为 函数值返回。例如:当s所指字符串中的内容为 abcdabfab,t所指字符串的内容为ab,则函数 返回整数3。 请改正程序中的错误,使它能得出正确的结果。 注意:不要改动main函数,不得增行或删行,也不得更改程序的结构。 试题程序 #include<conio h> #include<stdio h> #include<string h int fun(char *s, char *t int n; char p,r while(s) while(*r) /* else break if(r==0°) har s[ 100] t[ 100],; int m printf("nPlease enter string S: ) scanf( %s"s); 唯C动力(htp/www.clearner.net)版权所有
第 20 页 共 75 页 唯 C 动力(http://www.clearner.net)版权所有 else c=fun(n-1)+2; return(c): } main() { int n; printf(“Enter n ; ”); scanf( “%d”, &n); printf(“The result : %d\n\n” ,fun(n)); } 24、下列给定程序中,函数 fun 的功能是:从 s 所指字符串中,找出 t 所指子串的个数作为 函数值返回。例如:当 s 所指字符串中的内容为 abcdabfab,t 所指字符串的内容为 ab,则函数 返回整数 3。 请改正程序中的错误,使它能得出正确的结果。 注意:不要改动 main 函数,不得增行或删行,也不得更改程序的结构。 试题程序: #include<conio.h> #include<stdio.h> #include<string.h> int fun(char *s,char *t) { int n; char *p,*r; n=0; while(*s) { p=s; r=t; while(*r) /***********found**********/ if(r=p) {r++;p++} else break; /***********found**********/ if(r==’\0’) n++; s++; } return n; } main() { char s[100], t[100],; int m; clrscr(); printf(“\nPlease enter string S:”); scanf(“%s”,s);