南开二级C语言上机100题 1、改错题1 下列给定程序的功能是:读入一个整数(2<=k《=10000,打印它的所有质因子(即所有为素数的因子) 例如,若输入整数2310,则应输出2、3、5、7、11。 请改正程序中的错误,使程序能得出正确的结果。 注意,不要改多main函数,不得增行或删行,也不得更改程序的结构! 试题程序: # include< conIo.上 #include <stdio h IsPrime( int n) 一去掉分号 fint 1, m; m=1; for( i=2; i<n; 1++) 水客*客**水*称水*客*水*客水*客*水*容水 if! (n%1) -if(!(n %i)) m=0; break;i return(m) i int j, k; corsaro printf("nPlease enter an integer number between 2 and 10000: ") scanf("%d", &k); printf("nnThe prime factor(s)of%d is(are): " k) for (=2 j<=k:j++ if((! (k%j)&&(ls Prime() printf("n%4d"j); printf("n"); 2、编程题1 m个人的成绩存放在 score数组中,请编写函数fun,它的功能是:将低于平均分的人数作为函数值行会, 将低于平均分的分数放在 below所指的数组中。 例如,但 score数组的数据为10、20、30、40、50、60、70、80、90时,函数返回的人数应该时4, below 中的数据应为10、20、30、40 注意:部分源程序给出如下 请勿改动主函数man和其他函数中的任何内容仅在函数fun的花括号中填入所编写的若干语句 试题程序: #include <conio. h> #include <stdio h #include <string. h> 第1页共16页
第 1 页 共 16 页 南开二级 C 语言上机 100 题 1、 改错题 1 下列给定程序的功能是:读入一个整数(2<=k《=10000》,打印它的所有质因子(即所有为素数的因子)。 例如,若输入整数 2310,则应输出 2、3、5、7、11。 请改正程序中的错误,使程序能得出正确的结果。 注意,不要改多 main 函数,不得增行或删行,也不得更改程序的结构! 试题程序: #include <conio.h> #include <stdio.h> ******************************* IsPrime(int n); ——————去掉分号 {int i,m; m=1; for ( i=2; i<n; i++) ****************************** if !(n%i) ——————if (!(n%i)) { m=0; break; } return (m); } main() { int j,k; clrscr(); printf("nPlease enter an integer number between 2 and 10000:");scanf("%d",&k); printf("nnThe prime factor(s) of %d is (are):",k); for (j=2;j<=k;j++) if ((!(k%j))&&(IsPrime(j)) printf("n %4d",j); printf("n"); ) 2、 编程题 1 m 个人的成绩存放在 score 数组中,请编写函数 fun,它的功能是:将低于平均分的人数作为函数值行会, 将低于平均分的分数放在 below 所指的数组中。 例如,但 score 数组的数据为 10、20、30、40、50、60、70、80、90 时,函数返回的人数应该时 4,below 中的数据应为 10、20、30、40。 注意:部分源程序给出如下。 请勿改动主函数 main 和其他函数中的任何内容仅在函数 fun 的花括号中填入所编写的若干语句。 试题程序: #include <conio.h> #include <stdio.h> #include <string.h>
int fun(int score[, int m, int below) -int ik=o: float aver=0 for(i=0;<m;i++) ave +=(scoreD); if(score[]<aver) i below=score1 retern(k): i maino fint i, n, below[]; Int score[9}={10,20,30,40,50,60,7080,90}; clrscro fun(score, 9, below) printf("nBelow the average score are: " for(i=0; i<n; i++) printf("%d",below)) 3、改错2 下列给定程序中,函数fun的功能是:逐个比较a、b两个字符串对应位置中的字符,把ASCI值大或等于 的字符一次存放到c数组中,形成一个新的字符串。例如,若a中的字符串为 aBCDefgh. b中的字符串为 ABcd,则c中的字符串为: aBcdefgh 请改正程序中的错误,使程序能得出正确的结果 注意,不要改多main函数,不得增行或删行,也不得更改程序的结构 试题程序 #include <studio. h> #include <string h> ****水*称****客水**客水*称*客客水称*水**水* void,fun(char*p,char*q, char *c)——去分号 fint k=1; 1改为0 while( pl=* q) !=改为 {if(°p<*q)c[k=*q; 小于号改为小于等于 else ck]=*p f(p)p++ k++ 第2页共16页
第 2 页 共 16 页 int fun (int score[],int m, int below[]) { ——————int i,k=0;float aver=0; for(i=0;i<m;i++) aver+=(score[i]); aver/=m; for (i=0;i<m;i++) if (score[i]<aver) {below[k]=score[i]; k++; retern(k);} } main() {int i, n, below[9]; int score [9]={10,20,30,40,50,60,70,80,90}; clrscr(); n=fun(score,9,below); printf("nBelow the average score are:"); for (i=0;i<n;i++) printf ("%d",below[i]); } 3、 改错 2 下列给定程序中,函数 fun 的功能是:逐个比较 a、b 两个字符串对应位置中的字符,把 ASCII 值大或等于 的字符一次存放到 c 数组中,形成一个新的字符串。例如,若 a 中的字符串为 aBCDeFgH,b 中的字符串为: ABcd,则 c 中的字符串为:aBcdeFgh。 请改正程序中的错误,使程序能得出正确的结果。 注意,不要改多 main 函数,不得增行或删行,也不得更改程序的结构! 试题程序 #include <studio.h> #include <string.h> **************************************** void; fun(char *p,char *q,char *c)——去分号 *************************************** {int k=1; ——1 改为 0 *************************************** while(*p!=*q) —— != 改为 || *************************************** {if (*p<*q) c[k]=*q; ——小于号改为小于等于 else c[k]=*p; if (*p) p++; if (*q) q++; k++; } }
ichar a[10]="aBCDeFgh", b[10="ABcd" c[80]=() printf("The string a: ") puts(a); printf("The string b: ) puts(b); printf("The result: ) puts(c) 改错3 下列给定程序中,函数fun的功能是:依次取出字符串中所有数字字符,形成新的字符串,并取代原字符 请改正程序中的错误,使程序能得出正确的结果 注意,不要改多main函数,不得增行或删行,也不得更改程序的结构! 试题程序 #include <stdio h #include <conio. h> void fun(char *s) for(i=0j=0sl="03i++) if(s[>=0&&s可]9 s[=s[O] 丁改为j++ s[]="VO" ]=0 i char item[80]: clrscro printf("\nEnter a string: ) gets(item); printf("lnln The string is: \%sIn",item) fun(item) printf("\n\n The string of changing is: \%sn", item); 5、改错4 下列给定程序中,函数fun的功能是:分别铜级字符串中大写字母和小写字母的个数。例如,给字符串 输入: AAaabBbl23 Cccccd,则应该输出结果: upper=6ower=8。 请改正程序中的错误,使程序能得出正确的结果 注意,不要改多main函数,不得增行或删行,也不得更改程序的结构 试题程序 #include <conio. h> #include <stdio. h> 第3页共16页
第 3 页 共 16 页 main() {char a[10]="aBCDeFgh",b[10]="ABcd",c[80]={''}; fun(a,b,c); printf("The string a:"); puts(a); printf("The string b:"); puts(b); printf("The result:"); puts(c); } 4、 改错 3 下列给定程序中,函数 fun 的功能是:依次取出字符串中所有数字字符,形成新的字符串,并取代原字符 串。 请改正程序中的错误,使程序能得出正确的结果。 注意,不要改多 main 函数,不得增行或删行,也不得更改程序的结构! 试题程序 #include <stdio.h> #include <conio.h> void fun (char *s) { int i,j; for (i=0,j=0;s[i]!='\0';i++) **************************************** if (s[i]>='0' && s[i]<='9') s[j]=s[i]; ——————j 改为 j++ *************************************** s[j]="\0"; ——————s[j]='\0' } main() {char item[80]; clrscr(); printf("\nEnter a string:");gets(item); printf("\n\nThe string is:\%s\n",item); fun (item); printf("\n\nThe string of changing is :\%s\n",item); } 5、 改错 4 下列给定程序中,函数 fun 的功能是:分别铜级字符串中大写字母和小写字母的个数。例如,给字符串 s 输入:AAaaBBb123CCccccd,则应该输出结果:upper=6,lower=8。 请改正程序中的错误,使程序能得出正确的结果。 注意,不要改多 main 函数,不得增行或删行,也不得更改程序的结构! 试题程序 #include <conio.h> #include <stdio.h> **********************************
void fun(char*s,inta,intb)-—a应为*ab应为+b if(*s>=A'&&*s<=Z) if(*s>=a&&*s<=z) 本幸***水**水*本*客*客*水*水水*客 b++ *b)++ maino i char s[ 100]; int upper=0, lower=0 Insco printf("nPlease a string: ");gets(s) fun(s, &upper, &lower); printf("n upper=%d lower=%odn",upper, lower) 6、改错5 假定整数数列中的数不重复,并存放在数组中。下列给定程序中,函数fun的功能是:删除数列中值为x 的元素。n中存放的是数列中元素的个数 请改正程序中的错误,使程序能得出正确的结果。 注意,不要改多main函数,不得增行或删行,也不得更改程序的结构 试题程序 #include <stdio. h> #define n20 fun (int*a, int n, int x) Hint P=0, i; an=x while(xI=alpD) if(p==n)return-I ( for(i=p i<n; 1++) a[i+1=a[il a=a[计+1l; return n-1 maino { int WIN}={-3,0,1,5,79910,15,30,90},xni printf("The original data n") 第4页共16页
第 4 页 共 16 页 void fun (char *s,int a, int b) ————a 应为*a,b 应为*b { while(*s) { if (*s>='A' && *s<='Z') *********************************** a++; ————(*a)++; if (*s>='a' && *s<='z') ************************************ b++; ————(*b)++; s++; } } main() { char s[100];int upper=0,lower=0; clrscr(); printf("nPlease a string:");gets(s); fun(s,&upper, &lower); printf("n upper=%d lower=%dn",upper,lower); } 6、 改错 5 假定整数数列中的数不重复,并存放在数组中。下列给定程序中,函数 fun 的功能是:删除数列中值为 x 的元素。n 中存放的是数列中元素的个数。 请改正程序中的错误,使程序能得出正确的结果。 注意,不要改多 main 函数,不得增行或删行,也不得更改程序的结构! 试题程序 #include <stdio.h> #define N 20 fun (int *a,int n, int x) {int p=0,i; a[n]=x; while (x!=a[p]) p=p+1; if (p==n) return -1; else {for (i=p;i<n;i++) ****************************************** a[i+1]=a[i]; ————a[i]=a[i+1]; return n-1; } } main() {int w[N]={-3,0,1,5,7,99,10,15,30,90},x,n,i; n=10; printf("The original data:n");
for(i=0 i<n; i++) printf("%5d"w); printf("nInput x( to delete): ") scanf("%d", &x); printf("Delete: %dn,x) fun( w, n, x) if (n==-1)printf("***Nor be found! ***nn"); i printf("The data after deleted: n"); for(i=0, i<n; i++) printf("%5d" w): printf("nn"); 7、改错6 下列给定程序中,函数fun的功能是:根据整型形参m的值,计算如下公式的值。t=1-1/2×2-1/3×3-…-1/m m 例如,若m中的值为5,则应输出:0.536389。 请改正程序中的错误,使程序能得出正确的结果。 注意,不要改多main函数,不得增行或删行,也不得更改程序的结构! 试题程序: #include <conio. h> (int m) i double y=1. 0 Int I 客水客水*客水*水本涂水*水亦*称客幸客客水*水客水客水水容容幸*宗 for(i=2; i<m; 1++) “<”改为“<=” 孝水客水客水容水**水水客水*称*水水水客幸容客水称*客水水* “1”改为“1.0” return(y) fint n=5 clrscro printf("\nRhe result is %1fn", fun(n)) 8、改错7 下列给定程序中,函数fun的功能是:用选择法对数组中的n个元素按从小到大的顺序进行排序 请改正程序中的错误,使程序能得出正确的结果。 注意,不要改多main函数,不得增行或删行,也不得更改程序的结构 试题程序: #include <stdio. h> #define n 20 第5页共16页
第 5 页 共 16 页 for (i=0;i<n;i++) printf("%5d",w[i]); printf("nInput x (to delete):");scanf("%d",&x); printf("Delete:%dn",x); n=fun(w,n,x); if (n==-1) printf("***Nor be found!***nn"); else {printf("The data after deleted:n"); for (i=0,i<n;i++) printf("%5d",w[i]);printf("nn"); } } 7、 改错 6 下列给定程序中,函数 fun 的功能是:根据整型形参 m 的值,计算如下公式的值。t=1-1/2×2-1/3×3-…-1/m ×m 例如,若 m 中的值为 5,则应输出:0.536389。 请改正程序中的错误,使程序能得出正确的结果。 注意,不要改多 main 函数,不得增行或删行,也不得更改程序的结构! 试题程序: #include <conio.h> #include <stdio.h> double fun (int m) { double y=1.0; int i; *************************************** for (i=2;i<m;i++) ———— “<”改为“<=” *************************************** y-=1/(i*i); ————“1”改为“1.0” return(y); } main () {int n=5; clrscr(); printf("\nRhe result is %1f\n",fun(n)); } 8、 改错 7 下列给定程序中,函数 fun 的功能是:用选择法对数组中的 n 个元素按从小到大的顺序进行排序。 请改正程序中的错误,使程序能得出正确的结果。 注意,不要改多 main 函数,不得增行或删行,也不得更改程序的结构! 试题程序: #include <stdio.h> #define N 20