吉林大学2004C语言程序设计试题(含答案)
注意:1.答案一律书写在答题纸上。
2.题签随答题纸交回。
3.对每道题都要写出其中重要变量的功能。
4.书写要保持整齐,否则会影响分数。
***************************************************
1.(30分)我们称用1和0组成的串为“零幺串”,称只用1组成的串为“幺串”,称只用0组成的串为“零串”。试写一个函数(过程),使得它对任给零幺串S,将返回一个值N1和值N0,其中N1表示S中最长幺串的长度,N0表示S中最长零串的长度。例如,假设S是下面表示的零幺串,则在N1中返回4,在N0中返回3
S=00010111001110001111
[参考答案]
/*===========================================================================*/ /
*程序名称:2004_1.c */
/*程序目的:计算一个“零幺串”当中最长的零/幺串*/
/*Writen by Apechn ,Soft Lab of JLU */
/*===========================================================================*/ #include
#define MAX 100
void main(void)
{
char s[MAX]; /* 用来保存用户输入的"零/幺串" */
int i = 0; /* 计数器*/
int n0 = 0; /* 保存最长"零串"数0的个数*/
int n1 = 0; /* 保存最长"幺串"中1的个数*/
int temp0 = 0,temp1 = 0;
printf("请输入一个仅由0/1组成的字符串:\n");
scanf("%s",s); /* 由用户输入"零幺串" */
while (s)
{
if (s == '0')
temp0++;
if (temp0 > n0) /* n0取已经扫描过的"零串"中0个数最多者*/
{
n0 = temp0;
temp1 = 0; /* 处理"零串"时将"幺串"计数的临时变量置零*/
}
if (s == '1')
temp1++;
if (temp1 > n1) /* n1取已经扫描过的"零串"中1个数最多者*/
{
n1 = temp1;
temp0 = 0; /* 处理"幺串"时将"零串"计数的临时变量置零*/
}
i++;
}
printf("最长的\"零串\"中零的个数即n0 = %d .\n",n0); /* 输出结果*/
printf("最长的\"幺串\"中幺的个数即n1 = %d .\n",n1);
}
2.(30分)多项式用链表示,例如“4X5+2X2+5X+6”被表示成下面形式:
试写一个函数,使得它对任给的两个多项式链表,形成多项式相加的链表,并返回指向该链表的指针值。例如,假设给定下面P和Q两个多项式链表,则产生R多项式链表,并返回R 的指针值。注意:不能有零系数项。
[参考答案]
/*============================================================================= =======*/
/*程序名称:2004_2.c*/
/*程序目的:两个多项式相加*/
/*Writen by Apechn ,Soft Lab of JLU*/
/
*============================================================================= =======*/
#include
#include
struct list /* 节点结构声明*/
{
int power; /* 多项式中变量的幂*/
int coeff; /* 多项式中变量的系数*/
struct list *next;
};
typedef struct list node;
typedef node *poly;
/*-------------------------------------*/
/* 打印出一个多项式的各项*/
/*-------------------------------------*/
void printPoly(poly head)
{
poly pointer; /* 临时指针变量*/
pointer = head;
while (pointer != NULL) /* 打印各节点*/
{
printf("[%d,%d] ",pointer->coeff,pointer->power);
pointer = pointer->next;
}
printf("\n");
}
/*------------------------------------------------------------------------------*/
/* 从键盘输入一个多项式的项数和各项的幂和系数,多项式的系数不能为零*/
/*------------------------------------------------------------------------------*/
poly createPoly(poly head) /* 从键盘输入一个多项式的项数和各项的幂和系数*/
{
poly newNode; /* 临时节点*/
poly pointer;
int n,i;
int coeffTemp,powerTemp; /* 临时变量*/
head = (poly) malloc(sizeof(node)); /* 内存配置*/
if (head == NULL)
printf("Memory allocate Failure!!\n"); /* 内存配置失败*/
else
{
printf("请输入要创建的多项式的项数:\n");
scanf("%d",&n);
if (n == 0)
return NULL;
printf("请按x降幂依次输入各项的系数和幂:\n");
scanf("%d%d",&coeffTemp,&powerTemp);/* 输入多项式一项的系数和幂*/
head->coeff = coeffTemp; /* 定义首节点*/
head->power = powerTemp;
head->next = NULL;
pointer = head;
for (i = 1;i < n;i++) /* 依次定义其它节点*/
{
scanf("%d%d",&coeffTemp,&powerTemp); /* 输入各项的系数和幂*/ newNode = (poly) malloc(sizeof(node));
newNode->coeff = coeffTemp;
newNode->power = powerTemp;
newNode->next = NULL;
pointer->next = newNode; /* 将新定义的节点连接到原链表的表尾*/ pointer = newNode; /* pointer指针后移*/
}
}
printPoly(head);
return head;
}
/*-----------------------------------*/
/* 释放一个链表的空间*/
/*-----------------------------------*/
void freeList(poly head)
{
poly pointer; /* 临时指针变量*/
while (head != NULL)
{
pointer = head;
head = head->next;
free(pointer);
}
}
/*--------------------------------------------*/
/* 计算两个多项式的和,并将结果存入另一个链表*/
/*--------------------------------------------*/
poly addPoly(poly head1,poly head2)
{
poly pointer1,pointer2,pointer,newNode,head;/* 临时指针变量*/
pointer1 = head1;
pointer2 = head2;
head = (poly) malloc(sizeof(node));
pointer = head;
while ((pointer1 != NULL) || (pointer2 != NULL)) /* 若两个多项式至少有一个没有访问结束*/ {
if (pointer1 !=NULL && pointer2 != NULL &&(pointer1->power > pointer2->power))
{/* 若两个多项式当前指针指向的项的幂不相等*/
newNode = (poly) malloc(sizeof(node));
newNode->power = pointer1->power;
newNode->coeff = pointer1->coeff;
newNode->next = NULL;
pointer->next = newNode;
pointer = newNode;
pointer1 = pointer1->next;
}
if (pointer1 !=NULL && pointer2 != NULL &&(pointer1->power < pointer2->power))
{/* 若两个多项式当前指针指向的项的幂不相等*/
newNode = (poly) malloc(sizeof(node));
newNode->power = pointer2->power;
newNode->coeff = pointer2->coeff;
newNode->next = NULL;
pointer->next = newNode;
pointer = newNode;/* 指向存储结果的链表的指针后移*/
pointer2 = pointer2->next;
}
if (pointer1 !=NULL && pointer2 != NULL &&(pointer1->power == pointer2->power))
{ /* 若两个多项式当前指针指向的项的幂相等*/
newNode = (poly) malloc(sizeof(node));
newNode->power = pointer1->power;
newNode->coeff = pointer1->coeff + pointer2->coeff;
newNode->next = NULL;
pointer->next = newNode;
pointer = newNode;/* 指向存储结果的链表的指针后移*/
pointer1 = pointer1->next;
递归函数c语言规则
pointer2 = pointer2->next;

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。