2002-1/*====================================================================================*/
/*函数名称:2002_1.c */
/*程序目的:递归地将一个数插入一个准递增序列,且插入完毕后仍然保持准递增特性 */
/*Writen by Apechn ,Soft Lab of JLU */
/*====================================================================================*/
#include <stdio.h>
struct node //定义链表结点结构,在最前面给出
{
int num;
node* next;
}
node* insert(node* head,int n)//题目要求的函数
{
node *p;
if (n <= head->num) //若n已经到了准递增序列的合适的位置,递归出口
{
p = (node*)malloc(sizeof(node));//新建一个节点用来存放n
p->num = n;
p->next = head;
head = p;
}
else
insert(head->next,n);//否则递归插入
return head;
}
2002-1/*=============================================================================*/
/*函数名称:2002_2.c */
/*函数目的:求给定序列的最长递增子序列的长度 */
/*Writen by Apechn,Soft Lab of JLU */
/*=============================================================================*/
#include <stdio.h>
const int N = 8;
int MaxLeng(int a[])
{
int i,len = 1,lmax = 1;
for (i = 1;i < N;i++)
{
if(a[i] >= a[i-1]) //如果满足递增子序列的定义,len增一
{
len ++;
contnue;
}
if (lamx < len)
lmax = len;
len = 1;
}
return lmax;
}
2002-3/*=============================================================================*/
/*函数名称:2004_3.c */
/*函数目的:用链表表示集合的相加 */
/*Writen by Apechn ,Soft Lab of JLU */
/*=============================================================================*/
#include <stdio.h>
struct node //定义链表结点结构,在最前面给出
{
int num;
node* next;
}
int ismember(int a,node* head)//子函数,判断整数a是不是在以head为头结点的链表中
{
int sign = 0; //如果在,返回1,否则返回0
node* p = head;
if (p == NULL) //注意集合为空集的情况
return sign;
while (p != NULL)
{
if (p->num == a)
{
sign = 1;
break;
}
p = p->next;
}
return sign;
}
node* SetAdd(node* P,node* Q)//题目要求的函数
{
int n;
node *r,*tail,*point1,*poing2;
if (P == NULL)
return Q;
if (Q == NULL)
return P;
r = P;
while (r != NULL) //tail用来记录P链表的尾部
{
tail = r;
r = r->next;
}
r = Q;
while (ismember(r->num,P))//找到Q中第一个不在P中的元素的位置,记为r
r = r->next;
point1 = r;
poing2 = r->next;
while (point2 != NULL) //从r开始遍历链表Q
{
n = point2->num;
if(ismember(n,P))
point1->next = point2->next;//如果遍历到的元素是P中的元素,那么把它删掉
else
point1 = point2;//否则继续
point2 = point2->next;
}
tail->next = r; //把剩余的元素放到P中
return P;
}
2002_4/*=============================================================================*/
/*函数名称:2004_4.c */
/*函数目的:用链表表示八进制数以及它们的和 */
/*Writen by Apechn ,Soft Lab of JLU */
/*=============================================================================*/
#include <stdio.h>
struct node
{
int num;
node* next;
};
node* inverse(node* head) //子函数,逆转链表的所有指针,把原来的尾结点变成头结点
{
node *f,*g,*h;
if (head != NULL)
{
f = head;
g = NULL;
while (f->next != NULL)
{
h = f;
f = f->next;
h->next = g;
g = h;
}
f->next = g;
head = f;
}
return head;
}
node* Add(node* P,node* Q) //题目要求的函数
{
node *p,*q,*r,*head = NULL;
int carry,temp;
if (P == NULL)
return Q;
if (Q == NULL)
return P;
p = inverse(P); //逆转两个链表的指针
q = inverse(Q);
carry = 0; //进位设为0
while (p != NULL && q != NULL)//逐为相加两个数,知道其中一个加完为止
{
temp = p->num + q->num +carry;
r = new node;
r->num = temp % 8;
r->next = head;
head = r;
carry = temp/8;
p = p->next;
q = q->next;
}
if (q == NULL) //修改一下指针,便于统一计算
q = p;
while (q != NULL) //把剩余的数加完
{
temp = q->num + carry;
r = new node;
r->num = temp % 8;
r->next = head;
head = r;
carry = temp/8;
q = q->next;
}
return head;
}
2002_5/*====================================================================================*/
/*函数名称:2002_5.c */
/*程序目的:将一个函数改写为递归函数 */
/*Writen by Apechn ,Soft Lab of JLU */
/*====================================================================================*/
#include <stdio.h>
#include <math.h>
int m,n;
doublef1(double x,double y)
{
L:
printf("This is S1 %d times \n",m);
m++;
printf("This is S2 %d times \n",n);
n++;
x = x + 1.5;
y = y + x;
if (y <= 100)
goto L;
return sin(y);
}
double f3(double x,double y)//递归函数
{
printf("This is S1 %d times\n",m);
m++;
printf("This is S2 %d times\n",n);
n++;
x = x + 1.5;
y = y + x;
if (y <= 100) //递归出口
return f3(x,y);
else
return sin(y);
}