内容正文:
编写说明:本冲刺卷严格依据湖南省计算机应用类高考考纲编写,依托《C语言程序设计》(高等教育出版社第5版),聚焦高三考生冲刺需求,助力高效提分。内容上深度覆盖考纲掌握、理解层级考点,既系统梳理构建知识框架,又强化应用能力训练;同时结合近五年高考真题,精准把握高频考点、命题趋势与题型特点,确保贴合高考方向。
湖南省计算机应用类
《C语言程序设计》
高频考点冲刺卷(六)解析版
时间:60分钟 总分:100分
班级:_________ 姓名:________ 学号:________ 成绩:_________
一、单选题(在本题的每一小题的备选答案中,只有一个答案是正确的。本大题共5小题,每小题2分,共10分)
1.执行以下语句后,b的值为( )
int a=5,b=6,w=1,x=2,y=3,z=4;
(a=w>x)&&(b=y>z);
A.6 B.0 C.1 D.4
2.若变量均已正确定义并赋值,设有语句:k=x<y?(y<z?1:0):0;,则以下选项中,与此语句功能相同的是( )
A.if(x<y||y<z)k=1;else k=0; B.if(x<y) k=0;else if (y<z) k=1;
C.if(x<y) if(y<z)k=1;else k=0; D.if(x<y&&y<z)k=1;else k=0;
3.若在定义语句:int a,b,c,*p=&c;之后,接着执行以下选项中的语句,则能正确执行的语句是( )
A.scanf("%d",a,b,c); B.scanf("%d%d%d" ,a,b, c);
C.scanf("%d" ,p); D.scanf("%d",&p);
4.设有以下程序段,则程序运行后的输出结果是( )
main()
{ int i=0;
do printf("%d,",i);while(i++);
printf("%d
",i);}
A.0,0 B.0,1 C.1,1 D.程序进入无限循环
5.设有定义:char p[ ]={'1','2','3'},*q=p;,则以下不能计算出一个char型数据所占字节数的表达式是( )
A.sizeof(p) B.sizeof(char)
C.sizeof(*q) D.sizeof(p[0])
二、程序分析题(本大题共3小题,共70分)
1.阅读程序,写出运行结果(每空2分,共14分)
(1)下列程序的运行结果是_________________。
#include <stdio.h>
#include<string.h>
#include<stdlib.h>
main()
{
char *p;
int i;
p=(char *)malloc(sizeof(char)*20);
strcpy(p,"Thank");
for(i=6;i>=0;i--)
putchar(*(p+i));
free(p);
}
(2)下列程序的运行结果是_________________。
#include <stdio.h>
main() {
int a[3][3] = {{1,2,3},{4,5,6},{7,8,9}};
int temp,sum,i,j;
for (i = 0; i < 3; i++) {
for (j = i + 1; j < 3; j++) {
temp = a[i][j];
a[i][j] = a[j][i];
a[j][i] = temp; }
}
for (i = 0; i < 3; i++) {
if (i == 0) a[i][i] += a[i][i+1];
else if (i == 2) a[i][i] += a[i][i-1];
else a[i][i] = a[i-1][i] + a[i+1][i];
}
sum = 0;
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
if ((i + j) % 2 == 0) sum += a[i][j]; }
}
printf("%d
", sum);
}
(3)下列程序的运行结果是_________________。
#include <stdio.h>
#include <string.h>
int main() {
char s[] = "HelloWorld",t;
int len = strlen(s),i;
for (i = 0; i < len / 2; i++) {
t = s[i];
s[i] = s[len - 1 - i];
s[len - 1 - i] = t;
}
s[4] = '\0';
printf("%s
", s);
}
(4)下列程序的运行结果是_________________。
#include <stdio.h>
void fun(char* s, char c){
int i,j,n;
for (i=0; s[i]!='\0';i++)
if (s[i]==c){
n=0;
while (s[i+1+n]!='\0')n++;
for (j=i+n+1;j>i;j--)s[j+1]=s[j];
s[j+1]=c;
i=i+1;
}
}
int main(){
char s[80]="baacda", c='a' ;
fun(s, c);
printf("result: %s
",s);
return 0;
}
(5)下列程序的运行结果是_________________。
#include <stdio.h>
int calculate(int x) {
static int y = 1;
y = y + x;
return y;}
int main() {
int result = 0,i,val;
int arr[4];
for(i = 1; i <= 4; i++) {
val = calculate(i);
result += val; }
printf("Final result: %d
", result);
for(i = 0; i < 4; i++) {
arr[i] = result % (i + 2); }
printf("
");}
(6)运行以下程序时,从键盘输入:56,则下列程序的输出结果是_________________。
#include<stdio.h>
main()
{ char *p,s[6];
int n;
p=s;
gets(p);
n=*p-'0';
while(*(++p)!='\0')
n=n*8+*p-'\0';
printf("%d
",n);}
(7)下列程序的运行结果是_________________。
#include <stdio.h>
#include <string.h>
int main()
{ int i=0,n=0; char s[80], *p;
p=s;
strcpy(p,"It is a book.");
for( ;*p!='\0';p++)
if( *p==' ') i=0;
else if( i==0)
{n++;i=1;}
printf("n=%d
",n);
return(0);}
2.程序填空。按照题目要求,将正确内容填入程序空白处,使程序完整(每空2分,共36分)。
(1)在此程序中,函数fun()的功能是将a,b中的两个正整数合并成一个新的整数存放在c中。合并的方式是将a中的十位和个位数依次放在变量c的十位和千位上,b中的十位和个位数依次放在变量c的个位和百位上。例如,a=45,b=12,调用该函数后,c=5241.(4空)
#include <stdio.h>
void fun(int a, int b, long *c)
{
______________①______________;
}
void NONO ()
{
_________②________;
int i, a,b; long c;
rf = fopen("in.dat","r");
wf = fopen("out.dat","w");
for(i =0;i<10;i++){
fscanf (________③_________) ;
fun (a, b, &c) ;
fprintf(wf,"a=%d,b=%d,c=%ld\ n",a,b,c);
}
fclose(rf) ;
fclose(wf);
}
main()
{int a,b; long c;void NONO();
printf("Input a, b:");
scanf("%d%d",&a,&b);
_________④__________;
printf("The result is:%ld
",c);
NONO();
}
(2)在下列程序中,fun函数,该函数的功能是:计算NXN矩阵元素的方差,结果由函数返回。维数在主函数中输入。(5空)
例如: 的计算结果是14.414.
求方差的公式为:
S= 其中 =
#include <stdio.h>
#include <stdlib.h>
#include<math.h>
#define N 20
double fun(_______①________,int n)
{
int i,j;
double s=0.0;
double f=0.0;
double aver=0.0;
double sd=0.0;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
s=s+a[i][j];
aver=_________②__________;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
f=f+(a[i][j]-aver)*(_______③______);
f=f/(n*n);
sd=________④__________;
return sd;
}
main()
{
int a[N][N];
int n;
int i,j;
double s;
printf("Input the dimension of array n:
");
scanf("%d",&n);
printf("The array
");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
a[i][j]=rand()%50;
while (a[i][j]==0)
a[i][j]=rand()%60;
printf("%4d",a[i][j]);
}
printf("
");
}
s=________⑤_______;
printf("THE RESULT
");
printf("%4.3f
",s);
}
(3)在此程序中,函数fun()的功能是把形参a所指数组元素中的最大值放在a[0]中,接着把a所指数组元素中的最小值放在a[1]中,再把 a所指数组元素中的次大值放在a[2]中,把a数组元素中的次小值放在a[3]中,依此类推。请在程序的下划线处填入正确的内容,使程序得出正确的结果。(5空)
#include <stdio.h>
#define N 9
void fun(_________①_________)
{ int i,j,max,min,px,pn,t;
for (i=0; i<n-1;________②_______)
{ max=min =a[i];
px=pn=i;
for (______③_______; j<n; j++)
{ if (max <a[j])
{max=a[j]; px=j;}
if (min >a[j])
{min=a[j]; pn=j; }
}
if (_________④________)
{ t=a[i];
a[i]=max;
a[px]=t;
if (pn == i)
pn = px; }
if (_________⑤__________)
{ t=a[i+1];a[i+1]=min;
a[pn]=t;} }
}
main()
{
int b[N]={1,4,2,3,9,6,5,8,7},i;
printf("
The original data:
");
for(i=0;i<N;i++)
printf("% 4d",b[i]);
printf("
");
fun (b, N);
printf("
The data after moving:
");
for (i=0;i<N;i++)
printf("% 4d",b[i]);
printf("
"); }
(4)一维数组中保存有1个从小到大有序的整数数列。下列函数的功能是利用折半查找算法查找整数m在数组中的位置。若找到,则返回其下标值:反之,则显示"没找到!"。(4空)
#include <stdio.h>
#define N 10
int fun(int a[],int m)
{
int low=0,high=N-1,mid;
while(low<=high)
{
mid=_______①_______;
if(m<a[mid]) ______②_____;
else if(m>a[mid]) _______③_______;
else return(mid);
}
________④________;
}
void main()
{
int i,a[N],k,m;
printf("请输入%d个数:",N);
for (i=0;i<N;i++)
scanf("%d",&a[i]);
printf("请输入要查找的数:");
scanf("%d",&m);
k=fun(a,m);
if(k>=0)
printf("%d的下标序号为:%d
",m,k);
else
printf("没找到!
");
}
3.阅读程序:修改程序中的错误,不得增行或删行,也不得更改程序结构,请在作答处指出错误代码所在的行号,并给出该行修改后的程序代码。(每空2分,共20分)。
(1)在此程序中,函数fun()的功能是根据形参m的值(2<=m<=9),在m行m列的二维数组中存放具有如下规律的数据,由main()函数输出。
请改正程序中的错误,使它能得出正确的结果。下面程序中,。以下程序只允许修改两行。
L1 #include <conio.h>
L2 #include <stdio.h>
L3 #define M 10
L4 int a[M][M]={0};
L5 void fun(int **a,int m)
L6 {int j,k;
L7 for(j=0;j<m;j++)
L8 for(k=0;k<m;k++)
L9 a[j][k]=k*j;
L10 }
L11 main ()
L12 {int i,j,n;
L13 printf ("Entern:");
L14 scanf("%d",&n);
L15 fun(a,n);
L16 for(i=0;i<n;i++)
L17 {for (j=0;j<n;j++)
L18 printf ("% 4d",a[i][j]);
L19 printf ("
"); }
L20 }
①___________________________________________________
②___________________________________________________
(2)下面程序的功能是:用随机函数产生100个[0,99]范围内的随机整数,统计个位上的数字分别为1,2,3,4,5,6,7,8,9,0的数的个数,即个位是1的个数存放在x[1]中,个位是2的个数存放在x[2].....个位是0的个数存放在x[10]中,并打印出来。。以下程序只允许修改三行。
L1 #include<stdio.h>
L2 #include<stdlib.h>
L3 void main()
L4 { int a[101],x[11],i,p;
L5 for(i=0;i<11;i++)
L6 x[i]=0;
L7 for(i=1;i<=100;i++)
L8 {a[i]=rand( )/100;
L9 printf("%4d",a[i]);
L10 if(i%10==0) printf("
");}
L11 for(i=1;i<=100;i++)
L12 {p=a[i]/10;
L13 if(p==0)p=10;
L14 x[p++];}
L15 for(i=1;i<=10;i++)
L16 {p=i;
L17 if(i==10)p=0;
L18 printf("%d,%d
",p,x[i]);}
L19 }
①___________________________________________________
②___________________________________________________
③___________________________________________________
(3)“3025”这个数具有一种独特的性质:将它平分为二段,即30和25,使之相加后求平方,即(30+25)2,恰好等于3025本身。下列程序的功能是找出具有这样性质的全部四位数。以下程序只允许修改两行。
L1 #include<stdio.h>
L2 int main()
L3 {
L4 int n,a,b;
L5 printf("There are following number with 4 digits satisfied condition
");
L6 for(n=1000;n<10000;n++)
L7 {
L8 a=n/10;
L9 b=n%100;
L10 if((a+b)*(a+b))
L11 printf("%d ",n);
L12 }
L13 }
①___________________________________________________
②___________________________________________________
(4)下面程序的功能是:求素数表中1~1000之间的所有素数 。以下程序只允许修改三行。
L1 #include<stdio.h>
L2 int main()
L3 {
L4 int n1,nm,i,j,flag,count=0;
L5 do{
L6 printf("Input START and END=?
");
L7 scanf("%d%d",&n1,&nm);
L8 }while(!(n1>0&&n1<nm));
L9 printf("PRIME TABLE(%d–%d)
",n1,nm);
L10 if(n1==1&&n1==2)
L11 {
L12 printf("%4d",2);
L13 n1=3;
L14 count++;
L15 }
L16 for(i=n1;i<=nm;i++)
L17 {
L18 if(!(i%2))
L19 break;
L20 for(flag=1,j=3;flag&&j<i/2;j+=2)
L21 if(i%j)
L22 flag=0;
L23 if(flag) printf(++count%15?"%4d":"%4d
",i);
L24 }
L25 }
①___________________________________________________
②___________________________________________________
③___________________________________________________
三、程序设计题(本大题共2小题,合计20分)
1.下列程序的功能是:实现一个从链表中随机选择节点的功能。由于链表长度未知,需要使用蓄水池采样算法。该算法的核心思想是:遍历链表时,第i个节点有1/i的概率被选中,最终保证每个节点被选中的概率均为1/n(n为链表长度)。(每空2分,共10分)
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
struct ListNode
{
int val;
struct ListNode* next;
};
_________①_________
{
struct ListNode* head;
} Solution;
Solution* solutionCreate(struct ListNode* head)
{
Solution* obj = NULL;
obj = (Solution*)malloc(sizeof(Solution));
____________②____________;
srand(time(NULL));
return obj;
}
int solutionGetRandom(Solution* obj)
{
int result = 0;
int count = 0;
struct ListNode* curr = NULL;
curr = obj->head;
count = 0;
result = 0;
while (curr != NULL)
{
__________③_________;
if (rand() % count == 0)
{
_________④_________;
}
curr = curr->next;
}
return result;
}
void solutionFree(Solution* obj)
{
free(obj);
}
struct ListNode* createList(int arr[], int n)
{
struct ListNode* head = NULL;
struct ListNode* cur = NULL;
int i = 0;
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->next = (struct ListNode*)malloc(sizeof(struct ListNode));
cur = cur->next;
cur->val = arr[i];
}
cur->next = NULL;
return head;
}
void printList(struct ListNode* head)
{
struct ListNode* temp = NULL;
temp = head;
while (temp != NULL)
{
printf("%d ", temp->val);
temp = temp->next;
}
printf("
");
}
main()
{
int arr[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int n = 0;
struct ListNode* head = NULL;
Solution* obj = NULL;
int i = 0;
int val = 0;
int counts[11] = {0};
struct ListNode* temp = NULL;
struct ListNode* to_free = NULL;
n = 10;
____________⑤_____________;
obj = solutionCreate(head);
printf("原始链表: ");
printList(head);
printf("
");
printf("蓄水池采样结果(运行10次):
");
for (i = 0; i < 10; i++)
{
val = solutionGetRandom(obj);
printf("%d ", val);
}
printf("
");
printf("概率分布验证(运行10000次):
");
for (i = 0; i < 10000; i++)
{
val = solutionGetRandom(obj);
if (val >= 1 && val <= 10)
{
counts[val] = counts[val] + 1;
}
}
for (i = 1; i <= 10; i++)
{
printf("节点 %2d: %4d次, 概率: %5.2f%%
", i, counts[i], counts[i] * 100.0 / 10000);
}
printf("
");
solutionFree(obj);
temp = head;
while (temp != NULL)
{
to_free = temp;
temp = temp->next;
free(to_free);
}
}
2.下列程序的功能是:合并K个升序链表为一个升序链表。(每空2分,共10分)
#include <stdio.h>
#include <stdlib.h>
struct ListNode
{
int val;
struct ListNode* next;
};
struct ListNode* mergeTwoLists(struct ListNode* l1, struct ListNode* l2)
{
struct ListNode dummy;
struct ListNode* tail = &dummy;
dummy.next = NULL;
while (l1 != NULL && l2 != NULL)
{
if (_________①__________)
{
tail->next = l1;
l1 = l1->next;
}
else
{
tail->next = l2;
l2 = l2->next;
}
_____________②____________;
}
_____________③_____________;
return dummy.next;
}
struct ListNode* mergeKLists(struct ListNode** lists, int listsSize)
{
int mid;
struct ListNode* left;
struct ListNode* right;
if (listsSize == 0)
return NULL;
if (listsSize == 1)
return lists[0];
mid = listsSize / 2;
left = mergeKLists(lists, mid);
right = _______________④________________;
____________⑤______________;
}
struct ListNode* createList(int arr[], int n)
{
struct ListNode* head = NULL;
struct ListNode* tail = NULL;
int i;
struct ListNode* node;
for (i = 0; i < n; i++)
{
node = (struct ListNode*)malloc(sizeof(struct ListNode));
node->val = arr[i];
node->next = NULL;
if (head == NULL)
{
head = node;
tail = node;
}
else
{
tail->next = node;
tail = node;
}
}
return head;
}
void printList(struct ListNode* head)
{
struct ListNode* curr;
curr = head;
while (curr != NULL)
{
printf("%d ", curr->val);
curr = curr->next;
}
printf("
");
}
main()
{
int k;
int i, j;
int len;
int* arr;
struct ListNode** lists;
struct ListNode* merged;
printf("请输入链表的数量 k:");
scanf("%d", &k);
lists = (struct ListNode**)malloc(k * sizeof(struct ListNode*));
for (i = 0; i < k; i++)
{
printf("
请输入第 %d 个链表的节点个数:", i + 1);
scanf("%d", &len);
arr = (int*)malloc(len * sizeof(int));
printf("请输入第 %d 个链表的 %d 个升序整数:", i + 1, len);
for (j = 0; j < len; j++)
{
scanf("%d", &arr[j]);
}
lists[i] = createList(arr, len);
free(arr);
}
printf("
================ 输入的链表 ================
");
for (i = 0; i < k; i++)
{
printf("第 %d 个链表:", i + 1);
printList(lists[i]);
}
merged = mergeKLists(lists, k);
printf("
================ 合并后的升序链表 ================
");
printf("Merged list:");
printList(merged);
free(lists);
}
原创精品资源学科网独家享有版权,侵权必究!
学科网(北京)股份有限公司
学科网(北京)股份有限公司
$
编写说明:本冲刺卷严格依据湖南省计算机应用类高考考纲编写,依托《C语言程序设计》(高等教育出版社第5版),聚焦高三考生冲刺需求,助力高效提分。内容上深度覆盖考纲掌握、理解层级考点,既系统梳理构建知识框架,又强化应用能力训练;同时结合近五年高考真题,精准把握高频考点、命题趋势与题型特点,确保贴合高考方向。
湖南省计算机应用类
《C语言程序设计》
高频考点冲刺卷(六)解析版
时间:60分钟 总分:100分
班级:_________ 姓名:________ 学号:________ 成绩:_________
一、单选题(在本题的每一小题的备选答案中,只有一个答案是正确的。本大题共5小题,每小题2分,共10分)
1.执行以下语句后,b的值为( )
int a=5,b=6,w=1,x=2,y=3,z=4;
(a=w>x)&&(b=y>z);
A.6 B.0 C.1 D.4
【答案】A
【解析】逻辑与 && 具有短路特性,w>x 为假,a=0,左侧为假后右侧表达式不再执行,b 保持原值 6。
2.若变量均已正确定义并赋值,设有语句:k=x<y?(y<z?1:0):0;,则以下选项中,与此语句功能相同的是( )
A.if(x<y||y<z)k=1;else k=0; B.if(x<y) k=0;else if (y<z) k=1;
C.if(x<y) if(y<z)k=1;else k=0; D.if(x<y&&y<z)k=1;else k=0;
【答案】D
【解析】原语句表示 x<y 且 y<z 时 k=1,否则 k=0,与 if (x<y&&y<z) k=1;else k=0; 功能完全一致。
3.若在定义语句:int a,b,c,*p=&c;之后,接着执行以下选项中的语句,则能正确执行的语句是( )
A.scanf("%d",a,b,c); B.scanf("%d%d%d" ,a,b, c);
C.scanf("%d" ,p); D.scanf("%d",&p);
【答案】C
【解析】scanf语句的格式是'scanf(格式控制,地址列表),本题定义了整型变量a,b,c,且定义了指针变量p指向整型变量c,"p"代表的是变量c的地址,所以对整型变量a,b,c正确的输入语句应为:scanf("%d%d%d",&a,&b,&c)或scanf("%d%d%d',&a,&b,p)。
4.设有以下程序段,则程序运行后的输出结果是( )
main()
{ int i=0;
do printf("%d,",i);while(i++);
printf("%d
",i);}
A.0,0 B.0,1 C.1,1 D.程序进入无限循环
【答案】B
【解析】本题先执行do后面的循环体,即输出i的值0,然后判断while的表达式是否为真,由于"i++"是先判断i的值,再自身加1,所以,while表达式值为假,则退出循环,执行最后一个printf语句,输出此时的i的值1。故答案为B。
5.设有定义:char p[ ]={'1','2','3'},*q=p;,则以下不能计算出一个char型数据所占字节数的表达式是( )
A.sizeof(p) B.sizeof(char)
C.sizeof(*q) D.sizeof(p[0])
【答案】A
【解析】sizeof (函数用于计算字符数组的长度。本题要求计算出一个char型数据所占字节数,而选项A中计算的是字符数组p所占的字节数,其结果为3;其它选项均计算的是一个char型数据所占字节数,其结果为1。故本题答案为A。
二、程序分析题(本大题共3小题,共70分)
1.阅读程序,写出运行结果(每空2分,共14分)
(1)下列程序的运行结果是_________________。
#include <stdio.h>
#include<string.h>
#include<stdlib.h>
main()
{
char *p;
int i;
p=(char *)malloc(sizeof(char)*20);
strcpy(p,"Thank");
for(i=6;i>=0;i--)
putchar(*(p+i));
free(p);
}
【答案】 knahT
(2)下列程序的运行结果是_________________。
#include <stdio.h>
main() {
int a[3][3] = {{1,2,3},{4,5,6},{7,8,9}};
int temp,sum,i,j;
for (i = 0; i < 3; i++) {
for (j = i + 1; j < 3; j++) {
temp = a[i][j];
a[i][j] = a[j][i];
a[j][i] = temp; }
}
for (i = 0; i < 3; i++) {
if (i == 0) a[i][i] += a[i][i+1];
else if (i == 2) a[i][i] += a[i][i-1];
else a[i][i] = a[i-1][i] + a[i+1][i];
}
sum = 0;
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
if ((i + j) % 2 == 0) sum += a[i][j]; }
}
printf("%d
", sum);
}
【答案】40
(3)下列程序的运行结果是_________________。
#include <stdio.h>
#include <string.h>
int main() {
char s[] = "HelloWorld",t;
int len = strlen(s),i;
for (i = 0; i < len / 2; i++) {
t = s[i];
s[i] = s[len - 1 - i];
s[len - 1 - i] = t;
}
s[4] = '\0';
printf("%s
", s);
}
【答案】dlro
(4)下列程序的运行结果是_________________。
#include <stdio.h>
void fun(char* s, char c){
int i,j,n;
for (i=0; s[i]!='\0';i++)
if (s[i]==c){
n=0;
while (s[i+1+n]!='\0')n++;
for (j=i+n+1;j>i;j--)s[j+1]=s[j];
s[j+1]=c;
i=i+1;
}
}
int main(){
char s[80]="baacda", c='a' ;
fun(s, c);
printf("result: %s
",s);
return 0;
}
【答案】result: baaaacdaa
(5)下列程序的运行结果是_________________。
#include <stdio.h>
int calculate(int x) {
static int y = 1;
y = y + x;
return y;}
int main() {
int result = 0,i,val;
int arr[4];
for(i = 1; i <= 4; i++) {
val = calculate(i);
result += val; }
printf("Final result: %d
", result);
for(i = 0; i < 4; i++) {
arr[i] = result % (i + 2); }
printf("
");}
【答案】Final result: 24
(6)运行以下程序时,从键盘输入:56,则下列程序的输出结果是_________________。
#include<stdio.h>
main()
{ char *p,s[6];
int n;
p=s;
gets(p);
n=*p-'0';
while(*(++p)!='\0')
n=n*8+*p-'\0';
printf("%d
",n);}
【答案】94
(7)下列程序的运行结果是_________________。
#include <stdio.h>
#include <string.h>
int main()
{ int i=0,n=0; char s[80], *p;
p=s;
strcpy(p,"It is a book.");
for( ;*p!='\0';p++)
if( *p==' ') i=0;
else if( i==0)
{n++;i=1;}
printf("n=%d
",n);
return(0);}
【答案】n=4
2.程序填空。按照题目要求,将正确内容填入程序空白处,使程序完整(每空2分,共36分)。
(1)在此程序中,函数fun()的功能是将a,b中的两个正整数合并成一个新的整数存放在c中。合并的方式是将a中的十位和个位数依次放在变量c的十位和千位上,b中的十位和个位数依次放在变量c的个位和百位上。例如,a=45,b=12,调用该函数后,c=5241.(4空)
#include <stdio.h>
void fun(int a, int b, long *c)
{
______________①______________;
}
void NONO ()
{
_________②________;
int i, a,b; long c;
rf = fopen("in.dat","r");
wf = fopen("out.dat","w");
for(i =0;i<10;i++){
fscanf (________③_________) ;
fun (a, b, &c) ;
fprintf(wf,"a=%d,b=%d,c=%ld\ n",a,b,c);
}
fclose(rf) ;
fclose(wf);
}
main()
{int a,b; long c;void NONO();
printf("Input a, b:");
scanf("%d%d",&a,&b);
_________④__________;
printf("The result is:%ld
",c);
NONO();
}
【答案】①*c=(a%10)*1000+(b%10)*100+(a/10)*10+(b/10)
②FILE *rf, *wf ③rf, "%d,%d", &a, &b ④fun(a, b,&c)
(2)在下列程序中,fun函数,该函数的功能是:计算NXN矩阵元素的方差,结果由函数返回。维数在主函数中输入。(5空)
例如: 的计算结果是14.414.
求方差的公式为:
S= 其中 =
#include <stdio.h>
#include <stdlib.h>
#include<math.h>
#define N 20
double fun(_______①________,int n)
{
int i,j;
double s=0.0;
double f=0.0;
double aver=0.0;
double sd=0.0;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
s=s+a[i][j];
aver=_________②__________;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
f=f+(a[i][j]-aver)*(_______③______);
f=f/(n*n);
sd=________④__________;
return sd;
}
main()
{
int a[N][N];
int n;
int i,j;
double s;
printf("Input the dimension of array n:
");
scanf("%d",&n);
printf("The array
");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
a[i][j]=rand()%50;
while (a[i][j]==0)
a[i][j]=rand()%60;
printf("%4d",a[i][j]);
}
printf("
");
}
s=________⑤_______;
printf("THE RESULT
");
printf("%4.3f
",s);
}
【答案】①int a[ ][N] ②s/(n*n) ③a[i][j]-aver
④sqrt(f) ⑤fun(a,n)
(3)在此程序中,函数fun()的功能是把形参a所指数组元素中的最大值放在a[0]中,接着把a所指数组元素中的最小值放在a[1]中,再把 a所指数组元素中的次大值放在a[2]中,把a数组元素中的次小值放在a[3]中,依此类推。请在程序的下划线处填入正确的内容,使程序得出正确的结果。(5空)
#include <stdio.h>
#define N 9
void fun(_________①_________)
{ int i,j,max,min,px,pn,t;
for (i=0; i<n-1;________②_______)
{ max=min =a[i];
px=pn=i;
for (______③_______; j<n; j++)
{ if (max <a[j])
{max=a[j]; px=j;}
if (min >a[j])
{min=a[j]; pn=j; }
}
if (_________④________)
{ t=a[i];
a[i]=max;
a[px]=t;
if (pn == i)
pn = px; }
if (_________⑤__________)
{ t=a[i+1];a[i+1]=min;
a[pn]=t;} }
}
main()
{
int b[N]={1,4,2,3,9,6,5,8,7},i;
printf("
The original data:
");
for(i=0;i<N;i++)
printf("% 4d",b[i]);
printf("
");
fun (b, N);
printf("
The data after moving:
");
for (i=0;i<N;i++)
printf("% 4d",b[i]);
printf("
"); }
【答案】①int a[ ], int n ②i+=2 ③j=i+1 ④px!=i
⑤pn!=i+1
(4)一维数组中保存有1个从小到大有序的整数数列。下列函数的功能是利用折半查找算法查找整数m在数组中的位置。若找到,则返回其下标值:反之,则显示"没找到!"。(4空)
#include <stdio.h>
#define N 10
int fun(int a[],int m)
{
int low=0,high=N-1,mid;
while(low<=high)
{
mid=_______①_______;
if(m<a[mid]) ______②_____;
else if(m>a[mid]) _______③_______;
else return(mid);
}
________④________;
}
void main()
{
int i,a[N],k,m;
printf("请输入%d个数:",N);
for (i=0;i<N;i++)
scanf("%d",&a[i]);
printf("请输入要查找的数:");
scanf("%d",&m);
k=fun(a,m);
if(k>=0)
printf("%d的下标序号为:%d
",m,k);
else
printf("没找到!
");
}
【答案】①(low+high)/2 ②high=mid-1 ③low=mid+1
④return (-1)
3.阅读程序:修改程序中的错误,不得增行或删行,也不得更改程序结构,请在作答处指出错误代码所在的行号,并给出该行修改后的程序代码。(每空2分,共20分)。
(1)在此程序中,函数fun()的功能是根据形参m的值(2<=m<=9),在m行m列的二维数组中存放具有如下规律的数据,由main()函数输出。
请改正程序中的错误,使它能得出正确的结果。下面程序中,。以下程序只允许修改两行。
L1 #include <conio.h>
L2 #include <stdio.h>
L3 #define M 10
L4 int a[M][M]={0};
L5 void fun(int **a,int m)
L6 {int j,k;
L7 for(j=0;j<m;j++)
L8 for(k=0;k<m;k++)
L9 a[j][k]=k*j;
L10 }
L11 main ()
L12 {int i,j,n;
L13 printf ("Entern:");
L14 scanf("%d",&n);
L15 fun(a,n);
L16 for(i=0;i<n;i++)
L17 {for (j=0;j<n;j++)
L18 printf ("% 4d",a[i][j]);
L19 printf ("
"); }
L20 }
①___________________________________________________
②___________________________________________________
【答案】①L5 void fun(int a[][M],int m)
②L9 a[j][k]=(k+1)*(j+1);
(2)下面程序的功能是:用随机函数产生100个[0,99]范围内的随机整数,统计个位上的数字分别为1,2,3,4,5,6,7,8,9,0的数的个数,即个位是1的个数存放在x[1]中,个位是2的个数存放在x[2].....个位是0的个数存放在x[10]中,并打印出来。。以下程序只允许修改三行。
L1 #include<stdio.h>
L2 #include<stdlib.h>
L3 void main()
L4 { int a[101],x[11],i,p;
L5 for(i=0;i<11;i++)
L6 x[i]=0;
L7 for(i=1;i<=100;i++)
L8 {a[i]=rand( )/100;
L9 printf("%4d",a[i]);
L10 if(i%10==0) printf("
");}
L11 for(i=1;i<=100;i++)
L12 {p=a[i]/10;
L13 if(p==0)p=10;
L14 x[p++];}
L15 for(i=1;i<=10;i++)
L16 {p=i;
L17 if(i==10)p=0;
L18 printf("%d,%d
",p,x[i]);}
L19 }
①___________________________________________________
②___________________________________________________
③___________________________________________________
【答案】①L8 {a[i]=rand( )%100;
②L12 {p=a[i]%10; ③L14 x[p]++;}
(3)“3025”这个数具有一种独特的性质:将它平分为二段,即30和25,使之相加后求平方,即(30+25)2,恰好等于3025本身。下列程序的功能是找出具有这样性质的全部四位数。以下程序只允许修改两行。
L1 #include<stdio.h>
L2 int main()
L3 {
L4 int n,a,b;
L5 printf("There are following number with 4 digits satisfied condition
");
L6 for(n=1000;n<10000;n++)
L7 {
L8 a=n/10;
L9 b=n%100;
L10 if((a+b)*(a+b))
L11 printf("%d ",n);
L12 }
L13 }
①___________________________________________________
②___________________________________________________
【答案】①L8 a=n/100;
②L10 if((a+b)*(a+b)==n)
(4)下面程序的功能是:求素数表中1~1000之间的所有素数 。以下程序只允许修改三行。
L1 #include<stdio.h>
L2 int main()
L3 {
L4 int n1,nm,i,j,flag,count=0;
L5 do{
L6 printf("Input START and END=?
");
L7 scanf("%d%d",&n1,&nm);
L8 }while(!(n1>0&&n1<nm));
L9 printf("PRIME TABLE(%d–%d)
",n1,nm);
L10 if(n1==1&&n1==2)
L11 {
L12 printf("%4d",2);
L13 n1=3;
L14 count++;
L15 }
L16 for(i=n1;i<=nm;i++)
L17 {
L18 if(!(i%2))
L19 break;
L20 for(flag=1,j=3;flag&&j<i/2;j+=2)
L21 if(i%j)
L22 flag=0;
L23 if(flag) printf(++count%15?"%4d":"%4d
",i);
L24 }
L25 }
①___________________________________________________
②___________________________________________________
③___________________________________________________
【答案】①L10 if(n1==1||n1==2)
②L19 continue;
③L21 if(!(i%j))
三、程序设计题(本大题共2小题,合计20分)
1.下列程序的功能是:实现一个从链表中随机选择节点的功能。由于链表长度未知,需要使用蓄水池采样算法。该算法的核心思想是:遍历链表时,第i个节点有1/i的概率被选中,最终保证每个节点被选中的概率均为1/n(n为链表长度)。(每空2分,共10分)
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
struct ListNode
{
int val;
struct ListNode* next;
};
_________①_________
{
struct ListNode* head;
} Solution;
Solution* solutionCreate(struct ListNode* head)
{
Solution* obj = NULL;
obj = (Solution*)malloc(sizeof(Solution));
____________②____________;
srand(time(NULL));
return obj;
}
int solutionGetRandom(Solution* obj)
{
int result = 0;
int count = 0;
struct ListNode* curr = NULL;
curr = obj->head;
count = 0;
result = 0;
while (curr != NULL)
{
__________③_________;
if (rand() % count == 0)
{
_________④_________;
}
curr = curr->next;
}
return result;
}
void solutionFree(Solution* obj)
{
free(obj);
}
struct ListNode* createList(int arr[], int n)
{
struct ListNode* head = NULL;
struct ListNode* cur = NULL;
int i = 0;
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->next = (struct ListNode*)malloc(sizeof(struct ListNode));
cur = cur->next;
cur->val = arr[i];
}
cur->next = NULL;
return head;
}
void printList(struct ListNode* head)
{
struct ListNode* temp = NULL;
temp = head;
while (temp != NULL)
{
printf("%d ", temp->val);
temp = temp->next;
}
printf("
");
}
main()
{
int arr[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int n = 0;
struct ListNode* head = NULL;
Solution* obj = NULL;
int i = 0;
int val = 0;
int counts[11] = {0};
struct ListNode* temp = NULL;
struct ListNode* to_free = NULL;
n = 10;
____________⑤_____________;
obj = solutionCreate(head);
printf("原始链表: ");
printList(head);
printf("
");
printf("蓄水池采样结果(运行10次):
");
for (i = 0; i < 10; i++)
{
val = solutionGetRandom(obj);
printf("%d ", val);
}
printf("
");
printf("概率分布验证(运行10000次):
");
for (i = 0; i < 10000; i++)
{
val = solutionGetRandom(obj);
if (val >= 1 && val <= 10)
{
counts[val] = counts[val] + 1;
}
}
for (i = 1; i <= 10; i++)
{
printf("节点 %2d: %4d次, 概率: %5.2f%%
", i, counts[i], counts[i] * 100.0 / 10000);
}
printf("
");
solutionFree(obj);
temp = head;
while (temp != NULL)
{
to_free = temp;
temp = temp->next;
free(to_free);
}
}
【答案】①typedef struct ②obj->head = head
③count = count + 1 ④result = curr->val
⑤head = createList(arr, n)
2.下列程序的功能是:合并K个升序链表为一个升序链表。(每空2分,共10分)
#include <stdio.h>
#include <stdlib.h>
struct ListNode
{
int val;
struct ListNode* next;
};
struct ListNode* mergeTwoLists(struct ListNode* l1, struct ListNode* l2)
{
struct ListNode dummy;
struct ListNode* tail = &dummy;
dummy.next = NULL;
while (l1 != NULL && l2 != NULL)
{
if (_________①__________)
{
tail->next = l1;
l1 = l1->next;
}
else
{
tail->next = l2;
l2 = l2->next;
}
_____________②____________;
}
_____________③_____________;
return dummy.next;
}
struct ListNode* mergeKLists(struct ListNode** lists, int listsSize)
{
int mid;
struct ListNode* left;
struct ListNode* right;
if (listsSize == 0)
return NULL;
if (listsSize == 1)
return lists[0];
mid = listsSize / 2;
left = mergeKLists(lists, mid);
right = _______________④________________;
____________⑤______________;
}
struct ListNode* createList(int arr[], int n)
{
struct ListNode* head = NULL;
struct ListNode* tail = NULL;
int i;
struct ListNode* node;
for (i = 0; i < n; i++)
{
node = (struct ListNode*)malloc(sizeof(struct ListNode));
node->val = arr[i];
node->next = NULL;
if (head == NULL)
{
head = node;
tail = node;
}
else
{
tail->next = node;
tail = node;
}
}
return head;
}
void printList(struct ListNode* head)
{
struct ListNode* curr;
curr = head;
while (curr != NULL)
{
printf("%d ", curr->val);
curr = curr->next;
}
printf("
");
}
main()
{
int k;
int i, j;
int len;
int* arr;
struct ListNode** lists;
struct ListNode* merged;
printf("请输入链表的数量 k:");
scanf("%d", &k);
lists = (struct ListNode**)malloc(k * sizeof(struct ListNode*));
for (i = 0; i < k; i++)
{
printf("
请输入第 %d 个链表的节点个数:", i + 1);
scanf("%d", &len);
arr = (int*)malloc(len * sizeof(int));
printf("请输入第 %d 个链表的 %d 个升序整数:", i + 1, len);
for (j = 0; j < len; j++)
{
scanf("%d", &arr[j]);
}
lists[i] = createList(arr, len);
free(arr);
}
printf("
================ 输入的链表 ================
");
for (i = 0; i < k; i++)
{
printf("第 %d 个链表:", i + 1);
printList(lists[i]);
}
merged = mergeKLists(lists, k);
printf("
================ 合并后的升序链表 ================
");
printf("Merged list:");
printList(merged);
free(lists);
}
【答案】①l1->val < l2->val ②tail = tail->next
③tail->next = l1 ? l1 : l2 ④mergeKLists(lists + mid, listsSize - mid)
⑤return mergeTwoLists(left, right)
原创精品资源学科网独家享有版权,侵权必究!
学科网(北京)股份有限公司
学科网(北京)股份有限公司
$