目录
前言
数据结构实验三(上):
7-1 运用循环单链表实现约瑟夫问题;
7-2 运用顺序表实现多项式相加;
7-3 两个序列的中位数。
一、7-1 运用循环单链表实现约瑟夫问题
题目:
本题目要求读入2个整数A和B,然后输出2个整数C和D。其中A表示人数,这些人的id分别为1,2,3,...A,他们按照id依次围成一圈。从id为1的人开始报数,报到B的人退出圈,然后从下一个人开始重新报数(即从1开始报数),报到B的人又退出圈,如此反复,至到剩下2人为止。C和D为剩下的2人的id。
输入格式:
在一行中输入大于0且不超过1000的整数A和B。要求A>B。
输出格式:
在一行中输出C和D,由空格隔开。
输入样例:
在这里给出一组输入。例如:
41 3
输出样例:
在这里给出相应的输出。例如:
16 31
代码:
# include <stdio.h>
# include <stdlib.h>
typedef struct LinkList {
int id;
struct LinkList *next;
}L;
int main () {
int A,B,i;
scanf ("%d%d", &A, &B);
L *head = NULL;
L *tail = NULL;
for (i = 1; i <= A; i ++) {
L *p = (L *) malloc (sizeof (L));
p->id = i;
p->next = NULL;
if (head == NULL)
head = p;
else
tail->next = p;
tail = p;
}
tail->next = head;
//接下来报数退圈
L *pr = head; //遍历指针
int count = 1; //计数器
while (A>2) {
if (count < B) {
pr = pr->next;
count ++;
}
// L*prr = pr->next;
pr->next = pr->next->next;
// free (prr);
pr = pr->next;
count = 1;
A--;
}
printf ("%d %d\n", pr->id, pr->next->id);
return 0;
}
此处说明一下:32,34注释:不能直接free(pr->next),虽然写法正确,但后面的节点就找不到了,必须再用另一个指针指向节点:先从链上摘下,再释放!
注释32:prr指向待删除节点;
注释33:从链上摘下,此语句等价于:pr->next = prr->next;
注释34:释放。
二、7-2 运用顺序表实现多项式相加
题目:
本题要求输入两个一元多项式,然后输出它们的和(相加后得到的一元多项式)
输入格式:
输入一个整数n(表示输入组数),然后依次输入每一组数据:
输入一个整数A(表示多项式的项数,小于100),然后输入A对整数,每一对整数表示对应项的指数和系数。
输出格式:
对每一组输入,在一行中输出得到的一元多项式。
输入样例:
在这里给出一组输入。例如:
2
5
0 2
1 4
5 7
7 10
8 19
4
0 3
2 6
4 19
5 -9
3
0 3
4 7
8 2
3
0 -3
5 9
7 21
输出样例:
在这里给出相应的输出。例如:
5x^0+4x^1+6x^2+19x^4-2x^5+10x^7+19x^8
7x^4+9x^5+21x^7+2x^8
代码:
# include <stdio.h>
typedef struct Sqlist {
int index; //指数
int coef; //系数
}T;
int main () {
int i, j, k, A, m, n, coef;
T a[101], b[101], c[202];
scanf ("%d", &A);
while (A--) {
scanf ("%d", &n);
for (i = 0; i < n; i ++) {
scanf ("%d%d", &a[i].index, &a[i].coef);
}
scanf ("%d", &m);
for (i = 0; i < m; i ++) {
scanf ("%d%d", &b[i].index, &b[i].coef);
}
for (i = 0, j = 0, k = 0; i < n && j < m;) {
if (a[i].index < b[j].index)
c[k++] = a[i++];
else if (b[j].index < a[i].index)
c[k++] = b[j++];
else {
coef = a[i].coef + b[j].coef;
if (coef) {
c[k].coef = coef;
c[k++].index = a[i++].index;
j ++;
}
else {
j ++; i ++;
}
}
}
while (i < n)
c[k++] = a[i++];
while (j < m)
c[k++] = b[j++];
printf ("%dx^%d", c[0].coef, c[0].index);
for (i = 1; i < k; i ++) {
if (c[i].coef > 0)
printf ("+%dx^%d", c[i].coef, c[i].index);
else
printf ("%dx^%d", c[i].coef, c[i].index);
}
printf ("\n");
}
return 0;
}
三、7-2 运用顺序表实现多项式相加
题目:
一个长度为n(n⩾1)的升序序列S,处在第2n个位置的数称为序列S的中位数(median number),例如,序列S1={10,13,14,16,18,19}的中位数是14。两个序列的中位数是它们所有元素的升序序列的中位数,例如,S2={2,4,8,9,20,21},则S1和S2的中位数是13。现有两个等长升序序列A和B,试设计一个在时间和空间两方面都尽可能高效的算法,找出两个序列的中位数。
输入格式:
输入在三行进行,第一行1个非负整数N,表示两个数列的长度,第二行和第三行,每行N个非负整数,数与数之间用空格间隔。
输出格式:
在一行内输出一个整数。
输入样例:
6
8 11 14 15 17 19
2 4 6 9 10 12
输出样例:
10
代码:
算法一:
# include <stdio.h>
# include <stdlib.h>
int main () {
int n, i, j, res, *a, *b;
scanf ("%d", &n);
a = (int *) malloc (n*sizeof (int));
b = (int *) malloc (n*sizeof (int)); //数组长度“未知”时动态数组的创建方式
for (i = 0; i < n; i ++)
scanf ("%d", &a[i]);
for (i = 0; i < n; i ++)
scanf ("%d", &b[i]); //数组的键入
for (i = 0, j = 0; i+j < n;) {
if (a[i] >= b[j])
res = b[j ++];
else res = a[i ++]; //res标记
}
printf ("%d\n", res);
return 0;
}
算法二:
# include <stdio.h>
# include <stdlib.h>
typedef struct link {
int data;
struct link *next;
}L;
L *creatlist (L *head, int N);
L *solve (L *s1_head, L *s2_head);
int main () {
int N;
scanf ("%d", &N);
L *s1_head = NULL;
L *s2_head = NULL;
L *s3_head = NULL; //定义链表头指针
s1_head = creatlist (s1_head,N);
s2_head = creatlist (s2_head,N);
s3_head = solve (s1_head, s2_head);
//输出新链表
L *p = s3_head->next;
for (int i = 1; i <= 2*N; i ++) {
printf ("%d ", p->data);
p = p->next;
}
return 0;
}
//函数功能:创建头节点为空的链表,并返回头指针
L *creatlist (L *head, int N) {
head = (L *) malloc (sizeof (L));
head->next = NULL;
L *p = head;
for (int i = 1; i <= N; i ++) {
L *newnode = (L *) malloc (sizeof (L));
scanf ("%d", &newnode->data);
newnode->next = NULL;
p->next = newnode; //头节点没有存数据
p = p->next;
}
return head;
}
//函数功能:比较两链表各节点数值,合并数据升序排列在新链表中,返回新链表的头指针
L *solve (L *s1_head, L *s2_head) {
L *p_s1 = s1_head->next;
L *p_s2 = s2_head->next;
L *s3_head = (L *) malloc (sizeof (L));
s3_head->next = NULL; //链表3头节点
L *p_s3 = s3_head;
while (p_s1 != NULL || p_s2 != NULL) {
if (p_s1->data <= p_s2->data) { //如果链表1节点值<=链表2节点值,
L *newnode = (L *) malloc (sizeof (L));
newnode->data = p_s1->data; //则将链表1值赋给新节点
newnode->next = NULL;
p_s3->next = newnode; //链表3指针后移,链表2指针后移
p_s3 = p_s3->next;
p_s1 = p_s1->next;
}
if (p_s1->data > p_s2->data) { //情况与上述相反
L *newnode = (L *) malloc (sizeof (L));
newnode->data = p_s2->data;
newnode->next = NULL;
p_s3->next = newnode;
p_s3 = p_s3->next;
p_s2 = p_s2->next;
}
}
//最后一定有一个链表先遍历完,
if (p_s1 == NULL) {
while (p_s2 != NULL) { //如果先遍历完的是链表1,就直接将链表2中剩下的数值一一存入链表3的新节点
L *newnode = (L *) malloc (sizeof (L));
newnode->data = p_s2->data;
newnode->next = NULL;
p_s3->next = newnode;
p_s3 = p_s3->next;
p_s2 = p_s2->next;
}
}
else { //情况与上述相反
while (p_s1 != NULL) {
L *newnode = (L *) malloc (sizeof (L));
newnode->data = p_s1->data;
newnode->next = NULL;
p_s3->next = newnode;
p_s3 = p_s3->next;
p_s1 = p_s1->next;
}
}
return s3_head; //返回链表3的头指针
}