内容正文:
编写说明:本冲刺卷严格依据湖南省计算机应用类高考考纲编写,依托《C语言程序设计》(高等教育出版社第5版),聚焦高三考生冲刺需求,助力高效提分。内容上深度覆盖考纲掌握、理解层级考点,既系统梳理构建知识框架,又强化应用能力训练;同时结合近五年高考真题,精准把握高频考点、命题趋势与题型特点,确保贴合高考方向。
湖南省计算机应用类
《C语言程序设计》
高频考点冲刺卷(八)解析版
时间:60分钟 总分:100分
班级:_________ 姓名:________ 学号:________ 成绩:_________
一、单选题(在本题的每一小题的备选答案中,只有一个答案是正确的。本大题共5小题,每小题2分,共10分)
1.已有定义int x;float y;且执行scanf(“%3d%f”,&x,&y);语句时,从第一列开始输入数据12345678<CR>,则x、y的值为( )
A.12345 678.0 B.123 45678.000000
C.45 678.0 D.345 678.0
2.若有以下程序,则程序的输出结果是( )
#include<stdio.h>
#define S(x) (x)*(x)
#define T(x) S(x)/S(x)+1
main()
{int k=3,j=2;
printf("%d,%d
",S(k+j),T(k+j)); }
A.25,2 B.25,26 C.11,12 D.11,2
3.设有定义:int w=11,x=22,y=33,z=44;,则表达式:w<x?w:y<z?y:z的值为( )
A.44 B.33 C.22 D.11
4.设有以下定义,则下列选项中叙述错误的一项是( )
union data
{ int d1;float d2;}demo;
A.变量demo与成员d2所占的内存字节数相同
B.变量demo中各个成员的地址相同
C.变量demo和各个成员的地址相同
D.若给demo.d1赋99后,demo.d2中的值是99.0
5.设有以下函数,则下列选项中关于aa函数的功能叙述正确的是( )
int aa(char *s)
{ char *t=s;
while(*t++);
t--; return (t-s);}
A.求字符串s的长度 B.比较两个字符串的大小
C.将串s复制到串t D.求字符串s所占字节数
二、程序分析题(本大题共3小题,共70分)
1.阅读程序,写出运行结果(每空2分,共14分)
(1)下列程序的运行结果是_________________。
#include <stdio.h>
main()
{
int arr[ ]={64,25,12,22,11};
int n=sizeof(arr)/sizeof(arr[0]),i,j,key;
for(i=1;i<n;i++)
{ key=arr[i];
j=i-1;
while(j>=0&&arr[j]>key)
{ arr[j+1]=arr[j];
j--;
}
arr[j+1]=key;
}
for(i=0;i<n;i++)
{ printf("%d ",arr[i]); }
}
(2)下列程序的运行结果是_________________。
#include <stdio.h>
#include <string.h>
main() {
char s[] = "ALGORITHM",temp;
int len = strlen(s);
int step = 2;
int count = 0,i,target;
for (i = 0; i < len; i++) {
target = (i + step) % len;
if (s[i] > s[target]) {
temp = s[i];
s[i] = s[target];
s[target] = temp;
step = (step + 1) % 4;
count++;
}
else {
s[i] = s[i] + 1;
if (s[i] > 'Z') s[i] = 'A';
}
}
printf("%s,%d
", s, count);
}
(3)下列程序的运行结果是_________________。
#include<stdio.h>
#include<string.h>
struct A
{
int a;
char b[10];
double c; };
void f(struct A t);
main()
{
struct A a={1001,"ZhangDa",1098.0};
f(a);
printf("%d,%s,%6.1f
",a.a,a.b,a.c);
}
void f(struct A t)
{ t.a=1002;
strcpy(t.b,"ChangRong");
t.c=1202.0;
}
(4)下列程序的运行结果是_________________。
#include <stdio.h>
void transform(int (*a)[3], int n) {
int i,j,temp;
for(i=0; i<n; i++) {
for(j=0; j<i; j++) {
temp = *(*(a+i)+j);
*(*(a+i)+j) = *(*(a+j)+i);
*(*(a+j)+i) = temp;
}
}
}
main() {
int arr[3][3] = {{1, 2, 3},{4, 5, 6},{7, 8, 9}},i,j,*p;
transform(arr, 3);
for(i=0; i<3; i++) {
for(j=0; j<3; j++) {
printf("%d ", *(*(arr+i)+j));
}
}
p=&arr[0][0];
printf(" %d
", *(p+4) + *(p+8));
}
(5)下列程序的运行结果是_________________。
#include <stdio.h>
main() {
int numbers[5] = {3, 7, 1, 9, 4};
int *p = numbers;
int min = p[0], max = p[0],i;
for(i = 1; i < 5; i++) {
if(p[i] < min) {
min = p[i]; }
if(p[i] > max) {
max = p[i]; }
}
printf("Min: %d, Max: %d
", min, max);
for(i = 0; i < 5; i++) {
if(numbers[i] % 2 == 0) {
numbers[i] = numbers[i] + min; }
else {
numbers[i] = numbers[i] - max; }
}
printf("Modified array: ");
for(i = 0; i < 5; i++) {
printf("%d ", numbers[i]);
}
printf("
");
}
(6)下列程序的运行结果是_________________。
#include <stdio.h>
int main() {
int matrix[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
int i, j, sum = 0;
for(i = 0; i < 3; i++) {
printf("Row %d: ", i);
for(j = 0; j < 3; j++) {
if(i == j) {
sum += matrix[i][j] * 2; }
else if(i < j) {
sum += matrix[i][j];}
else {
sum -= matrix[i][j]; }
}
printf(" sum=%d
", sum);
}
}
(7)下列程序的运行结果是_________________。
#include <stdio.h>
void modify(int *p, int n) {
int i;
for(i = 0; i < n; i++) {
if(i % 2 == 0) {
p[i] += 2; }
else {
p[i] -= 1; }
}
}
main() {
int arr[4] = {1, 3, 5, 7},i;
modify(arr, 4);
modify(arr + 1, 3);
for(i = 0; i < 4; i++) {
printf("%d ", arr[i]); }
printf("
"); }
2.程序填空。按照题目要求,将正确内容填入程序空白处,使程序完整(每空2分,共36分)。
(1)在下列程序中,函数fun()的功能是将形参s所指字符串中的所有数字字符顺序前移,其他字符顺序后移,处理后新字符串的首地址作为函数值返回。例如,s所指字符串为asd123fgh543df,处理后新字符串为123543asdfghdf.
请在程序的下划线处填人正确的内容,使程序得出正确的结果。(4空)
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
char *fun(char *s)
{int i,j, k,n;char *p,*t;
n=strlen(s)+1;
t=(char *)malloc(n * sizeof(char));
p=(char *)malloc(n * sizeof(char));
j=0;k=0;
for(i=0;i<n;i++)
{
if(isdigit(s[i]))
{
________①_________;
j++; }
else
{ t[k]=s[i];k++;}
}
for(i=0;i<k;i++)
___________②__________;
p[j+k]=0;
return _______③_______;
}
main()
{
char s[80];
printf("Please input:");
scanf("%s",s);
printf("
The result is:%s
", ________④________);
}
(2)已知产品销售记录包括产品编号,名称,单价,数量,金额(金额=单价x数量)。采用结构体保存数据,并按以下要求补充程序。
(1)输入m个产品的销售记录数据,将数据保存到二进制文件中。
(2)从文件中读取销售记录数据,按金额从小到大排序,若金额相同,则按产品编号从小到大排序,将排序结果保存到另一个二进制文件中。(5空)
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define M 3
struct product
{ char bh[5]; //产品编号
char name[11]; //产品名称
float dj; //单价
int s1; //数量
float je; //金额
}sell[M];
void Input_Data()
{
FILE *fp;
int i;
char fname[50];
printf("请输入产品销售记录文件名:
");
gets(fname);
if(__________①__________)
{ printf("不能打开文件,写文件失败!
");
exit(0); }
for(i=0;i<M;i++)
{
printf("请输入第%d个产品的销售记录数据:
",i+1);
printf("产品编号:");
scanf("%s",sell[i].bh);
printf("产品名称:");
scanf("%s",sell[i].name);
printf("单价:");
scanf("%f",&sell[i].dj);
printf("数量:");
scanf ("%d",&sell[i].s1);
sell[i].je=________②_________; }
if(fwrite(sell,sizeof (struct product),M,fp)!=M)
printf("文件写错误!
");
fclose(fp); }
void Sort_Data()
{
FILE *fp,*fp1;
int i,j;
char fname [50],fnamel[50];
struct product t;
printf("请输入产品销售记录文件名和排序文件名:
");
scanf("%s%s", fname,fnamel);
if((fp=fopen(fname,"rb"))==NULL)
{
printf("不能打开文件,读文件失败!
");
exit(0); }
fread(__________③___________);
fclose(fp);
for(i=1;i<=M-1;i++)
for(j=0;j<M-i;j++)
if((sell[j].je>sell[j+1].je)||_________④__________)
{
t=sell[j];
sell[j]=sell[j+1];
sell[j+1]=t;
}
if((fp1=fopen(fname,"wb"))==NULL)
{
printf("不能打开文件,写文件失败!
");
exit(0);
}
for(i=0;i<M;i++)
{
fwrite(______________⑤_______________);
printf("%s\t%s\t%f\t%d\t%f
",sell[i] .bh, sell[i].name,
sell[i].dj,sell[i].s1,sell[1].je);
}
fclose(fp1);
}
void main()
{
Input_Data();
Sort_Data();
}
(3)给定程序中,函数fun的功能是:计算形参x所指数组中N个数的平均值(规定所有数均为正数),将所指数组中大于平均值的数据移至数组的前部,小于等于平均值的数据移至x所指数组的后部,平均值作为函数值返回,在主函数中输出平均值和移动后的数据。请在程序的下划线处填入正确的内容,使程序得出正确的结
果。(4空)
#include <stdlib.h>
#include <stdio.h>
#define N 10
double fun(double *x)
{ int i, j;
double s, av, y[N];
s=0;
for(i=0; i<N; i++) s=s+x[i];
av=_______①________;
for(i=j=0; i<N; i++)
if( x[i]>av ){
________②_________; x[i]=-1;}
for(i=0; i<N; i++)
if( ______③________) y[j++]=x[i];
for(i=0; i<N; i++)x[i] = y[i];
_________④_________
}
main()
{ int i;
double x[N]= {46,30,32,40,6,17,45,15,48,26};
for(i=0; i<N; i++) printf("%4.0f ",x[i]);
printf("
");
printf("
The average is: %f
",fun(x));
printf("
The result :
",fun(x));
for(i=0; i<N; i++) printf("%5.0f ",x[i]);
printf("
");
getchar();
}
(4)王老板将养的一缸金鱼分五次出售系统上一次卖出全部的一半加二分之一条;第二次卖出余下的三分之一加三分之一条;第三次卖出余下的四分之一加四分之一条;第四次卖出余下的五分之一加五分之一条;最后卖出余下的11条。下列程序的功能是求出原来的鱼缸中共有几条金鱼。请填空。(2空)
#include<stdio.h>
int main()
{
int i,j,n=0,x;
for(i=23;n==0;i+=2)
{
for(j=1,x=i;j<=4&&x>=11;j++)
if((x+1)%(j+1)==0)
x=_______①__________;
else {x=0;break;}
if(______②_______)
{
printf("There are %d fishes at first.
",i);
n=1; }
}
}
(5)一辆以固定速度行驶的汽车,司机在上午10点看到里程表上的读数是一个对称数(即这个数从左向右读和从右向左读是完全一样的),为95859。两小时后里程表上出现了一个新的对称数。下列程序的功能是求出该车的速度和新的对称数。请填空。(3空)
#include<stdio.h>
int main()
{
int t,a[5];
long int k,i;
for(i=95860;;i++)
{
for(t=0,k=100000;k>=10;t++)
{
a[t]=________①_______;
k=k/10;
}
if(__________②__________)
{
printf("The new symmetrical number kelometers is:%d%d%d%d%d
",
a[0],a[1],a[2],a[3],a[4]);
printf("The velocity of the car is: %.2f
",(i-95859)/2.0);
________③________; }
}
}
3.阅读程序:修改程序中的错误,不得增行或删行,也不得更改程序结构,请在作答处指出错误代码所在的行号,并给出该行修改后的程序代码。(每空2分,共20分)。
(1)在此程序中,函数fun()的功能是求出如下分数数列的前n项之和。
,,,,,...
和值通过函数值返回main()函数。例如,若 n=5,则应输出8.391667.
请改正程序中的错误,使它能得出正确的结果。以下程序只允许修改两行。
L1 #include <stdlib.h>
L2 #include <conio.h>
L3 #include <stdio.h>
L4 double fun(int n)
L5 { int a,b,c,k;
L6 double s;
L7 s=0.0;
L8 a=2;b=1;
L9 for(k=1;k<n;k++)
L10 {
L11 s=s+a/b;
L12 c=a;a=a+b;b=c;
L13 }
L14 return s;
L15 }
L16 main()
L17 {
L18 int n=5;
L19 system("CLS");
L20 printf("
The value of func- tion is:%1f
",fun(n));
L21 }
①___________________________________________________
②___________________________________________________
(2)下面程序的功能是:编写函数实现putw( )和getw( )一样的功能 。以下程序只允许修改三行。
L1 #include <stdio.h>
L2 void putww(int x,file *fp)
L3 { char *a;
L4 a=(char *)&x;;
L5 fputc(a[0],fp);
L6 fputc(a[1],fp);
L7 }
L8 int getww(FILE *fp)
L9 { int *x;
L10 char a[2];
L11 a[0]=fgetc(fp);
L12 a[1]=fgetc(fp);
L13 x=(a[1]<<8)|a[0];;
L14 return x;
L15 }
L16 void main()
L17 {FILE *fp;
L18 int x;
L19 scanf("%d",&x);
L20 fp=fopen("new.txt","rb");
L21 putww(x,fp);
L22 fclose(fp);
L23 fp=fopen("new.txt","rb");
L24 x=getww(fp);
L25 printf("%d",x);
L26 fclose(fp);
L27 }
①___________________________________________________
②___________________________________________________
③___________________________________________________
(3)分子为1 的分数称为埃及分数,如:8/11=1/2+1/5+1/55+1/110,下列程序的功能是:输入一个真分数,将该分数分解为埃及分数。以下程序只允许修改两行。
L1 #include<stdio.h>
L2 int main(void)
L3 {long int a,b,c;
L4 while(1)
L5 {printf("Please enter a optional fraction(a/b):");
L6 scanf("%ld/%ld",&a,&b);
L7 printf("It can be decomposed to:");
L8 while(1)
L9 { if(b/a)
L10 c=b/a+1;
L11 else
L12 { c=b/a;
L13 a=1;}
L14 if(a==1)
L15 {printf("1/%ld
",c);
L16 continue;}
L17 else
L18 printf("1/%ld + ",c);
L19 a=a*c-b;
L20 b=b*c;
L21 if(a==3)
L22 { printf("1/%ld + 1/%ld
",b/2,b); break;}}
L23 }
L24 return 0;}
①___________________________________________________
②___________________________________________________
(4)下面程序的功能是:进行分数的通分后比较分子的大小。以下程序只允许修改三行。
L1 #include<stdio.h>
L2 int zxgb(int a,int b);
L3 int main()
L4 {
L5 int i,j,k,l,m,n;
L6 printf("Input two FENSHU:
");
L7 scanf("%d/%d,%d/%d",&i,&j,&k,&l);
L8 m=zxgb(j,l)/j*i;
L9 n=zxgb(j,l)/l*i;
L10 if(m>n)
L11 printf("%d/%d>%d/%d
",i,j,k,l);
L12 else if(m==n)
L13 printf("%d/%d=%d/%d
",i,j,k,l);
L14 else
L15 printf("%d/%d<%d/%d
",i,j,k,l);
L16 }
L17 int zxgb(int a,int b)
L18 {
L19 long int c;
L20 int d;
L21 if(a>b) c=a,a=b,b=c;
L22 for(c=a*b;b!=0;)
L23 { d=b;
L24 b=a/b;
L25 a=d; }
L26 return (int)c/a;
L27 }
①___________________________________________________
②___________________________________________________
③___________________________________________________
三、程序设计题(本大题共2小题,合计20分)
1.给定一个单链表,每k个节点一组进行反转,返回修改后的链表。如果节点总数不是k的整数倍,则最后剩余的节点保持原有顺序。(每空2分,共10分)
程序运行输出结果为:
原始链表: 1 2 3 4 5 6 7 8
K=3分组反转后: 3 2 1 6 5 4 7 8
#include <stdio.h>
#include <stdlib.h>
struct ListNode
{
int val;
struct ListNode* next; };
struct ListNode* reverseKGroup(struct ListNode* head, int k)
{
int length=0,i,j;
struct ListNode* pre,* cur,* next,* result;
_____________________①_________________________
dummy->next = head;
pre = dummy;
cur = head;
next = NULL;
if (__________②__________) return head;
while (head != NULL) {
length++;
head = head->next;
}
head = __________③__________;
for (i = 0; i < length / k; i++) {
for (j = 0; j < k - 1; j++) {
next = cur->next;
cur->next = next->next;
next->next = pre->next;
_____________④_____________;
}
pre = cur;
cur = pre->next;
}
____________⑤___________;
free(dummy);
return result;
}
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 *cur;
struct ListNode *head=(struct ListNode*)malloc(sizeof(struct ListNode));
head->val = arr[0];
cur = head;
if (n == 0) return NULL;
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;
}
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 = reverseKGroup(head, 3);
printf("K=3分组反转后: ");
printList(head);
}
2.下列程序的功能是对单链表进行排序。(每空2分,共10分)
输入:4->2->1->3->5->9->7->8->6
输出:
原始链表: 4 2 1 3 5 9 7 8 6
排序后链表: 1 2 3 4 5 6 7 8 9
#include <stdio.h>
#include <stdlib.h>
struct ListNode {
int val;
struct ListNode* next;
};
struct ListNode* merge(struct ListNode* l1, struct ListNode* l2)
{
struct ListNode dummy;
struct ListNode* tail = &dummy;
dummy.next = ________①________;
while (l1 != NULL && l2 != NULL)
{
if (_________②__________)
{
tail->next = l1;
l1 = l1->next;
}
else
{
tail->next = l2;
l2 = l2->next;
}
tail = tail->next;
}
_____________③____________;
return dummy.next;
}
struct ListNode* sortList(struct ListNode* head)
{
struct ListNode *slow,*fast,*mid,*left,*right;
if (head == NULL || head->next == NULL)
return head;
slow = head;
fast = head->next;
while (___________④___________)
{
slow = slow->next;
fast = fast->next->next;
}
mid = slow->next;
slow->next = NULL;
left = sortList(head);
right = sortList(mid);
return _________⑤____________;
}
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->next = (struct ListNode*)malloc(sizeof(struct ListNode));
cur = cur->next;
cur->val = arr[i];
}
cur->next = NULL;
return head;
}
main()
{
int arr[] = {4, 2, 1, 3, 5, 9, 7, 8, 6};
int n = sizeof(arr) / sizeof(arr[0]);
struct ListNode* head = createList(arr, n);
printf("原始链表: ");
printList(head);
head = sortList(head);
printf("排序后链表: ");
printList(head);
}
原创精品资源学科网独家享有版权,侵权必究!
学科网(北京)股份有限公司
学科网(北京)股份有限公司
$
编写说明:本冲刺卷严格依据湖南省计算机应用类高考考纲编写,依托《C语言程序设计》(高等教育出版社第5版),聚焦高三考生冲刺需求,助力高效提分。内容上深度覆盖考纲掌握、理解层级考点,既系统梳理构建知识框架,又强化应用能力训练;同时结合近五年高考真题,精准把握高频考点、命题趋势与题型特点,确保贴合高考方向。
湖南省计算机应用类
《C语言程序设计》
高频考点冲刺卷(八)解析版
时间:60分钟 总分:100分
班级:_________ 姓名:________ 学号:________ 成绩:_________
一、单选题(在本题的每一小题的备选答案中,只有一个答案是正确的。本大题共5小题,每小题2分,共10分)
1.已有定义int x;float y;且执行scanf(“%3d%f”,&x,&y);语句时,从第一列开始输入数据12345678<CR>,则x、y的值为( )
A.12345 678.0 B.123 45678.000000
C.45 678.0 D.345 678.0
【答案】B
【解析】%3d 表示最多读取 3 位数字,因此从 12345678 中取前 3 位 123 赋给 x,剩余的 45678 赋给 y,所以 x=123,y=45678.000000。
2.若有以下程序,则程序的输出结果是( )
#include<stdio.h>
#define S(x) (x)*(x)
#define T(x) S(x)/S(x)+1
main()
{int k=3,j=2;
printf("%d,%d
",S(k+j),T(k+j)); }
A.25,2 B.25,26 C.11,12 D.11,2
【答案】B
【解析】S(k+j)=(3+2)*(3+2)=25,T(k+j)=(3+2)*(3+2)/(3+2)*(3+2)+1=25/5*5+1=26,输出25,26。
3.设有定义:int w=11,x=22,y=33,z=44;,则表达式:w<x?w:y<z?y:z的值为( )
A.44 B.33 C.22 D.11
【答案】D
【解析】条件运算符结合性为“自右至左”。
4.设有以下定义,则下列选项中叙述错误的一项是( )
union data
{ int d1;float d2;}demo;
A.变量demo与成员d2所占的内存字节数相同
B.变量demo中各个成员的地址相同
C.变量demo和各个成员的地址相同
D.若给demo.d1赋99后,demo.d2中的值是99.0
【答案】D
【解析】共用体的存储特点:(1)同一块内存可以存放不同类型的数据,但在某一-时刻只能存放其中的一种;(2)共用体变量中起作用的成员是最后-次存放的成员,即在存入一个新的成员后原有的成员失去作用,所以选项D的说法是错误的;(3)共用体变量的地址和它的成员的地址是同-个地址,且各成员的地址也相同,因为共用体成员是共同占用同-一段存储空间的,所以选项B,C的说法都正确;(4)共用体变量不能整体被赋值,也不能给共用体变量赋初值;(5)共用体所占的内存长度等于最长的成员的长度,所以选项A的说法正确。故本题答。案为D。
5.设有以下函数,则下列选项中关于aa函数的功能叙述正确的是( )
int aa(char *s)
{ char *t=s;
while(*t++);
t--; return (t-s);}
A.求字符串s的长度 B.比较两个字符串的大小
C.将串s复制到串t D.求字符串s所占字节数
【答案】A
【解析】函数aaa中首先定义了字符型指针变量t,用于指向指针变量s所在存储单元的首地址。通过while循环将指针变量t指向字符串s的结束标志'\0'所在的地址,再通过"t--"将指针变量t前移一个字符地址,即指向字符串s的第一个字符。最后,通过"t-s"用最后一个字符的地址减去第一个字符的地址,则得到字符串s的长度。故本题答案为A
二、程序分析题(本大题共3小题,共70分)
1.阅读程序,写出运行结果(每空2分,共14分)
(1)下列程序的运行结果是_________________。
#include <stdio.h>
main()
{
int arr[ ]={64,25,12,22,11};
int n=sizeof(arr)/sizeof(arr[0]),i,j,key;
for(i=1;i<n;i++)
{ key=arr[i];
j=i-1;
while(j>=0&&arr[j]>key)
{ arr[j+1]=arr[j];
j--;
}
arr[j+1]=key;
}
for(i=0;i<n;i++)
{ printf("%d ",arr[i]); }
}
【答案】11 12 22 25 64
(2)下列程序的运行结果是_________________。
#include <stdio.h>
#include <string.h>
main() {
char s[] = "ALGORITHM",temp;
int len = strlen(s);
int step = 2;
int count = 0,i,target;
for (i = 0; i < len; i++) {
target = (i + step) % len;
if (s[i] > s[target]) {
temp = s[i];
s[i] = s[target];
s[target] = temp;
step = (step + 1) % 4;
count++;
}
else {
s[i] = s[i] + 1;
if (s[i] > 'Z') s[i] = 'A';
}
}
printf("%s,%d
", s, count);
}
【答案】BMHIHPUSN,2
(3)下列程序的运行结果是_________________。
#include<stdio.h>
#include<string.h>
struct A
{
int a;
char b[10];
double c; };
void f(struct A t);
main()
{
struct A a={1001,"ZhangDa",1098.0};
f(a);
printf("%d,%s,%6.1f
",a.a,a.b,a.c);
}
void f(struct A t)
{ t.a=1002;
strcpy(t.b,"ChangRong");
t.c=1202.0;
}
【答案】1001,ZhangDa,1098.0
(4)下列程序的运行结果是_________________。
#include <stdio.h>
void transform(int (*a)[3], int n) {
int i,j,temp;
for(i=0; i<n; i++) {
for(j=0; j<i; j++) {
temp = *(*(a+i)+j);
*(*(a+i)+j) = *(*(a+j)+i);
*(*(a+j)+i) = temp;
}
}
}
main() {
int arr[3][3] = {{1, 2, 3},{4, 5, 6},{7, 8, 9}},i,j,*p;
transform(arr, 3);
for(i=0; i<3; i++) {
for(j=0; j<3; j++) {
printf("%d ", *(*(arr+i)+j));
}
}
p=&arr[0][0];
printf(" %d
", *(p+4) + *(p+8));
}
【答案】1 4 7 2 5 8 3 6 9 14
(5)下列程序的运行结果是_________________。
#include <stdio.h>
main() {
int numbers[5] = {3, 7, 1, 9, 4};
int *p = numbers;
int min = p[0], max = p[0],i;
for(i = 1; i < 5; i++) {
if(p[i] < min) {
min = p[i]; }
if(p[i] > max) {
max = p[i]; }
}
printf("Min: %d, Max: %d
", min, max);
for(i = 0; i < 5; i++) {
if(numbers[i] % 2 == 0) {
numbers[i] = numbers[i] + min; }
else {
numbers[i] = numbers[i] - max; }
}
printf("Modified array: ");
for(i = 0; i < 5; i++) {
printf("%d ", numbers[i]);
}
printf("
");
}
【答案】
Min: 1, Max: 9
Modified array: -6 -2 -8 0 5
(6)下列程序的运行结果是_________________。
#include <stdio.h>
int main() {
int matrix[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
int i, j, sum = 0;
for(i = 0; i < 3; i++) {
printf("Row %d: ", i);
for(j = 0; j < 3; j++) {
if(i == j) {
sum += matrix[i][j] * 2; }
else if(i < j) {
sum += matrix[i][j];}
else {
sum -= matrix[i][j]; }
}
printf(" sum=%d
", sum);
}
}
【答案】
Row 0: sum=7
Row 1: sum=19
Row 2: sum=22
(7)下列程序的运行结果是_________________。
#include <stdio.h>
void modify(int *p, int n) {
int i;
for(i = 0; i < n; i++) {
if(i % 2 == 0) {
p[i] += 2; }
else {
p[i] -= 1; }
}
}
main() {
int arr[4] = {1, 3, 5, 7},i;
modify(arr, 4);
modify(arr + 1, 3);
for(i = 0; i < 4; i++) {
printf("%d ", arr[i]); }
printf("
"); }
【答案】3 4 6 8
2.程序填空。按照题目要求,将正确内容填入程序空白处,使程序完整(每空2分,共36分)。
(1)在下列程序中,函数fun()的功能是将形参s所指字符串中的所有数字字符顺序前移,其他字符顺序后移,处理后新字符串的首地址作为函数值返回。例如,s所指字符串为asd123fgh543df,处理后新字符串为123543asdfghdf.
请在程序的下划线处填人正确的内容,使程序得出正确的结果。(4空)
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
char *fun(char *s)
{int i,j, k,n;char *p,*t;
n=strlen(s)+1;
t=(char *)malloc(n * sizeof(char));
p=(char *)malloc(n * sizeof(char));
j=0;k=0;
for(i=0;i<n;i++)
{
if(isdigit(s[i]))
{
________①_________;
j++; }
else
{ t[k]=s[i];k++;}
}
for(i=0;i<k;i++)
___________②__________;
p[j+k]=0;
return _______③_______;
}
main()
{
char s[80];
printf("Please input:");
scanf("%s",s);
printf("
The result is:%s
", ________④________);
}
【答案】①p[j]=s[i] ②p[j+i]=t[i] ③p ④fun(s)
(2)已知产品销售记录包括产品编号,名称,单价,数量,金额(金额=单价x数量)。采用结构体保存数据,并按以下要求补充程序。
(1)输入m个产品的销售记录数据,将数据保存到二进制文件中。
(2)从文件中读取销售记录数据,按金额从小到大排序,若金额相同,则按产品编号从小到大排序,将排序结果保存到另一个二进制文件中。(5空)
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define M 3
struct product
{ char bh[5]; //产品编号
char name[11]; //产品名称
float dj; //单价
int s1; //数量
float je; //金额
}sell[M];
void Input_Data()
{
FILE *fp;
int i;
char fname[50];
printf("请输入产品销售记录文件名:
");
gets(fname);
if(__________①__________)
{ printf("不能打开文件,写文件失败!
");
exit(0); }
for(i=0;i<M;i++)
{
printf("请输入第%d个产品的销售记录数据:
",i+1);
printf("产品编号:");
scanf("%s",sell[i].bh);
printf("产品名称:");
scanf("%s",sell[i].name);
printf("单价:");
scanf("%f",&sell[i].dj);
printf("数量:");
scanf ("%d",&sell[i].s1);
sell[i].je=________②_________; }
if(fwrite(sell,sizeof (struct product),M,fp)!=M)
printf("文件写错误!
");
fclose(fp); }
void Sort_Data()
{
FILE *fp,*fp1;
int i,j;
char fname [50],fnamel[50];
struct product t;
printf("请输入产品销售记录文件名和排序文件名:
");
scanf("%s%s", fname,fnamel);
if((fp=fopen(fname,"rb"))==NULL)
{
printf("不能打开文件,读文件失败!
");
exit(0); }
fread(__________③___________);
fclose(fp);
for(i=1;i<=M-1;i++)
for(j=0;j<M-i;j++)
if((sell[j].je>sell[j+1].je)||_________④__________)
{
t=sell[j];
sell[j]=sell[j+1];
sell[j+1]=t;
}
if((fp1=fopen(fname,"wb"))==NULL)
{
printf("不能打开文件,写文件失败!
");
exit(0);
}
for(i=0;i<M;i++)
{
fwrite(______________⑤_______________);
printf("%s\t%s\t%f\t%d\t%f
",sell[i] .bh, sell[i].name,
sell[i].dj,sell[i].s1,sell[1].je);
}
fclose(fp1);
}
void main()
{
Input_Data();
Sort_Data();
}
【答案】①(fp=fopen(fname,"wb"))==NULL ②sell[i].dj*sell[i].s1
③sell,sizeof (struct product),M,fp
④(sell[j].je==sell[j+1].je&& strcmp(sell[j].bh,sell[j+1].bh)>0)
⑤&sell[i],sizeof(struct product),1,fp1
(3)给定程序中,函数fun的功能是:计算形参x所指数组中N个数的平均值(规定所有数均为正数),将所指数组中大于平均值的数据移至数组的前部,小于等于平均值的数据移至x所指数组的后部,平均值作为函数值返回,在主函数中输出平均值和移动后的数据。请在程序的下划线处填入正确的内容,使程序得出正确的结
果。(4空)
#include <stdlib.h>
#include <stdio.h>
#define N 10
double fun(double *x)
{ int i, j;
double s, av, y[N];
s=0;
for(i=0; i<N; i++) s=s+x[i];
av=_______①________;
for(i=j=0; i<N; i++)
if( x[i]>av ){
________②_________; x[i]=-1;}
for(i=0; i<N; i++)
if( ______③________) y[j++]=x[i];
for(i=0; i<N; i++)x[i] = y[i];
_________④_________
}
main()
{ int i;
double x[N]= {46,30,32,40,6,17,45,15,48,26};
for(i=0; i<N; i++) printf("%4.0f ",x[i]);
printf("
");
printf("
The average is: %f
",fun(x));
printf("
The result :
",fun(x));
for(i=0; i<N; i++) printf("%5.0f ",x[i]);
printf("
");
getchar();
}
【答案】①s/N ②y[j++]=x[i] ③x[i]!=-1 ④return av;
(4)王老板将养的一缸金鱼分五次出售系统上一次卖出全部的一半加二分之一条;第二次卖出余下的三分之一加三分之一条;第三次卖出余下的四分之一加四分之一条;第四次卖出余下的五分之一加五分之一条;最后卖出余下的11条。下列程序的功能是求出原来的鱼缸中共有几条金鱼。请填空。(2空)
#include<stdio.h>
int main()
{
int i,j,n=0,x;
for(i=23;n==0;i+=2)
{
for(j=1,x=i;j<=4&&x>=11;j++)
if((x+1)%(j+1)==0)
x=_______①__________;
else {x=0;break;}
if(______②_______)
{
printf("There are %d fishes at first.
",i);
n=1; }
}
}
【答案】①x-(x+1)/(j+1) ②j==5&&x==11
(5)一辆以固定速度行驶的汽车,司机在上午10点看到里程表上的读数是一个对称数(即这个数从左向右读和从右向左读是完全一样的),为95859。两小时后里程表上出现了一个新的对称数。下列程序的功能是求出该车的速度和新的对称数。请填空。(3空)
#include<stdio.h>
int main()
{
int t,a[5];
long int k,i;
for(i=95860;;i++)
{
for(t=0,k=100000;k>=10;t++)
{
a[t]=________①_______;
k=k/10;
}
if(__________②__________)
{
printf("The new symmetrical number kelometers is:%d%d%d%d%d
",
a[0],a[1],a[2],a[3],a[4]);
printf("The velocity of the car is: %.2f
",(i-95859)/2.0);
________③________; }
}
}
【答案】①(i%k)/(k/10) ②(a[0]==a[4])&&(a[1]==a[3])
③break
3.阅读程序:修改程序中的错误,不得增行或删行,也不得更改程序结构,请在作答处指出错误代码所在的行号,并给出该行修改后的程序代码。(每空2分,共20分)。
(1)在此程序中,函数fun()的功能是求出如下分数数列的前n项之和。
,,,,,...
和值通过函数值返回main()函数。例如,若 n=5,则应输出8.391667.
请改正程序中的错误,使它能得出正确的结果。以下程序只允许修改两行。
L1 #include <stdlib.h>
L2 #include <conio.h>
L3 #include <stdio.h>
L4 double fun(int n)
L5 { int a,b,c,k;
L6 double s;
L7 s=0.0;
L8 a=2;b=1;
L9 for(k=1;k<n;k++)
L10 {
L11 s=s+a/b;
L12 c=a;a=a+b;b=c;
L13 }
L14 return s;
L15 }
L16 main()
L17 {
L18 int n=5;
L19 system("CLS");
L20 printf("
The value of func- tion is:%1f
",fun(n));
L21 }
①___________________________________________________
②___________________________________________________
【答案】①L9 for(k=1;k<=n;k++)
②L11 s=s+(double)a/b;
(2)下面程序的功能是:编写函数实现putw( )和getw( )一样的功能 。以下程序只允许修改三行。
L1 #include <stdio.h>
L2 void putww(int x,file *fp)
L3 { char *a;
L4 a=(char *)&x;;
L5 fputc(a[0],fp);
L6 fputc(a[1],fp);
L7 }
L8 int getww(FILE *fp)
L9 { int *x;
L10 char a[2];
L11 a[0]=fgetc(fp);
L12 a[1]=fgetc(fp);
L13 x=(a[1]<<8)|a[0];;
L14 return x;
L15 }
L16 void main()
L17 {FILE *fp;
L18 int x;
L19 scanf("%d",&x);
L20 fp=fopen("new.txt","rb");
L21 putww(x,fp);
L22 fclose(fp);
L23 fp=fopen("new.txt","rb");
L24 x=getww(fp);
L25 printf("%d",x);
L26 fclose(fp);
L27 }
①___________________________________________________
②___________________________________________________
③___________________________________________________
【答案】①L2 void putww(int x,FILE *fp)
②L9 { int x; ③L20 fp=fopen("new.txt","wb");
(3)分子为1 的分数称为埃及分数,如:8/11=1/2+1/5+1/55+1/110,下列程序的功能是:输入一个真分数,将该分数分解为埃及分数。以下程序只允许修改两行。
L1 #include<stdio.h>
L2 int main(void)
L3 {long int a,b,c;
L4 while(1)
L5 {printf("Please enter a optional fraction(a/b):");
L6 scanf("%ld/%ld",&a,&b);
L7 printf("It can be decomposed to:");
L8 while(1)
L9 { if(b/a)
L10 c=b/a+1;
L11 else
L12 { c=b/a;
L13 a=1;}
L14 if(a==1)
L15 {printf("1/%ld
",c);
L16 continue;}
L17 else
L18 printf("1/%ld + ",c);
L19 a=a*c-b;
L20 b=b*c;
L21 if(a==3)
L22 { printf("1/%ld + 1/%ld
",b/2,b); break;}}
L23 }
L24 return 0;}
①___________________________________________________
②___________________________________________________
【答案】①L9 { if(b%a)
②L16 break;}
(4)下面程序的功能是:进行分数的通分后比较分子的大小。以下程序只允许修改三行。
L1 #include<stdio.h>
L2 int zxgb(int a,int b);
L3 int main()
L4 {
L5 int i,j,k,l,m,n;
L6 printf("Input two FENSHU:
");
L7 scanf("%d/%d,%d/%d",&i,&j,&k,&l);
L8 m=zxgb(j,l)/j*i;
L9 n=zxgb(j,l)/l*i;
L10 if(m>n)
L11 printf("%d/%d>%d/%d
",i,j,k,l);
L12 else if(m==n)
L13 printf("%d/%d=%d/%d
",i,j,k,l);
L14 else
L15 printf("%d/%d<%d/%d
",i,j,k,l);
L16 }
L17 int zxgb(int a,int b)
L18 {
L19 long int c;
L20 int d;
L21 if(a>b) c=a,a=b,b=c;
L22 for(c=a*b;b!=0;)
L23 { d=b;
L24 b=a/b;
L25 a=d; }
L26 return (int)c/a;
L27 }
①___________________________________________________
②___________________________________________________
③___________________________________________________
【答案】①L9 n=zxgb(j,l)/l*k;
②L21 if(a<b) c=a,a=b,b=c;
③L24 b=a%b;
三、程序设计题(本大题共2小题,合计20分)
1.给定一个单链表,每k个节点一组进行反转,返回修改后的链表。如果节点总数不是k的整数倍,则最后剩余的节点保持原有顺序。(每空2分,共10分)
程序运行输出结果为:
原始链表: 1 2 3 4 5 6 7 8
K=3分组反转后: 3 2 1 6 5 4 7 8
#include <stdio.h>
#include <stdlib.h>
struct ListNode
{
int val;
struct ListNode* next; };
struct ListNode* reverseKGroup(struct ListNode* head, int k)
{
int length=0,i,j;
struct ListNode* pre,* cur,* next,* result;
_____________________①_________________________
dummy->next = head;
pre = dummy;
cur = head;
next = NULL;
if (__________②__________) return head;
while (head != NULL) {
length++;
head = head->next;
}
head = __________③__________;
for (i = 0; i < length / k; i++) {
for (j = 0; j < k - 1; j++) {
next = cur->next;
cur->next = next->next;
next->next = pre->next;
_____________④_____________;
}
pre = cur;
cur = pre->next;
}
____________⑤___________;
free(dummy);
return result;
}
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 *cur;
struct ListNode *head=(struct ListNode*)malloc(sizeof(struct ListNode));
head->val = arr[0];
cur = head;
if (n == 0) return NULL;
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;
}
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 = reverseKGroup(head, 3);
printf("K=3分组反转后: ");
printList(head);
}
【答案】①struct ListNode *dummy=(struct ListNode*)malloc(sizeof(struct ListNode));
②head == NULL || k == 1 ③dummy->next
④pre->next = next ⑤result = dummy->next
2.下列程序的功能是对单链表进行排序。(每空2分,共10分)
输入:4->2->1->3->5->9->7->8->6
输出:
原始链表: 4 2 1 3 5 9 7 8 6
排序后链表: 1 2 3 4 5 6 7 8 9
#include <stdio.h>
#include <stdlib.h>
struct ListNode {
int val;
struct ListNode* next;
};
struct ListNode* merge(struct ListNode* l1, struct ListNode* l2)
{
struct ListNode dummy;
struct ListNode* tail = &dummy;
dummy.next = ________①________;
while (l1 != NULL && l2 != NULL)
{
if (_________②__________)
{
tail->next = l1;
l1 = l1->next;
}
else
{
tail->next = l2;
l2 = l2->next;
}
tail = tail->next;
}
_____________③____________;
return dummy.next;
}
struct ListNode* sortList(struct ListNode* head)
{
struct ListNode *slow,*fast,*mid,*left,*right;
if (head == NULL || head->next == NULL)
return head;
slow = head;
fast = head->next;
while (___________④___________)
{
slow = slow->next;
fast = fast->next->next;
}
mid = slow->next;
slow->next = NULL;
left = sortList(head);
right = sortList(mid);
return _________⑤____________;
}
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->next = (struct ListNode*)malloc(sizeof(struct ListNode));
cur = cur->next;
cur->val = arr[i];
}
cur->next = NULL;
return head;
}
main()
{
int arr[] = {4, 2, 1, 3, 5, 9, 7, 8, 6};
int n = sizeof(arr) / sizeof(arr[0]);
struct ListNode* head = createList(arr, n);
printf("原始链表: ");
printList(head);
head = sortList(head);
printf("排序后链表: ");
printList(head);
}
【答案】①NULL ②l1->val < l2->val
③tail->next = (l1 != NULL) ? l1 : l2 ④fast != NULL && fast->next != NULL
⑤merge(left, right)
原创精品资源学科网独家享有版权,侵权必究!
学科网(北京)股份有限公司
学科网(北京)股份有限公司
$