|
|
@ -107,6 +107,57 @@ int main(){
|
|
|
|
}
|
|
|
|
}
|
|
|
|
~~~
|
|
|
|
~~~
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
### 03-02
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
~~~c
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
int num;
|
|
|
|
|
|
|
|
int maxcnt = 0;
|
|
|
|
|
|
|
|
void split(int a[], int low, int high, int& mid, int& left, int& right)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
mid = (low + high) / 2;
|
|
|
|
|
|
|
|
for (left = low; left <= high; left++)
|
|
|
|
|
|
|
|
if (a[left] == a[mid])
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
for (right = left + 1; right <= high; right++)
|
|
|
|
|
|
|
|
if (a[right] != a[mid])
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
right--;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void Getmaxcnt(int a[], int low, int high)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
if (low <= high)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
int mid, left, right;
|
|
|
|
|
|
|
|
split(a, low, high, mid, left, right);
|
|
|
|
|
|
|
|
int cnt = right - left + 1;
|
|
|
|
|
|
|
|
if (cnt > maxcnt)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
num = a[mid];
|
|
|
|
|
|
|
|
maxcnt = cnt;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
Getmaxcnt(a, low, left - 1);
|
|
|
|
|
|
|
|
Getmaxcnt(a, right + 1, high);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
int main()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
int a[] = {1,2,2,2,3,3,5,6,6,6,6};
|
|
|
|
|
|
|
|
int n = sizeof(a) / sizeof(a[0]);
|
|
|
|
|
|
|
|
printf("求解结果\n");
|
|
|
|
|
|
|
|
printf(" 递增序列: ");
|
|
|
|
|
|
|
|
for (int i = 0; i < n; i++)//输出待求众数序列中的元素;
|
|
|
|
|
|
|
|
printf("%d ",a[i]);
|
|
|
|
|
|
|
|
printf("\n");
|
|
|
|
|
|
|
|
Getmaxcnt(a, 0, n - 1);
|
|
|
|
|
|
|
|
printf(" 众数: %d, 重数: %d\n", num, maxcnt);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
~~~
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
### 04-01
|
|
|
|
### 04-01
|
|
|
|
|
|
|
|
|
|
|
|
~~~c
|
|
|
|
~~~c
|
|
|
@ -158,4 +209,99 @@ int main(){
|
|
|
|
return 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
~~~
|
|
|
|
~~~
|
|
|
|
|
|
|
|
### 04-02
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
~~~c
|
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
#include<stdlib.h>
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
typedef int ElemType;
|
|
|
|
|
|
|
|
typedef struct node
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
ElemType data;
|
|
|
|
|
|
|
|
struct node* lchild;
|
|
|
|
|
|
|
|
struct node* rchild;
|
|
|
|
|
|
|
|
} BTNode;
|
|
|
|
|
|
|
|
BTNode* CreateBTree(ElemType a[], ElemType b[], int n)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
int k;
|
|
|
|
|
|
|
|
if (n <= 0) return NULL;
|
|
|
|
|
|
|
|
ElemType root = a[0];
|
|
|
|
|
|
|
|
BTNode* bt = (BTNode*)malloc(sizeof(BTNode));
|
|
|
|
|
|
|
|
bt->data = root;
|
|
|
|
|
|
|
|
for (k = 0; k < n; k++)
|
|
|
|
|
|
|
|
if (b[k] == root)
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
bt->lchild = CreateBTree(a + 1, b, k);
|
|
|
|
|
|
|
|
bt->rchild = CreateBTree(a + k + 1, b + k + 1, n - k - 1);
|
|
|
|
|
|
|
|
return bt;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void DispBTree(BTNode* bt)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
if (bt != NULL)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
printf("%d", bt->data);
|
|
|
|
|
|
|
|
if (bt->lchild != NULL || bt->rchild != NULL)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
printf("(");
|
|
|
|
|
|
|
|
DispBTree(bt->lchild);
|
|
|
|
|
|
|
|
if (bt->rchild != NULL) printf(",");
|
|
|
|
|
|
|
|
DispBTree(bt->rchild);
|
|
|
|
|
|
|
|
printf(")");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void DestroyBTree(BTNode*& bt)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
if (bt != NULL)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
DestroyBTree(bt->lchild);
|
|
|
|
|
|
|
|
DestroyBTree(bt->rchild);
|
|
|
|
|
|
|
|
free(bt);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
int maxsum = 0;
|
|
|
|
|
|
|
|
vector<int> maxpath;
|
|
|
|
|
|
|
|
void Findmaxpath(BTNode* bt, vector <int > apath, int asum)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
if (bt == NULL)
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
apath.push_back(bt->data); asum += bt->data;
|
|
|
|
|
|
|
|
asum += bt->data;
|
|
|
|
|
|
|
|
if (bt->lchild == NULL && bt->rchild == NULL)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
if (asum - maxsum)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
maxsum = asum;
|
|
|
|
|
|
|
|
maxpath.clear();
|
|
|
|
|
|
|
|
maxpath = apath;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
Findmaxpath(bt->lchild, apath, asum);
|
|
|
|
|
|
|
|
Findmaxpath(bt->rchild, apath, asum);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
int main()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
BTNode* bt;
|
|
|
|
|
|
|
|
ElemType a[] = { 5,2,3,4,1,6 };
|
|
|
|
|
|
|
|
ElemType b[] = { 2,3,5,1,4,6 };
|
|
|
|
|
|
|
|
int n = sizeof(a)/sizeof(a[0]);
|
|
|
|
|
|
|
|
bt = CreateBTree(a,b,n);
|
|
|
|
|
|
|
|
printf("实验结果:\n");
|
|
|
|
|
|
|
|
printf("二叉树bt:");
|
|
|
|
|
|
|
|
DispBTree(bt);
|
|
|
|
|
|
|
|
printf("'in");
|
|
|
|
|
|
|
|
printf("最大路径");
|
|
|
|
|
|
|
|
vector<int> apath;
|
|
|
|
|
|
|
|
int asum = 0;
|
|
|
|
|
|
|
|
Findmaxpath(bt,apath,asum);
|
|
|
|
|
|
|
|
printf("路径和: % d,路径 : ",maxsum);
|
|
|
|
|
|
|
|
for (int i=0;i < maxpath.size();i++)
|
|
|
|
|
|
|
|
printf("%d ",maxpath[i]);
|
|
|
|
|
|
|
|
printf("\n");
|
|
|
|
|
|
|
|
printf("销毁树bt\n");
|
|
|
|
|
|
|
|
DestroyBTree(bt);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
~~~
|
|
|
|
|
|
|
|
|
|
|
|