代码还是需要敲的,只看没用,打出来才能体会到编程的美!才能找到自己的逻辑漏洞!
#include
/*------------------------------------------------------
* 2019考研数据结构 王道程序设计题目
* 章节:第一章线性表
* 平台+工具:win10+clion
* 编程:倾斜的正弦波
* ----------------------------------------------------*/
#define Maxlen 20
typedef struct {
int data[Maxlen];
int len;
} Sqlist;
Sqlist sq;
int len = 10;
int stack[20] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
int test_e = 0;
//删除顺序表中最小元素,然后用最后的元素填充所删除的位置
bool test_01(Sqlist &L, int &e) {
int min_vaule = L.data[0];
int min = 0;
if (L.len == 0)return false;
for (int i = 0; i < L.len; ++i) {
if (L.data[i] < min_vaule) {
min = i;
}
}
e = L.data[min];
L.data[min] = L.data[L.len - 1];
return true;
}
//顺序表元素逆置
bool test_02(Sqlist &L) {
int temp = 0;
for (int i = 0; i < L.len / 2; ++i) {
temp = L.data[i];
L.data[i] = L.data[L.len - i - 1];
L.data[L.len - i - 1] = temp;
}
return true;
}
//删除顺序表元素为e的元素
bool test_03(Sqlist &L, int e) {
int k;
for (int i = 0; i < L.len; ++i) {
if (L.data[i] == e)k++;
else {
L.data[i - k] = L.data[i];
}
}
L.len -= k;
return true;
}
//删除有序顺序表中大于s小于t的数(s= t || L.len == 0)return false;
for (i = 0; i < L.len && L.data[i] < s; ++i);
if (i >= L.len)return false;
for (j = i; j < L.len && L.data[j] <= t; ++j);
for (; j < L.len; i++, j++) {
L.data[i] = L.data[j];
}
L.len = i;
return true;
}
//删除无序表中大于s小于t的元素
bool test_05(Sqlist &L, int s, int t) {
int k = 0;
if (L.len == 0 || s >= t)return false;
for (int i = 0; i < L.len; ++i) {
if (L.data[i] >= s && L.data[i] <= t) {
k++;
} else {
L.data[i - k] = L.data[i];
}
}
L.len -= k;
return true;
}
//删除所有序表中的重复元素
bool test_06(Sqlist &L) {
if (L.len == 0)return false;
int i, j;
for (i = 0, j = 1; j < L.len; ++j) {
if (L.data[i] != L.data[j]) {
L.data[++i] = L.data[j];
}
}
L.len = i + 1;
return true;
}
//合并两个有序表
bool test_07(Sqlist &A, Sqlist &B, Sqlist &C) {
int i, j, k = 0;
while (i < A.len && j < B.len) {
if (A.data[i] <= B.data[j]) C.data[k++] = A.data[i++];
else C.data[k++] = B.data[j++];
}
while (i < A.len) C.data[k++] = A.data[i++];
while (j < B.len)C.data[k++] = B.data[j++];
C.len = k + 1;
return true;
}
void reverse(int A[], int left, int right) {
if (left >= right)return;
int mid = (right + left) / 2;
for (int i = 0; i < mid - left; ++i) {
int temp = A[i + left];
A[i + left] = A[right - i - 1];
A[right - i - 1] = temp;
}
}
//A【m+n】中存放两个线性表 将两个顺序表互换卫位置
void test_08(int A[], int m, int n) {
reverse(A, 0, n + m);
reverse(A, 0, n);
reverse(A, n, m + n);
}
//递增有序表 查找指定元素 找到则与后记元素互换 否则 插入指定元素
void test_09(int A[], int e, int &len) {
int low = 0, high = len - 1, mid = 0;
if (len + 1 > Maxlen)return;
while (low <= high) {
mid = (low + high) / 2;
if (A[mid] == e) break;
else if (A[mid] < e)low = mid + 1;
else high = mid - 1;
}
if (A[mid] == e && mid != len - 1) {
int temp = A[mid];
A[mid] = A[mid + 1];
A[mid + 1] = temp;
}
if (low > high) {
for (int i = len - 1; i > high; i--) {
A[i + 1] = A[i];
A[i] = e;
}
len = len + 1;
}
}
//数组元素左移动p个位置
void test_10(int A[], int p, int len) {
reverse(A, 0, len);
reverse(A, 0, len - p);
reverse(A, len - p, len);
}
int _11_a[5] = {11, 13, 15, 17, 19};
int _11_b[5] = {2, 4, 6, 8, 20};
//找出两个递增有序数组合并后的数组中位数
int test_11(int A[], int B[], int n) {
int s1 = 0, d1 = n - 1, m1;
int s2 = 0, d2 = n - 1, m2;
while (s1 != d1 || s2 != d2) {
m1 = (s1 + d1) / 2;
m2 = (s2 + d2) / 2;
if (A[m1] == B[m2])return A[m1];
if (A[m1] < B[m2]) {
if ((s1 + d1) % 2 == 0)//个数为奇数 从0开始 所有整除为奇数
{
s1 = m1;
d2 = m2;
} else {
s1 = m1 + 1;
d2 = m2;
}
} else {
if ((s1 + d1) % 2 == 0)//个数为奇数 从0开始 所有整除为奇数
{
d1 = m1;
s2 = m2;
} else {
d1 = m1;
s2 = m2 + 1;
}
}
}
return A[s1] < B[s2] ? A[s1] : B[s2];
}
int array_A[8] = {0, 5, 5, 3, 5, 7, 5, 5};
//找出数组中的主元素
int test_12(int A[], int n) {
int c, count = 1;
c = A[0];
for (int i = 0; i < n; ++i) {
if (A[i] == c) {
count++;
} else {
if (count > 0) {
count--;
} else {
c = A[i];
count = 1;
}
}
}
if (count > 0) {
for (int j = count = 0; j < n; ++j) {
if (A[j] == c) {
count++;
}
}
}
if (count > n / 2) return c;
else return -1;
}
int main() {
// std::cout << "-------------12众数!--------" << std::endl;
// printf("%d\n", test_12(array_A, 8));
// std::cout << "-----------11中位数!--------" << std::endl;
// test_11(_11_a, _11_b, 8);
// printf("%d\n",test_11(_11_a,_11_b,8));
// std::cout << "-----------4 1 5 3 8 删除大于3小于7的数--------" << std::endl;
// sq.data[0] = 4;
// sq.data[1] = 1;
// sq.data[2] = 5;
// sq.data[3] = 3;
// sq.data[4] = 8;
// sq.len = 5;
// printf("是否正确:%d\n", test_05(sq,3,7));
// std::cout << "剩余元素" << std::endl;
//test_02(sq); //元素逆置
// test_03(sq, 5);//删除元素5
sq.data[0] = 1;
sq.data[1] = 2;
sq.data[2] = 3;
sq.data[3] = 5;
sq.data[4] = 8;
sq.len = 5;
test_04(sq, 1, 5);
// test_05(sq,1,3);
// test_06(sq); //删除重复元素
for (int i = 0; i < sq.len; i++) {
printf("%d\n", sq.data[i]);
}
// test_08(stack, 3, 7);
//test_09(stack, 3, len);
//test_10(stack, 5, len);
// for (int i = 0; i < len; i++) {
// printf("%d", stack[i]);
// }
return 0;
}