内容正文:
编写说明:本冲刺卷严格依据湖南省计算机应用类高考考纲编写,依托《C语言程序设计》(高等教育出版社第5版),聚焦高三考生冲刺需求,助力高效提分。内容上深度覆盖考纲掌握、理解层级考点,既系统梳理构建知识框架,又强化应用能力训练;同时结合近五年高考真题,精准把握高频考点、命题趋势与题型特点,确保贴合高考方向。
湖南省计算机应用类
《C语言程序设计》
高频考点冲刺卷(二)解析版
时间:60分钟 总分:100分
班级:_________ 姓名:________ 学号:________ 成绩:_________
一、单选题(在本题的每一小题的备选答案中,只有一个答案是正确的。本大题共5小题,每小题2分,共10分)
1.若有以下定义,则对a数组元素地址的正确引用是( )
int a[5],*p=a;
A.p+5 B.*a+1 C.&a+1 D.&a[0]
2.下列程序运行时,输入:12345,则程序的输出结果是( )
main()
{ char a,b;
scanf("%c%*3c%c",&a,&b);
printf("%c%c
",a,b); }
A.14 B.15 C.123 D.12
3.若有定义:double a=22;int i=0,k=18;,则下列选项中不符合C语言规定的赋值语句是( )
A.a=a++,i++; B.i=(a+k)<=(i+k); C.i=a%11; D.i=!a
4.下列选项中,与语句:if(a==1) a=b;else a++;功能不同的switch语句是( )
A.switch(a){case 1:a=b;break;default:a++;}
B.switch(a==1) {case 0:a=b; break; case 1 :a++;}
C.switch (a) {default:a++ ;break; case 1:a=b;}
D.switch(a==1) {case 1:a=b;break; case 0:a++;}
5.设有定义:double a[10],*s=a;,则以下选项中能代表数组元素a[3]的是( )
A.(*s)[3] B.*(s+3) C.*s[3] D.*s+3
二、程序分析题(本大题共3小题,共70分)
1.阅读程序,写出运行结果(每空2分,共14分)
(1)下列程序的运行结果是_________________。
#include <stdio.h>
main()
{
int a[2][4]={1,2,3,4,5,6,7,8};
int col_max[4],col_aver[4];
int i,j,max,sum;
for(i=0;i<4;i++)
{ max=a[0][i],sum=0;
for(j=0;j<2;j++)
{
if(max<a[j][i])
max=a[j][i];
sum+=a[j][i];
}
col_max[i]=max;
col_aver[i]=sum/2;
}
for(i=0;i<4;i++)
{ printf("max=%d,aver=%d
",col_max[i],col_aver[i]); }
}
(2)下列程序的运行结果是_________________。
#include <stdio.h>
int global_flag = 0;
int* process(int *arr, int idx)
{
global_flag++;
if (global_flag % 2 == 0)
{
arr[idx] += 10;
return &arr[idx + 1];
}
else
{
arr[idx] -= 5;
return &arr[idx];
}
}
main() {
int data[] = {1, 2, 3, 4, 5};
int *ptrs[5];
int sum = 0,i;
for (i = 0; i < 4; i++) {
ptrs[i] = process(data, i);
*ptrs[i] += i;
}
for (i = 0; i < 5; i++) {
sum += data[i];
}
printf("%d
", sum);
}
(3)下列程序的运行结果是_________________。
#include<stdio.h>
#define M 5
main()
{
int a[M]={1,2,3,4,5};
int i,j,t;
i=1;
j=M-1;
while(i<j)
{ t=*(a+i);
*(a+i)=*(a+j);
*(a+j)=t;
i++;j--;
}
for(i=0;i<M;i++)
printf("%d",*(a+i));
}
(4)下列程序的运行结果是_________________。
#include<stdio.h>
#include<stdlib.h>
void fun(int *p1,int *s)
{ int *t;
t=(int *)malloc(2*sizeof(int));
*t=*p1+*p1++;
*(t+1)=*p1+*p1;
s=t;}
main()
{int a[2]={1,2},b[2]={0};
fun(a,b);
printf("%d,%d
",b[0],b[1]);}
(5)下列程序的运行结果是_________________。
#include "stdio.h"
main()
{ int b[2][3]={1,3,5,7,9,11};
int *a[2][3];
int i,j;
int * *p,m;
for(i=0;i<2;i++)
for(j=0;j<3;j++)
a[i][j]=*(b+i)+j;
p=a[0];
for(m=0;m<6;m++)
{ printf("%4d",**p);
p++; }
}
(6)下列程序的运行结果是_________________。
#include <stdio.h>
int process(int *arr, int n) {
int sum,i;
if(n <= 0) return 0;
sum = arr[0];
for(i = 0; i < n-1; i++) {
arr[i] = arr[i+1]; }
return sum + process(arr, n-1);}
int main() {
int data[4] = {1, 2, 3, 4};
int result = process(data, 4);
printf("%d
", result);
return 0;}
(7)下列程序的运行结果是_________________。
#include<stdio.h>
main()
{ int a=28,b;
char s[10],*p;
p=s;
do{b=a%16;
if(b<10) *p=b+48;
else *p=b+55;
p++;
a=a/5;}while(a>0);
*p='\0';
puts(s);}
2.程序填空。按照题目要求,将正确内容填入程序空白处,使程序完整(每空2分,共36分)。
(1)在此程序中,函数fun()的功能是将a所指3x5的矩阵中第k列的元素左移到第0列,第k列以后的每列元素行依次左移,原来左边的各列依次绕到右边。
例如,有下列矩阵:
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
若k为2,程序执行结果为
3 4 5 1 2
3 4 5 1 2
3 4 5 1 2
请在程序的下划线处填入正确的内容,使程序得出正确的结果。(4空)
#include <stdio.h>
#define M 3
#define N 5
void fun(int (*a)[N],int k)
{int i,j,p,temp;
for(p=1;_______①______;p++)
for(i=0;i<M;i++)
{temp=a[i][0];
for(j=0;j<N-1;j++)
a[i][j]=________②_________;
_______③_______=temp;
}
}
main()
{int x[M][N]={{1,2,3,4,5},{1,2,3,4,5},{1,2,3,4,5}},i,j;
printf("The array before mov- ing:
");
for(i=0;i<M;i++)
{for(j=0;j<N;j++)
printf("% 3d",x[i][j]);
printf("
");
}
_________④_________;
printf("The array after mov- ing:
");
for(i=0;i<M;i++)
{for(j=0;j<N;j++)
printf("% 3d",x[i][j]);
printf("
");
}
}
(2)下列程序的功能是将“yye.bin”文件中的数据按平均营业额由高到低进行排序,并将排序后的数据写入文件“sort.bin” 中。(5空)
#include <stdio.h>
#include <stdlib.h>
#define N 3 //N个商品
#define M 4 //M个季度
struct gs
{
char bh[6]; //商品编号
char mc[10]; //商品名称
float yue[M]; //季度营业额
float ave; //平均营业额
}goods[N],temp;
void main()
{
FILE *fp;
int i,j,n;
if(___________①____________)
{
printf("不能打开文件!
");
exit(0);
}
for(i=0;fread(___________②__________)!=0;i++);
fclose(fp);
n=i;
for(i=1;i<=n;i++)
for(j=0;j<n-i;j++)
{
if(__________③__________)
{
temp=goods[j];
goods[j]=goods[j+1];
goods[j+1]=temp;
}
}
if((fp=fopen("sort.bin",______④_____))==NULL)
{
printf("不能打开文件,写文件失败!
");
exit(0);
}
printf("编号\t名称\t1季度\t2季度\t3季度\t4季度\t平均额
");
for(i=0;i<n;i++)
{
fwrite(&goods[i],sizeof(struct gs),1,fp);
printf("%s\t%s\t%7.2f\t%7.2f\t%7.2f\t%7.2f\t%7.2f
",
goods[i].bh,goods[i].mc,goods[i].yue[0],goods[i].yue[1],
goods[i].yue[2],goods[i].yue[3],goods[i].ave);
}
_________⑤_________;
}
(3)在此程序中,函数fun()的功能是在形参S所指字符串中寻找与参数c相同的字符,并在其后插入一个与之相同的字符,若找不到相同的字符则不做任何处理。请在程序的下划线处填入正确的内容,使程序得出正确的结果。(4空)
#include <stdio.h>
void fun(char * s, char c)
{ int i,j,n;
for (i=0; __________①________;i++)
if(s[i]==c)
{
______②______;
while(______③_______!='\0')
n++;
for(j=i+n+1;j>i;
j--)
s[j+1]=s[j];
s[j+1]=_______④_______;
i=i+1;
}
}
main()
{
char s[80]="baacda", c;
printf("
The string:s
",s);
printf("
Input a character:");
scanf("%c",&c);
fun(s,c);
printf("
The result is:%s
",s);
}
(4)给定程序中,函数fun的功能是在形参ss所指字符串数组中,删除所有串长超过k的字符串,函数返回所剩字符串的个数。ss所指字符串数组中共有N个字符串,且串长小于M。请在程序的下划线处填入正确的内容,使程序得出正确的结果。(3空)
#include <stdio.h>
#include <string.h>
#define N 5
#define M 10
int fun(char (*ss)[M], int k)
{ int i,j=0,len;
for(i=0; i<N; i++)
{ len=______①________;
if(len<=_____②_____)
_________③_________;
}
return j;
}
main()
{ char x[N][M]={"Beijing","Shanghai","Tianjing","Nanjing","Wuhan"};
int i,f;
printf("
The original string
");
for(i=0;i<N;i++)puts(x[i]); printf("
");
f=fun(x,7);
printf("The string witch length is less than or equal to 7 :
");
for(i=0; i<f; i++) puts(x[i]);printf("
");
getchar();
}
(5)下面程序的功能是用递归法将一个整数存放到一个字符数组中,按逆序存放。如将483存放成384。(2空)
#include<stdio.h>
void convert(char *a,int n)
{ int i;
if((i=n/10)!=0)
convert(_______①_______);
*a=________②________;
}
main()
{
int number;
char str[10]=" ";
scanf("%d",&number);
convert(str,number);
puts(str);
}
3.阅读程序:修改程序中的错误,不得增行或删行,也不得更改程序结构,请在作答处指出错误代码所在的行号,并给出该行修改后的程序代码。(每空2分,共20分)。
(1)在此程序中,函数fun()的功能是将s所指字符串中最后一次出现的与t1所指字符串相同的子字符串替换成t2所指字符串,所形成的新字符串存放在w所指的数组中。要求t1和t2所指字符串的长度相同。请改正程序中的错误,使它能得出正确的结果。以下程序只允许修改两行。
L1 #include <conio.h>
L2 #include <stdio.h>
L3 #include <string.h>
L4 void fun (char * s,char * t1, char *t2,char *w)
L5 { char *p,*r,*a;
L6 strcpy(w,s);
L7 while(w)
L8 { p=w;
L9 r=t1;
L10 while (*r)
L11 if(*r==*p)
L12 { r++; p++; }
L13 else
L14 { break; }
L15 if(*r='\0')
L16 a=w;w++; }
L17 r=t2;
L18 while (*r)
L19 { *a=*r;
L20 a++;r++; } }
L21 main ()
L22 { char s[100],t1[100],t2[100],w[100];
L23 printf("
Please enter string s:");
L24 scanf("%s",s);
L25 printf("
Please enter substring t1:");
L26 scanf("%s",t1);
L27 printf("
Please enter substring t2:");
L28 scanf("%s",t2);
L29 if (strlen(t1)==strlen(t2))
L30 { fun (s,t1,t2,w);
L31 printf("
The resultis:%s
",w); }
L32 else
L33 { printf("
Error:strlen(t1)!=strlen(t2)
"); }
L34 }
①___________________________________________________
②___________________________________________________
(2)在此程序中,函数fun()的功能是对N名学生的学习成绩,按从高到低的顺序排列并找出前m(m≤10)名学生,将这些学生的数据存放在一个动态分配的连续存储区中,此存储区的首地址作为函数值返回。请改正程序中的错误,使它能得出正确的结果。以下程序只允许修改三行。
L1 #include <stdlib.h>
L2 #include <string.h>
L3 #include <stdio.h>
L4 #include <malloc.h>
L5 #define N 10
L6 struct ss
L7 { char num[10];
L8 int s;}STU;
L9 STU *fun(STU a[ ],int m)
L10 { STU b[N],*t;
L11 int i,j,k;
L12 *t=calloc(m,sizeof(STU));
L13 for(i=0;i<N;i++)
L14 b[i]=a[i];
L15 for(k=0;k<m;k++)
L16 { for (i=j=0;i<N;i++)
L17 if(b[i].s>b[j].s)
L18 j=i;
L19 t[k].num=b[j].num);
L20 t[k].s=b[j].s;
L21 b[j].s=0; }
L22 return t; }
L23 void outresult(STU a[ ],FILE *pf)
L24 { int i;
L25 for(i=0;i<N;i++)
L26 fprintf (pf, "No =%s Mark=%d
",a[i].num,a[i].s);
L27 fprintf(pf, "
"); }
L28 void main()
L29 {
L30 STU a[N]={{"A01 ",81},{"A02",89},{"A03",66},{"A04",87},
{"A05",77}, {"A06",90},{"A07",79},{"A08 ",61},{"A09 ",80},
{"A10 ",71}};
L31 STU *pOrder;
L32 int i,m;
L33 system("CLS");
L34 printf("**** The original data****
");
L35 outresult(a,stdout);
L36 printf("
Give the number of the students who have better score:");
L37 scanf("%d",&m);
L38 while(m>10)
L39 { printf("
Give the num-ber of the students who have better score:");
L40 scanf("%d",&m);}
L41 pOrder=fun(a,m);
L42 printf("**** The Result ****
");
L43 printf("The top:
");
L44 for(i=0;i<m;i++)
L45 printf("%s %d
",pOrder[i].num,pOrder[i].s);
L46 free(pOrder); }
①___________________________________________________
②___________________________________________________
③___________________________________________________
(3)下面程序中,函数fun的功能是在p所指字符串中找出ASCl码值最大的字符,将其放在第一个位置上;并将该字符前的原字符向后顺序移动。请改正程序中的错误,使它能得出正确结果。以下程序只允许修改两行。
L1 #include <stdio.h>
L2 void fun( char *p )
L3 { char max,*q;
L4 int i=0;
L5 max=p[i];
L6 q=p;
L7 while( p[i]==0 )
L8 { if( max<p[i] )
L9 { max=p[i];
L10 q=p+i;
L11 }
L12 i++;
L13 }
L14 while( q>p)
L15 { *q=*q-1;
L16 q--;
L17 }
L18 p[0]=max;
L19 }
L20 main()
L21 { char str[80];
L22 printf("Enter a string: ");
L23 gets(str);
L24 printf("
The original string: ");
L25 puts(str);
L26 fun(str);
L27 printf("
The string after moving: ");
L28 puts(str);
L29 printf("
");
L30 getchar();
L31 }
①___________________________________________________
②___________________________________________________
(4)下列给定的程序中,函数fun的功能是:把主函数中输入的3个数,最大的放在a中,最小的放在c中。例如,输入的数为55 12 34,输出结果应当是:a=55.0, b=34.0, c=12.0.请改正程序中的错误,使它能得到正确结果。以下程序只允许修改三行。
L1 #include <stdio.h>
L2 #include <stdlib.h>
L3 void fun(float *p,float *q,float *s)
L4 {
L5 float k;
L6 k=(float *)malloc(sizeof(float));
L7 if(*p<*q)
L8 {
L9 *k=*p;*p=*q;*q=*k;
L10 }
L12 if(*s<*p)
L13 {
L14 *k=*s;*s=*p;*p=*k;
L15 }
L16 if(*q<*s)
L17 {
L18 *k=*q;*q=*s;*s=*k;
L19 }
L20 free(k);
L21 }
L22 main()
L23 {
L24 float a,b,c;
L25 printf("Input a b c:");
L26 scanf("%f%f%f",&a,&b,&c);
L27 printf("a=%4.1f,b=%4.1f,c=%4.1f
",a,b,c);
L28 fun(a,b,c);
L29 printf("a=%4.1f,b=%4.1f,c=%4.1f
",a,b,c);
L30 }
①___________________________________________________
②___________________________________________________
③___________________________________________________
三、程序设计题(本大题共2小题,合计20分)
1.下列程序的功能是:将所有奇数位置的节点和偶数位置的节点分别聚集在一起,并保持原始相对顺序。(每空2分,共10分)
程序运行输出结果为:
原始链表: 1 2 3 4 5 6 7 8
奇偶重排后: 1 3 5 7 2 4 6 8
#include <stdio.h>
#include <stdlib.h>
struct ListNode
{
int val;
struct ListNode* next;
};
struct ListNode* oddEvenList(struct ListNode* head)
{
struct ListNode *odd,*even,*evenHead;
if (head == NULL)
return NULL;
odd = head;
even = _______①_______;
evenHead = even;
while (even != NULL && even->next != NULL)
{
odd->next = _______②_______;
odd = odd->next;
even->next = odd->next;
________③__________;
}
odd->next = _______④______;
return head;
}
void printList(struct ListNode* head)
{
while (head != NULL)
{
printf("%d ", head->val);
head = head->next;
}
printf("
");
}
struct ListNode* createList(int arr[], int n)
{
int i;
struct ListNode *head,*cur;
if (n == 0) return NULL;
head = (struct ListNode*)malloc(sizeof(struct ListNode));
head->val = arr[0];
cur = head;
for (i = 1; i < n; i++)
{
____________⑤_____________;
cur = cur->next;
cur->val = arr[i];
}
cur->next = NULL;
return head;
}
int main()
{
int arr[] = {1, 2, 3, 4, 5, 6, 7, 8};
int n = sizeof(arr) / sizeof(arr[0]);
struct ListNode* head = createList(arr, n);
printf("原始链表: ");
printList(head);
head = oddEvenList(head);
printf("奇偶重排后: ");
printList(head);
}
2.下列程序的功能是:判断一个链表是否为回文链表。(每空2分,共10分)
#include <stdio.h>
#include <stdlib.h>
struct ListNode
{
int val;
struct ListNode* next;
};
int isPalindrome(struct ListNode* head)
{
struct ListNode* slow;
struct ListNode* fast;
struct ListNode* prev;
struct ListNode* curr;
struct ListNode* next;
struct ListNode* first;
struct ListNode* second;
if (head == NULL || head->next == NULL)
return 1;
slow = head;
fast = head;
while (fast != NULL && fast->next != NULL)
{
slow = slow->next;
fast = ________①________;
}
prev = NULL;
curr = slow;
next = NULL;
while (curr != NULL)
{
next = curr->next;
_______②_________;
prev = curr;
curr = next;
}
first = head;
second = prev;
while (second != NULL) {
if (_____________③_____________) {
return 0;
}
first = first->next;
_________④__________;
}
return 1;
}
struct ListNode* createListByInput()
{
int n, i, val;
struct ListNode *head = NULL;
struct ListNode *cur = NULL;
struct ListNode *newNode = NULL;
printf("请输入链表节点个数:");
scanf("%d", &n);
if (n <= 0)
return NULL;
printf("请输入 %d 个整数:
", n);
for (i = 0; i < n; i++) {
scanf("%d", &val);
newNode = (struct ListNode*)malloc(sizeof(struct ListNode));
newNode->val = val;
newNode->next = NULL;
if (head == NULL)
{
head = newNode;
cur = head;
}
else
{
cur->next = newNode;
cur = cur->next;
}
}
return head;
}
void printList(struct ListNode* head)
{
struct ListNode* p;
p = head;
while (p != NULL)
{
printf("%d", p->val);
if (p->next != NULL)
{
printf("->");
}
___________⑤___________;
}
printf("
");
}
main()
{
struct ListNode* head;
int res;
printf("======== 回文链表判断程序 ========
");
head = createListByInput();
printf("你输入的链表:");
printList(head);
res = isPalindrome(head);
printf("是否回文链表:%s
", res ? "是" : "否");
}
原创精品资源学科网独家享有版权,侵权必究!
学科网(北京)股份有限公司
学科网(北京)股份有限公司
$
编写说明:本冲刺卷严格依据湖南省计算机应用类高考考纲编写,依托《C语言程序设计》(高等教育出版社第5版),聚焦高三考生冲刺需求,助力高效提分。内容上深度覆盖考纲掌握、理解层级考点,既系统梳理构建知识框架,又强化应用能力训练;同时结合近五年高考真题,精准把握高频考点、命题趋势与题型特点,确保贴合高考方向。
湖南省计算机应用类
《C语言程序设计》
高频考点冲刺卷(二)解析版
时间:60分钟 总分:100分
班级:_________ 姓名:________ 学号:________ 成绩:_________
一、单选题(在本题的每一小题的备选答案中,只有一个答案是正确的。本大题共5小题,每小题2分,共10分)
1.若有以下定义,则对a数组元素地址的正确引用是( )
int a[5],*p=a;
A.p+5 B.*a+1 C.&a+1 D.&a[0]
【答案】D
【解析】int a[5],*p=a; 中,&a[0] 是数组第 0 个元素的地址,是对数组元素地址的正确引用;
A 选项 p+5 指向数组越界后的地址,并非数组元素地址;
B 选项 *a+1 是 a[0]+1 的值,不是地址;
C 选项 &a+1 是整个数组之后的地址,并非元素地址。
2.下列程序运行时,输入:12345,则程序的输出结果是( )
main()
{ char a,b;
scanf("%c%*3c%c",&a,&b);
printf("%c%c
",a,b); }
A.14 B.15 C.123 D.12
【答案】B
【解析】scanf("%c%*3c%c", &a, &b); 中,%*3c 表示读取并丢弃 3 个字符;
输入12345时,a读取第 1 个字符'1',接着丢弃'2'、'3'、'4'这 3 个字符,最后b读取第 5 个字符'5',因此输出15。
3.若有定义:double a=22;int i=0,k=18;,则下列选项中不符合C语言规定的赋值语句是( )
A.a=a++,i++; B.i=(a+k)<=(i+k); C.i=a%11; D.i=!a
【答案】C
【解析】%(求余)运算符的运算对象只能是整型,选项C中i=a%11;变量a为双精度型,所以选项C错误。故本题答案为C。
4.下列选项中,与语句:if(a==1) a=b;else a++;功能不同的switch语句是( )
A.switch(a){case 1:a=b;break;default:a++;}
B.switch(a==1) {case 0:a=b; break; case 1 :a++;}
C.switch (a) {default:a++ ;break; case 1:a=b;}
D.switch(a==1) {case 1:a=b;break; case 0:a++;}
【答案】B
【解析】原语句功能为 a 等于 1 时执行 a=b,否则 a++。选项 B 中 switch 判断 a==1 后,执行逻辑与原语句完全相反,因此功能不同。
5.设有定义:double a[10],*s=a;,则以下选项中能代表数组元素a[3]的是( )
A.(*s)[3] B.*(s+3) C.*s[3] D.*s+3
【答案】B
【解析】指针s是指向数组a的首地址,可以通过指针变量s来引用数组a中的元素,*(s+k)相当于a[k],所以*(s+3)是代表数组元素a[3]。选项A是一个二维指针数组;选项C是不合法的;选项D中,*s即*(s+0),代表数组元素a[0],则*s+3是指数组元素a[0]加3。故本题答案为B。
二、程序分析题(本大题共3小题,共70分)
1.阅读程序,写出运行结果(每空2分,共14分)
(1)下列程序的运行结果是_________________。
#include <stdio.h>
main()
{
int a[2][4]={1,2,3,4,5,6,7,8};
int col_max[4],col_aver[4];
int i,j,max,sum;
for(i=0;i<4;i++)
{ max=a[0][i],sum=0;
for(j=0;j<2;j++)
{
if(max<a[j][i])
max=a[j][i];
sum+=a[j][i];
}
col_max[i]=max;
col_aver[i]=sum/2;
}
for(i=0;i<4;i++)
{ printf("max=%d,aver=%d
",col_max[i],col_aver[i]); }
}
【答案】
max=5,aver=3
max=6,aver=4
max=7,aver=5
max=8,aver=6
(2)下列程序的运行结果是_________________。
#include <stdio.h>
int global_flag = 0;
int* process(int *arr, int idx)
{
global_flag++;
if (global_flag % 2 == 0)
{
arr[idx] += 10;
return &arr[idx + 1];
}
else
{
arr[idx] -= 5;
return &arr[idx];
}
}
main() {
int data[] = {1, 2, 3, 4, 5};
int *ptrs[5];
int sum = 0,i;
for (i = 0; i < 4; i++) {
ptrs[i] = process(data, i);
*ptrs[i] += i;
}
for (i = 0; i < 5; i++) {
sum += data[i];
}
printf("%d
", sum);
}
【答案】31
(3)下列程序的运行结果是_________________。
#include<stdio.h>
#define M 5
main()
{
int a[M]={1,2,3,4,5};
int i,j,t;
i=1;
j=M-1;
while(i<j)
{ t=*(a+i);
*(a+i)=*(a+j);
*(a+j)=t;
i++;j--;
}
for(i=0;i<M;i++)
printf("%d",*(a+i));
}
【答案】15432
(4)下列程序的运行结果是_________________。
#include<stdio.h>
#include<stdlib.h>
void fun(int *p1,int *s)
{ int *t;
t=(int *)malloc(2*sizeof(int));
*t=*p1+*p1++;
*(t+1)=*p1+*p1;
s=t;}
main()
{int a[2]={1,2},b[2]={0};
fun(a,b);
printf("%d,%d
",b[0],b[1]);}
【答案】0,0
(5)下列程序的运行结果是_________________。
#include "stdio.h"
main()
{ int b[2][3]={1,3,5,7,9,11};
int *a[2][3];
int i,j;
int * *p,m;
for(i=0;i<2;i++)
for(j=0;j<3;j++)
a[i][j]=*(b+i)+j;
p=a[0];
for(m=0;m<6;m++)
{ printf("%4d",**p);
p++; }
}
【答案】 1 3 5 7 9 11
(6)下列程序的运行结果是_________________。
#include <stdio.h>
int process(int *arr, int n) {
int sum,i;
if(n <= 0) return 0;
sum = arr[0];
for(i = 0; i < n-1; i++) {
arr[i] = arr[i+1]; }
return sum + process(arr, n-1);}
int main() {
int data[4] = {1, 2, 3, 4};
int result = process(data, 4);
printf("%d
", result);
return 0;}
【答案】10
(7)下列程序的运行结果是_________________。
#include<stdio.h>
main()
{ int a=28,b;
char s[10],*p;
p=s;
do{b=a%16;
if(b<10) *p=b+48;
else *p=b+55;
p++;
a=a/5;}while(a>0);
*p='\0';
puts(s);}
【答案】C51
2.程序填空。按照题目要求,将正确内容填入程序空白处,使程序完整(每空2分,共36分)。
(1)在此程序中,函数fun()的功能是将a所指3x5的矩阵中第k列的元素左移到第0列,第k列以后的每列元素行依次左移,原来左边的各列依次绕到右边。
例如,有下列矩阵:
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
若k为2,程序执行结果为
3 4 5 1 2
3 4 5 1 2
3 4 5 1 2
请在程序的下划线处填入正确的内容,使程序得出正确的结果。(4空)
#include <stdio.h>
#define M 3
#define N 5
void fun(int (*a)[N],int k)
{int i,j,p,temp;
for(p=1;_______①______;p++)
for(i=0;i<M;i++)
{temp=a[i][0];
for(j=0;j<N-1;j++)
a[i][j]=________②_________;
_______③_______=temp;
}
}
main()
{int x[M][N]={{1,2,3,4,5},{1,2,3,4,5},{1,2,3,4,5}},i,j;
printf("The array before mov- ing:
");
for(i=0;i<M;i++)
{for(j=0;j<N;j++)
printf("% 3d",x[i][j]);
printf("
");
}
_________④_________;
printf("The array after mov- ing:
");
for(i=0;i<M;i++)
{for(j=0;j<N;j++)
printf("% 3d",x[i][j]);
printf("
");
}
}
【答案】①p<=k ②a[i][j+1] ③a[i][N-1] ④fun(x,2)
(2)下列程序的功能是将“yye.bin”文件中的数据按平均营业额由高到低进行排序,并将排序后的数据写入文件“sort.bin” 中。(5空)
#include <stdio.h>
#include <stdlib.h>
#define N 3 //N个商品
#define M 4 //M个季度
struct gs
{
char bh[6]; //商品编号
char mc[10]; //商品名称
float yue[M]; //季度营业额
float ave; //平均营业额
}goods[N],temp;
void main()
{
FILE *fp;
int i,j,n;
if(___________①____________)
{
printf("不能打开文件!
");
exit(0);
}
for(i=0;fread(___________②__________)!=0;i++);
fclose(fp);
n=i;
for(i=1;i<=n;i++)
for(j=0;j<n-i;j++)
{
if(__________③__________)
{
temp=goods[j];
goods[j]=goods[j+1];
goods[j+1]=temp;
}
}
if((fp=fopen("sort.bin",______④_____))==NULL)
{
printf("不能打开文件,写文件失败!
");
exit(0);
}
printf("编号\t名称\t1季度\t2季度\t3季度\t4季度\t平均额
");
for(i=0;i<n;i++)
{
fwrite(&goods[i],sizeof(struct gs),1,fp);
printf("%s\t%s\t%7.2f\t%7.2f\t%7.2f\t%7.2f\t%7.2f
",
goods[i].bh,goods[i].mc,goods[i].yue[0],goods[i].yue[1],
goods[i].yue[2],goods[i].yue[3],goods[i].ave);
}
_________⑤_________;
}
【答案】①(fp=fopen("yye.bin","rb"))==NULL
②&goods[i],sizeof(struct gs),1,fp
③goods[j].ave<goods[j+1].ave ④"wb"
⑤fclose(fp)
(3)在此程序中,函数fun()的功能是在形参S所指字符串中寻找与参数c相同的字符,并在其后插入一个与之相同的字符,若找不到相同的字符则不做任何处理。请在程序的下划线处填入正确的内容,使程序得出正确的结果。(4空)
#include <stdio.h>
void fun(char * s, char c)
{ int i,j,n;
for (i=0; __________①________;i++)
if(s[i]==c)
{
______②______;
while(______③_______!='\0')
n++;
for(j=i+n+1;j>i;
j--)
s[j+1]=s[j];
s[j+1]=_______④_______;
i=i+1;
}
}
main()
{
char s[80]="baacda", c;
printf("
The string:s
",s);
printf("
Input a character:");
scanf("%c",&c);
fun(s,c);
printf("
The result is:%s
",s);
}
【答案】①s[i]!='\0' ②n=0 ③s[i+1+n] ④c
(4)给定程序中,函数fun的功能是在形参ss所指字符串数组中,删除所有串长超过k的字符串,函数返回所剩字符串的个数。ss所指字符串数组中共有N个字符串,且串长小于M。请在程序的下划线处填入正确的内容,使程序得出正确的结果。(3空)
#include <stdio.h>
#include <string.h>
#define N 5
#define M 10
int fun(char (*ss)[M], int k)
{ int i,j=0,len;
for(i=0; i<N; i++)
{ len=______①________;
if(len<=_____②_____)
_________③_________;
}
return j;
}
main()
{ char x[N][M]={"Beijing","Shanghai","Tianjing","Nanjing","Wuhan"};
int i,f;
printf("
The original string
");
for(i=0;i<N;i++)puts(x[i]); printf("
");
f=fun(x,7);
printf("The string witch length is less than or equal to 7 :
");
for(i=0; i<f; i++) puts(x[i]);printf("
");
getchar();
}
【答案】①strlen(ss[i]) ②k ③strcpy(ss[j++],ss[i])
(5)下面程序的功能是用递归法将一个整数存放到一个字符数组中,按逆序存放。如将483存放成384。(2空)
#include<stdio.h>
void convert(char *a,int n)
{ int i;
if((i=n/10)!=0)
convert(_______①_______);
*a=________②________;
}
main()
{
int number;
char str[10]=" ";
scanf("%d",&number);
convert(str,number);
puts(str);
}
【答案】①a+1,i ②n%10+'0'
3.阅读程序:修改程序中的错误,不得增行或删行,也不得更改程序结构,请在作答处指出错误代码所在的行号,并给出该行修改后的程序代码。(每空2分,共20分)。
(1)在此程序中,函数fun()的功能是将s所指字符串中最后一次出现的与t1所指字符串相同的子字符串替换成t2所指字符串,所形成的新字符串存放在w所指的数组中。要求t1和t2所指字符串的长度相同。请改正程序中的错误,使它能得出正确的结果。以下程序只允许修改两行。
L1 #include <conio.h>
L2 #include <stdio.h>
L3 #include <string.h>
L4 void fun (char * s,char * t1, char *t2,char *w)
L5 { char *p,*r,*a;
L6 strcpy(w,s);
L7 while(w)
L8 { p=w;
L9 r=t1;
L10 while (*r)
L11 if(*r==*p)
L12 { r++; p++; }
L13 else
L14 { break; }
L15 if(*r='\0')
L16 a=w;w++; }
L17 r=t2;
L18 while (*r)
L19 { *a=*r;
L20 a++;r++; } }
L21 main ()
L22 { char s[100],t1[100],t2[100],w[100];
L23 printf("
Please enter string s:");
L24 scanf("%s",s);
L25 printf("
Please enter substring t1:");
L26 scanf("%s",t1);
L27 printf("
Please enter substring t2:");
L28 scanf("%s",t2);
L29 if (strlen(t1)==strlen(t2))
L30 { fun (s,t1,t2,w);
L31 printf("
The resultis:%s
",w); }
L32 else
L33 { printf("
Error:strlen(t1)!=strlen(t2)
"); }
L34 }
①___________________________________________________
②___________________________________________________
【答案】①L7 while(*w) ②L15 if(*r=='\0')
(2)在此程序中,函数fun()的功能是对N名学生的学习成绩,按从高到低的顺序排列并找出前m(m≤10)名学生,将这些学生的数据存放在一个动态分配的连续存储区中,此存储区的首地址作为函数值返回。请改正程序中的错误,使它能得出正确的结果。以下程序只允许修改三行。
L1 #include <stdlib.h>
L2 #include <string.h>
L3 #include <stdio.h>
L4 #include <malloc.h>
L5 #define N 10
L6 struct ss
L7 { char num[10];
L8 int s;}STU;
L9 STU *fun(STU a[ ],int m)
L10 { STU b[N],*t;
L11 int i,j,k;
L12 *t=calloc(m,sizeof(STU));
L13 for(i=0;i<N;i++)
L14 b[i]=a[i];
L15 for(k=0;k<m;k++)
L16 { for (i=j=0;i<N;i++)
L17 if(b[i].s>b[j].s)
L18 j=i;
L19 t[k].num=b[j].num);
L20 t[k].s=b[j].s;
L21 b[j].s=0; }
L22 return t; }
L23 void outresult(STU a[ ],FILE *pf)
L24 { int i;
L25 for(i=0;i<N;i++)
L26 fprintf (pf, "No =%s Mark=%d
",a[i].num,a[i].s);
L27 fprintf(pf, "
"); }
L28 void main()
L29 {
L30 STU a[N]={{"A01 ",81},{"A02",89},{"A03",66},{"A04",87},
{"A05",77}, {"A06",90},{"A07",79},{"A08 ",61},{"A09 ",80},
{"A10 ",71}};
L31 STU *pOrder;
L32 int i,m;
L33 system("CLS");
L34 printf("**** The original data****
");
L35 outresult(a,stdout);
L36 printf("
Give the number of the students who have better score:");
L37 scanf("%d",&m);
L38 while(m>10)
L39 { printf("
Give the num-ber of the students who have better score:");
L40 scanf("%d",&m);}
L41 pOrder=fun(a,m);
L42 printf("**** The Result ****
");
L43 printf("The top:
");
L44 for(i=0;i<m;i++)
L45 printf("%s %d
",pOrder[i].num,pOrder[i].s);
L46 free(pOrder); }
①___________________________________________________
②___________________________________________________
③___________________________________________________
【答案】①L6 typedef struct ss
②L12 t=(STU*)calloc(m,sizeof(STU));
③L19 strcpy(t[k].num,b[j].num);
(3)下面程序中,函数fun的功能是在p所指字符串中找出ASCl码值最大的字符,将其放在第一个位置上;并将该字符前的原字符向后顺序移动。请改正程序中的错误,使它能得出正确结果。以下程序只允许修改两行。
L1 #include <stdio.h>
L2 void fun( char *p )
L3 { char max,*q;
L4 int i=0;
L5 max=p[i];
L6 q=p;
L7 while( p[i]==0 )
L8 { if( max<p[i] )
L9 { max=p[i];
L10 q=p+i;
L11 }
L12 i++;
L13 }
L14 while( q>p)
L15 { *q=*q-1;
L16 q--;
L17 }
L18 p[0]=max;
L19 }
L20 main()
L21 { char str[80];
L22 printf("Enter a string: ");
L23 gets(str);
L24 printf("
The original string: ");
L25 puts(str);
L26 fun(str);
L27 printf("
The string after moving: ");
L28 puts(str);
L29 printf("
");
L30 getchar();
L31 }
①___________________________________________________
②___________________________________________________
【答案】①L7 while( p[i]!=0 )
②L15 { *q=*(q-1);
(4)下列给定的程序中,函数fun的功能是:把主函数中输入的3个数,最大的放在a中,最小的放在c中。例如,输入的数为55 12 34,输出结果应当是:a=55.0, b=34.0, c=12.0.请改正程序中的错误,使它能得到正确结果。以下程序只允许修改三行。
L1 #include <stdio.h>
L2 #include <stdlib.h>
L3 void fun(float *p,float *q,float *s)
L4 {
L5 float k;
L6 k=(float *)malloc(sizeof(float));
L7 if(*p<*q)
L8 {
L9 *k=*p;*p=*q;*q=*k;
L10 }
L12 if(*s<*p)
L13 {
L14 *k=*s;*s=*p;*p=*k;
L15 }
L16 if(*q<*s)
L17 {
L18 *k=*q;*q=*s;*s=*k;
L19 }
L20 free(k);
L21 }
L22 main()
L23 {
L24 float a,b,c;
L25 printf("Input a b c:");
L26 scanf("%f%f%f",&a,&b,&c);
L27 printf("a=%4.1f,b=%4.1f,c=%4.1f
",a,b,c);
L28 fun(a,b,c);
L29 printf("a=%4.1f,b=%4.1f,c=%4.1f
",a,b,c);
L30 }
①___________________________________________________
②___________________________________________________
③___________________________________________________
【答案】①L5 float *k;
②L12 if(*s>*p)
③L28 fun(&a,&b,&c);
三、程序设计题(本大题共2小题,合计20分)
1.下列程序的功能是:将所有奇数位置的节点和偶数位置的节点分别聚集在一起,并保持原始相对顺序。(每空2分,共10分)
程序运行输出结果为:
原始链表: 1 2 3 4 5 6 7 8
奇偶重排后: 1 3 5 7 2 4 6 8
#include <stdio.h>
#include <stdlib.h>
struct ListNode
{
int val;
struct ListNode* next;
};
struct ListNode* oddEvenList(struct ListNode* head)
{
struct ListNode *odd,*even,*evenHead;
if (head == NULL)
return NULL;
odd = head;
even = _______①_______;
evenHead = even;
while (even != NULL && even->next != NULL)
{
odd->next = _______②_______;
odd = odd->next;
even->next = odd->next;
________③__________;
}
odd->next = _______④______;
return head;
}
void printList(struct ListNode* head)
{
while (head != NULL)
{
printf("%d ", head->val);
head = head->next;
}
printf("
");
}
struct ListNode* createList(int arr[], int n)
{
int i;
struct ListNode *head,*cur;
if (n == 0) return NULL;
head = (struct ListNode*)malloc(sizeof(struct ListNode));
head->val = arr[0];
cur = head;
for (i = 1; i < n; i++)
{
____________⑤_____________;
cur = cur->next;
cur->val = arr[i];
}
cur->next = NULL;
return head;
}
int main()
{
int arr[] = {1, 2, 3, 4, 5, 6, 7, 8};
int n = sizeof(arr) / sizeof(arr[0]);
struct ListNode* head = createList(arr, n);
printf("原始链表: ");
printList(head);
head = oddEvenList(head);
printf("奇偶重排后: ");
printList(head);
}
【答案】①head->next ②even->next ③even = even->next
④evenHead
⑤cur->next = (struct ListNode*)malloc(sizeof(struct ListNode))
2.下列程序的功能是:判断一个链表是否为回文链表。(每空2分,共10分)
#include <stdio.h>
#include <stdlib.h>
struct ListNode
{
int val;
struct ListNode* next;
};
int isPalindrome(struct ListNode* head)
{
struct ListNode* slow;
struct ListNode* fast;
struct ListNode* prev;
struct ListNode* curr;
struct ListNode* next;
struct ListNode* first;
struct ListNode* second;
if (head == NULL || head->next == NULL)
return 1;
slow = head;
fast = head;
while (fast != NULL && fast->next != NULL)
{
slow = slow->next;
fast = ________①________;
}
prev = NULL;
curr = slow;
next = NULL;
while (curr != NULL)
{
next = curr->next;
_______②_________;
prev = curr;
curr = next;
}
first = head;
second = prev;
while (second != NULL) {
if (_____________③_____________) {
return 0;
}
first = first->next;
_________④__________;
}
return 1;
}
struct ListNode* createListByInput()
{
int n, i, val;
struct ListNode *head = NULL;
struct ListNode *cur = NULL;
struct ListNode *newNode = NULL;
printf("请输入链表节点个数:");
scanf("%d", &n);
if (n <= 0)
return NULL;
printf("请输入 %d 个整数:
", n);
for (i = 0; i < n; i++) {
scanf("%d", &val);
newNode = (struct ListNode*)malloc(sizeof(struct ListNode));
newNode->val = val;
newNode->next = NULL;
if (head == NULL)
{
head = newNode;
cur = head;
}
else
{
cur->next = newNode;
cur = cur->next;
}
}
return head;
}
void printList(struct ListNode* head)
{
struct ListNode* p;
p = head;
while (p != NULL)
{
printf("%d", p->val);
if (p->next != NULL)
{
printf("->");
}
___________⑤___________;
}
printf("
");
}
main()
{
struct ListNode* head;
int res;
printf("======== 回文链表判断程序 ========
");
head = createListByInput();
printf("你输入的链表:");
printList(head);
res = isPalindrome(head);
printf("是否回文链表:%s
", res ? "是" : "否");
}
【答案】①fast->next->next ②curr->next = prev
③first->val != second->val ④second = second->next
⑤ p = p->next
原创精品资源学科网独家享有版权,侵权必究!
学科网(北京)股份有限公司
学科网(北京)股份有限公司
$