1,合并线性表La到Lb中(重复的值要1个就欧克)
#include<stdio.h>
#include<assert.h>
#include<stdlib.h>
#include"Seqlist.h"
void unio(Seqlist*la,Seqlist*lb)
{
assert(la!=nullptr&&lb!=nullptr);
int n = lb->cursize;
for (int i = 0; i < n; i++)
{
if (!LocalElem(la, lb->data[i]))
{
Insert_back(la,lb->data[i]);
}
}
}
int main()
{
int ar[] = {2,4,6,8,10};
int an = sizeof(ar) / sizeof(ar[0]);
int br[] = {1,3,2,4,5,8,9,10,12,13};
int bn = sizeof(br) / sizeof(br[0]);
Seqlist La, Lb;
Init(&La);
Init(&Lb);
for (int i = 0; i < an; i++)
{
Insert_back(&La,ar[i]);
}
for (int i = 0; i < bn; i++)
{
Insert_back(&Lb, br[i]);
}
Printf(&La);
Printf(&Lb);
unio(&La, &Lb);
Printf(&La);
return 0;
}
数组中重复的数字
题目信息:假设给定数组的长度为n+1;内容位1到n所以里面必定有重复的值,假设只有一个重复的值;
1,求和然后和等差数列前所有项之和相减得出重复的数字
2,暴力法,先排序分别用数组中的数字和它后面的内容相比较两个for循环实现;
3,查表法
#include<stdio.h>
#include<assert.h>
#include<stdlib.h>
#include<string.h>
#include"Seqlist.h"
int Find_same(int*a,int n,int Max)
{
assert(a!=nullptr);
int*b= (int*)malloc(sizeof(int) * Max);
memset(b,0,sizeof(int)*Max);
int tem = -1;
for (int i = 0; i < n; i++)
{
int j = a[i];
if (b[j] == 0)
{
b[j] += 1;
}
else
{
tem =j;
break;
}
}
return tem;
}
int main()
{
int ar[] = {1,2,3,4,2,5};
int n = sizeof(ar) / sizeof(ar[1]);
int Max = 5;
printf("%d\n", Find_same(ar, n, Max));
return 0;
}
4,双下标法(快慢指针)
#include<stdio.h>
#include<assert.h>
#include<stdlib.h>
#include<string.h>
#include"Seqlist.h"
int Find_same(int *a,int n)
{
assert(a != nullptr);
int fast = 0;
int slow = 0;
{
slow = a[slow];
fast = a[fast];
fast = a[fast];
} while (fast != slow);
slow = 0;
while (slow != fast)
{
slow = a[slow];
fast = a[fast];
}
return fast;
}
int main()
{
int ar[] = { 1,2,3,4,2,5 };
int n = sizeof(ar) / sizeof(ar[1]);
printf("%d\n", Find_same(ar, n));
return 0;
}