第3部分 补充习题和模拟试卷 第3部分补充习题和模拟试卷 3.1补 充 习 题 3.1.1程序填空题 1. 下面程序是输入n(n<13),计算1!+3!+5!+…+n!的值。 #include #include int main() { long int f,s=0; int i,j,n; scanf("%d",&n); for(i=1;i<=n;[1]) { f=1; for(j=1;[2];j++) [3]; s=s+f; } printf("n=%d,s=%ld\n",n,s); system("pause"); return 0; } 答案: [1] i+=2 或 i=i+2 或 i++,i++ [2] j<=i 或 i>=j 或 jj [3] f= f * j 2. 已定义一个含有N个元素的数组s,函数fun1()的功能是按顺序分别赋予各元素从2开始的偶数,函数fun2()则按顺序每M个元素求一个平均值, 并将该值存放在数组w中。 #include #include #define N 30 #define M 5 void fun1(double s[]) { int k,i; for(k=2,i=0;i #include #include void strcopy(char *str1,char *str2,int m) { char *p1,*p2; [1]; p2=str2; while(*p1) [2]; [3]; } int main() { int i,m; char str1[80],str2[80]; gets(str1); scanf("%d",&m); [4]; puts(str1); puts(str2); system("pause"); return 0; } 答案: [1] p1= str1 + m [2] *p2++=*p1++ 或 *(p2++)=*(p1++) 或 *p2=*p1,p2++,p1++ 或 *p2=*p1++,p2++ 或 *p2++=*p1,p1++ [3] *p2='\0' 或 *p2=0 或 *p2=NULL [4] strcopy(str1,str2,m) 4. 从键盘上输入一个字符串, 将该字符串升序排列后输出到文件test.txt中,然后从该文件读出字符串并显示出来。 #include #include #include int main() { FILE*fp; char t,str[100],str1[100]; int n,i,j; if((fp=fopen("test.txt","w"))==NULL) { printf("can't open this file.\n"); exit(0); } printf("input a string:\n"); gets(str); [1]; for(i=0;[2];i++) for(j=0;ji 或 i<-1+n 或 ii 或 -1+n>i 或 i<=n-1 或 n-1>=i 或-1+n>=i [3] str[j]>str[ j + 1 ] 或 str[ j + 1 ] #include #define N 30 int main() { int a[N][N]; int i,j,n; scanf("%d",&n); for(i=0;ij&&i+jn-1 或 j>i && i + j>n-1 或 ii && i + n-1 #include int main() { int m,n,k,i=0; for(m=100;m<=1000;m++) { [1]; n=m; do { k=k+[2]; n=n/10; } [3]; if(k%15==0) { printf("%5d",m); i++; if(i%10==0) [4]; } } system("pause"); return 0; } 答案: [1] k=0 [2] n%10 或 n-n/10*10 或 n-10*(n/10) [3] while(n>0) 或 while(0 #include #define N 80 int main() { char str[N],ch; int i,k=0; gets([1]); ch=getchar(); for(i=0;[2];i++) if(str[i]!=ch) { [3]; k++; } [4]; puts(str); system("pause"); return 0; } 答案: [1] str [2] str[i]!='\0' 或 str[i]!=NULL 或 str[i]!=0 或 str[i] [3] str[k]=str[i] 或 *(str+k)=*(str+i) 或 str[k]=*(str+i) 或 *(str+k)=str[i] [4] str[k]='\0' 或 *(str+k)='\0' 或 str[k]=NULL 或 str[k]=0 或 *(str+k)=0 或 *(str+k)=NULL 8. 输入一字符串,删除该字符串中的所有数字字符。 #include #include #define N 80 void delnum(char *s) { int i,j; for(i=0,j=0;[1]'\0';i++) if(s[i]<'0'[2] s[i]>'9') { [3]; j++; } s[j]='\0'; } int main() { char item[N]; printf("\输入一个字符串:\n"); gets(item); [4]; printf("\n%s",item); system("pause"); return 0; } 答案: [1] s[i]!= 或 *(s+i)!= 或 *(i+s)!= [2] || [3] s[j]=s[i] 或 *(s+j)=*(s+i) 或 s[j]=*(s+i) 或 *(s+j)=s[i] [4] delnum(item) 9. 输入字符串,统计该字符串中的字母、数字、空格和其他字符的个数。 #include #include [1]; int main() { char s1[80]; int a[4]={0}; int k; gets(s1); [2]; puts(s1); for(k=0;k<4;k++) printf("%4d",a[k]); system("pause"); return 0; } void fun(char s[],int b[]) { int i; for(i=0;s[i]!='\0';i++) if('a'<=s[i]&&s[i]<='z'||'A'<=s[i]&&s[i]<='Z') b[0]++; else if([3]) b[1]++; else if([4]) b[2]++; else b[3]++; } 答案: [1] void fun(char s[],int b[])或void fun(char [],int []) [2] fun(s1,a) [3] '0'<=s[i] && s[i]<='9' 或 s[i]>='0' && s[i]<='9' 或 '0'<=s[i] && '9'>=s[i] 或 s[i]>='0' && '9'>=s[i] 或 48<=s[i] && s[i]<=57 或 s[i]>=48 && s[i]<=57 或 48<=s[i] && 57>=s[i] 或 s[i]>=48 && 57>=s[i] 或 !(x < 48 || x > 57) 或 !(x < '0' || x > '9') [4] s[i] == ' ' 或 s[i] == 32 10. 数组中元素已递增排序,下面的函数为二分法查找key值,若找到key则返回对应的下标,否则返回-1。 #include #include #define N 10 int fun(int a[],int n,int key) { int low,high,mid; low=0; high=n-1; while([1]) { mid=(low+high)/2; if(keya[mid]) [3]; else [4]; } return -1; } int main() { int a[N]={1,2,3,4,5,6,7,8,9,10}; int b,c; scanf("%d",&b); c=fun(a,N,b); if(c==-1) printf("not found"); else printf("position %d\n",c); system("pause"); return 0; } 答案: [1] low<=high 或 high>=low [2] high=mid-1 [3] low=mid+1 [4] return mid 或 return(mid) 11. 计算并输出high以内最大的10个素数之和,high由主函数传给fun()函数,若high的值为100,则函数的值为732。 #include #include #include int fun(inthigh) { int sum = 0,n=0,j,yes; while((high >= 2) && ([1])) { yes = 1; for(j=2; j<=high/2; j++) if([2]) { yes=0; break; } if(yes) { sum +=high; n++; } high--; } [3]; } int main() { int n; scanf("%d",&n); printf("%d\n", fun(n)); system("pause"); return 0; } 答案: [1] n<10 或 10>n [2] high%j == 0 或 !(high%j) [3] returnsum 或 returnv(sum) 12. 将s所指字符串的正序和反序进行连接,形成一个新串放在t所指的数组中。例如,当s串为"ABCD"时,则t串的内容应为"ABCDDCBA"。 #include #include void fun(char*s, char*t) { int i, d; d =[1]; for(i = 0; i #include #define N 10 void input(int [],int); void max_min(int [],n); void output(int [],int); int main() { int n,number[N]; scanf("%d",&n); input(number,n); max_min(number,n); output(number,n); system("pause"); return 0; } void input(int number[],int n) { int i; for(i=0;[1];i++) scanf("%d",&number[i]); } void max_min(int array[],int n)