導航:首頁 > 源碼編譯 > 線性表數據演算法

線性表數據演算法

發布時間:2023-12-30 05:24:14

⑴ 數據結構演算法 兩線性表A,B求交集。。。請高手指點!!!

將A與B分別排序,然後求交。
例如:將A與B按升序排列,設A表頭為P,B表頭為Q,若A[P]>B[Q]那麼Q++,若A[P]<B[Q]那麼P++;如果A[P]=B[Q],Q++、P++,Count++,And[Count]=B[Q-1];當P或者Q其中一個達到了A或者B的表尾 演算法結束。

以下是參考程序:
//----------------------------------------

#include <stdio.h>

int A[100001],B[100001];
int Ans[100001],Count,N,M;

void Swap(int &a,int &b)
{
int Temp=a;
a=b;
b=Temp;
}

void Sort(int L,int R,int A[])
{
int p,q,Mid;
if (L==R) return ;
Mid=A[(L+R)/2];
p=L-1;q=R+1;
do
{
p++;q--;
while (A[p]<Mid) p++;
while (A[q]>Mid) q--;
if (p<q) Swap(A[p],A[q]);
}while (p<q);
Sort(L,q,A);
Sort(q+1,R,A);
}

void Init()
{
int p,q;
p=q=1;
for (int i=2;i<=N;i++)
{
if (A[i]!=A[p])
{
p++;
A[p]=A[i];
}
}
for (int i=2;i<=M;i++)
{
if (A[i]!=B[q])
{
q++;
B[q]=A[i];
}
}

}

int main(void)
{
int p,q;
scanf("%d %d",&N,&M);//輸入兩個集合的元素的個數
for (int i=1;i<=N;i++)
scanf("%d",&A[i]);// 讀取A集合
for (int i=1;i<=M;i++)
scanf("%d",&B[i]);//讀取B集合
Sort(1,N,A);//A集合排序
Sort(1,M,B);//B集合排序
Init();//剔除同一集合的相同元素
p=q=1;
Count=0;
while (p<=N&&q<=M)//求解.
{
if (A[p]<B[q])
{
p++;
continue;
}
if (A[p]>B[q])
{
q++;
continue;
}
if (A[p]==B[q])
{
p++;q++;
Count++;
Ans[Count]=B[q-1];
continue;
}
}
for (int i=1;i<=Count;i++)
printf("%d ",Ans[i]);
return 0;
}

閱讀全文

與線性表數據演算法相關的資料

熱點內容
java列出所有文件 瀏覽:865
壓縮包看圖軟體 瀏覽:186
sqlite在android中的應用 瀏覽:656
一本通pdf 瀏覽:910
2021免費的編程軟體 瀏覽:123
項目編譯後瀏覽器不對應刷新 瀏覽:564
三星升級android60 瀏覽:292
粘土的壓縮模量 瀏覽:115
美國程序員生活 瀏覽:219
51單片機摘要 瀏覽:405
英語經典pdf下載 瀏覽:317
大學文件夾怎麼刪除 瀏覽:668
linux科研軟體 瀏覽:553
ue4打包編譯著色器 瀏覽:775
雲伺服器可以在手機上登錄嗎 瀏覽:677
網游腳本為什麼要連接伺服器 瀏覽:10
程序員發展路線圖 瀏覽:320
手機語音加密會議 瀏覽:592
冰與火pdf 瀏覽:421
為什麼叫我買阿里雲伺服器 瀏覽:475