顺序表
- 顺序表递增有序,插入元素x,仍递增有序
- 用顺序表最后一个元素覆盖整个顺序表中最小元素,并返回该最小元素
- 将顺序表的元素逆置
- 将a1,a2,a3……am b1,b2,b3……bn转换成b1,b2,b3……bn a1,a2,a3……am
- 删除顺序表中所有值为x的元素
- 从顺序表中删除给定值再s到t之间(包括s和t)的所有元素
- 删除有序表中所有值重复的元素
- 删除有序表中所有值重复的元素
- 将两个递增有序表 合并为 一个递增有序表
- 求两个递增序列的中位数
- 设计一个时间上尽可能高效的算法,找出数组中未出现的最小正整数
- 若一个整数序列中有过半相同元素,则称其为主元素设计算法找出数组A(a0,a1……an - 1)的主元素。(其中0 < ai < n)若存在主元素则输出,否则返回 - 1
顺序表递增有序,插入元素x,仍递增有序
#include <iostream>
using namespace std;
#define maxsize 100
typedef struct
{
int data[maxsize];
int length;
} Sqlist;
int find(Sqlist L, int x)
{
int i = 0;
for (int i = 0; i < L.length; i++)
{
if (L.data[i]<x)
{
continue;
}
else
{
return i;
}
}
}
void insert(Sqlist &L, int x)
{
int j, p;
p = find(L, x);
for (j = L.length - 1; j >= p; --j)
{
L.data[j + 1] = L.data[j];
}
L.data[p] = x;
++(L.length);
}
void insert23(Sqlist &L, int x)
{
int pos = L.data[0];
for (int i = 0; i < L.length - 1; i++)
{
if (L.data[i] < x) {
pos = i;
}
}
pos += 1;
for (int j = L.length-1; j >=pos; j--)
{
L.data[j + 1] = L.data[j];
}
L.data[pos] = x;
++L.length;
}
void insert233(Sqlist &L, int x)
{
int i;
for (i = 0; i < L.length - 1; i++)
{
if (L.data[i]>x)
{
break;
}
}
cout << "i=" << i;
cout << endl;
for (int j = L.length - 1; j >= i; j--)
{
L.data[j + 1] = L.data[j];
}
L.data[i] = x;
++L.length;
}
int main01()
{
Sqlist L = { {1, 3, 5, 8, 9}, 5 };
insert233(L, 6);
for (int j = 0; j < L.length; ++j)
{
cout << L.data[j] << endl;
}
cout << endl;
cout << L.length;
return EXIT_SUCCESS;
}
用顺序表最后一个元素覆盖整个顺序表中最小元素,并返回该最小元素
#include <iostream>
using namespace std;
#define maxsize 100
typedef struct
{
int data[maxsize];
int length;
} Sqlist;
int Del_Min(Sqlist &L)
{
int min = L.data[0];
int pos = 0;
for (int i = 0; i < L.length; ++i)
{
if (L.data[i] < min)
{
min = L.data[i];
pos = i;
}
L.data[pos] = L.data[L.length - 1];
L.length--;
}
return min;
}
int main02()
{
Sqlist L = { {5,13,3,7,10}, 5 };
int min = Del_Min(L);
for (int j = 0; j < L.length; ++j)
{
cout << L.data[j] << endl;
}
cout << endl;
cout << "" << min << endl;
return EXIT_SUCCESS;
}
将顺序表的元素逆置
#include <iostream>
using namespace std;
#define maxsize 100
typedef struct
{
int data[maxsize];
int length;
} Sqlist;
void Reverse(Sqlist &L)
{
int temp, i = 0, j = L.length - 1;
int mid = (i + j) / 2;
while (i <= mid && j >= mid)
{
temp = L.data[i];
L.data[i] = L.data[j];
L.data[j] = temp;
i++;
j--;
}
}
int main03()
{
Sqlist L = { {4,7,16,3,9,1}, 6 };
Reverse(L);
for (int j = 0; j < L.length; ++j)
{
cout << L.data[j] << endl;
}
return EXIT_SUCCESS;
}
将a1,a2,a3……am b1,b2,b3……bn转换成b1,b2,b3……bn a1,a2,a3……am
#include <iostream>
using namespace std;
void Reverse(int A[], int m, int n)
{
int mid = (m + n) / 2;
for (int i = m, j = n; i <= mid; ++i, --j)
{
int temp = A[i];
A[i] = A[j];
A[j] = temp;
}
}
void change(int A[], int m, int n)
{
Reverse(A, 0, m - 1);
Reverse(A, m, m + n - 1);
Reverse(A, 0, m + n - 1);
}
int main04()
{
int A[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
change(A, 3, 7);
for (int i = 0; i < 10; ++i)
{
cout << A[i] << " ";
}
return EXIT_SUCCESS;
}
删除顺序表中所有值为x的元素
#include <iostream>
using namespace std;
#define maxsize 100
typedef struct
{
int data[maxsize];
int length;
} Sqlist;
void del(Sqlist &L, int x)
{
int k = 0;
for (int i = 0; i <= L.length - 1; ++i)
{
if (L.data[i] != x)
{
L.data[k] = L.data[i];
++k;
}
}
L.length = k;
}
void Del(Sqlist &L, int x)
{
int k = 0;
for (int i = 0; i <= L.length - 1; ++i)
{
if (L.data[i] == x)
++k;
else
L.data[i - k] = L.data[i];
}
L.length = L.length - k;
}
int main05()
{
Sqlist L = { {12, 3, 5, 8, 9, 3}, 6 };
del(L, 3);
for (int j = 0; j < L.length; ++j)
cout << L.data[j] << endl;
return EXIT_SUCCESS;
}
从顺序表中删除给定值再s到t之间(包括s和t)的所有元素
#include <iostream>
using namespace std;
#define maxsize 100
typedef struct
{
int data[maxsize];
int length;
} Sqlist;
bool del(Sqlist &L, int s, int t)
{
int k = 0;
if (L.length == 0 || s >= t)
return false;
for (int i = 0; i < L.length; ++i)
{
if (L.data[i] >= s && L.data[i] <= t)
++k;
else
L.data[i - k] = L.data[i];
}
L.length -= k;
return true;
}
int main06()
{
Sqlist L = { {12, 13, 15, 18, 19}, 5 };
del(L, 13, 19);
for (int j = 0; j < L.length; ++j)
cout << L.data[j] << endl;
return EXIT_SUCCESS;
}
删除有序表中所有值重复的元素
#include <iostream>
using namespace std;
#define maxsize 100
typedef struct
{
int data[maxsize];
int length;
} Sqlist;
bool del(Sqlist &L)
{
int i, j;
for (i = 0, j = 1; j < L.length; ++j)
{
if (L.data[i] != L.data[j])
{
L.data[++i] = L.data[j];
}
else
{
continue;
}
}
L.length = i + 1;
return true;
}
int main07()
{
Sqlist L = { {12, 13, 13, 13, 19}, 5 };
del(L);
for (int j = 0; j < L.length; ++j)
cout << L.data[j] << endl;
return EXIT_SUCCESS;
}
删除有序表中所有值重复的元素
#include <iostream>
using namespace std;
#define maxsize 100
typedef struct
{
int data[maxsize];
int length;
} Sqlist;
bool del(Sqlist &L)
{
int i, j;
for (i = 0, j = 1; j < L.length; ++j)
{
if (L.data[i] != L.data[j])
{
L.data[++i] = L.data[j];
}
else
{
continue;
}
}
L.length = i + 1;
return true;
}
int main07()
{
Sqlist L = { {12, 13, 13, 13, 19}, 5 };
del(L);
for (int j = 0; j < L.length; ++j)
cout << L.data[j] << endl;
return EXIT_SUCCESS;
}
将两个递增有序表 合并为 一个递增有序表
#include <iostream>
using namespace std;
#define maxsize 100
typedef struct
{
int data[maxsize];
int length;
} Sqlist;
bool merge(Sqlist A, Sqlist B, Sqlist &C)
{
int i = 0, j = 0, k = 0;
while (i < A.length && j < B.length)
{
if (A.data[i] < B.data[j])
C.data[k++] = A.data[i++];
else
C.data[k++] = B.data[j++];
}
while (i < A.length)
C.data[k++] = A.data[i++];
while (j < B.length)
C.data[k++] = B.data[j++];
C.length = k;
return true;
}
int main08()
{
Sqlist A = { {2, 5, 9, 13, 19}, 5 };
Sqlist B = { {1, 6, 7, 10}, 4 };
Sqlist C;
merge(A, B, C);
for (int j = 0; j < C.length; ++j)
cout << C.data[j] << endl;
return EXIT_SUCCESS;
}
求两个递增序列的中位数
#include <iostream>
using namespace std;
#define maxsize 100
typedef struct
{
int data[maxsize];
int length;
} Sqlist;
int Find(Sqlist &A, Sqlist &B)
{
int i, j, k;
i = j = k = 0;
Sqlist C;
while (i < A.length && j < B.length)
if (A.data[i] < B.data[j])
C.data[k++] = A.data[i++];
else
C.data[k++] = B.data[j++];
while (i < A.length)
C.data[k++] = A.data[i++];
while (j < B.length)
C.data[k++] = B.data[j++];
if (C.length % 2 != 0)
{
return C.data[(A.length + B.length - 1) / 2];
}
if (C.length % 2 == 0)
{
return (C.data[(A.length + B.length - 1) / 2] + C.data[(A.length + B.length - 1) / 2 + 1])/2;
}
}
int main09()
{
Sqlist A = { {3,5,7,10}, 4 };
Sqlist B = { {4,9,15,20}, 4 };
cout << Find(A, B) << endl;
return EXIT_SUCCESS;
}
设计一个时间上尽可能高效的算法,找出数组中未出现的最小正整数
#include <iostream>
using namespace std;
int find(int A[], int n)
{
int i;
int *B = new int[n];
for (int k = 0; k < n; ++k)
{
B[k] = 0;
}
for (i = 0; i < n; ++i)
{
if (A[i] > 0 && A[i] <= n)
{
B[A[i] - 1] = 1;
}
}
for (i = 0; i < n; ++i)
{
if (B[i] == 0)
{
break;
}
}
cout << "i=" << i;
cout << endl;
delete[] B;
return i + 1;
}
int main10()
{
int A[5] = {1,2,3,4,5 };
cout << find(A, 5) << endl;
return EXIT_SUCCESS;
}
若一个整数序列中有过半相同元素,则称其为主元素设计算法找出数组A(a0,a1……an - 1)的主元素。(其中0 < ai < n)若存在主元素则输出,否则返回 - 1
#include <iostream>
using namespace std;
int fun(int A[], int n)
{
int *B = new int[n];
for (int i = 0; i < n; ++i)
{
B[i] = 0;
}
int i, k, max = 0;
for (i = 0; i < n; ++i)
{
if (A[i] > 0 && A[i] <= n)
{
B[A[i] - 1]++;
}
}
for (i = 0; i < n; ++i)
{
if (B[i] > max)
{
max = B[i];
k = i;
}
}
delete[] B;
if (max > n / 2)
return k + 1;
else
return -1;
}
int main11()
{
int A[8] = {2,3,3,6,3,5,3,3};
cout << fun(A, 8) << endl;
return EXIT_SUCCESS;
}