|
|
|
@ -1,4 +1,4 @@
|
|
|
|
|
二叉树
|
|
|
|
|
################################
|
|
|
|
|
#include <vector>
|
|
|
|
|
#include <string>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
@ -96,4 +96,118 @@ int main()
|
|
|
|
|
printf("\n");
|
|
|
|
|
printf("销毁树bt\n");
|
|
|
|
|
DestroyBTree(bt);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
##############################
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
//求解结果表示
|
|
|
|
|
int num; //全局变量,存放众数
|
|
|
|
|
int maxcnt = 0; //全局变量,存放重数
|
|
|
|
|
void split(int a[], int low, int high, int& mid, int& left, int& right)
|
|
|
|
|
//以a[low..high]中间的元素为界限,确定为等于a[mid]元素的左、右位置left和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) //a[low..high]序列至少有1个元素
|
|
|
|
|
{
|
|
|
|
|
int mid, left, right;
|
|
|
|
|
split(a, low, high, mid, left, right);
|
|
|
|
|
int cnt = right - left + 1; //求出a[mid]元素的重数
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
####################################
|
|
|
|
|
#include <iostream>
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
|
|
const int N=1010;
|
|
|
|
|
int v[N],w[N],f[N];
|
|
|
|
|
int n,m;
|
|
|
|
|
|
|
|
|
|
int main()
|
|
|
|
|
{
|
|
|
|
|
scanf("%d%d",&n,&m);
|
|
|
|
|
|
|
|
|
|
for(int i=1;i<=n;i++) scanf("%d%d",&v[i],&w[i]);
|
|
|
|
|
|
|
|
|
|
for(int i=1;i<=n;i++)
|
|
|
|
|
for(int j=m;j>=v[i];j--)
|
|
|
|
|
{
|
|
|
|
|
f[j]=max(f[j],f[j-v[i]]+w[i]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
printf("%d",f[m]);
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
######################################
|
|
|
|
|
#include<stdio.h>
|
|
|
|
|
int Fibon1(int n)
|
|
|
|
|
{
|
|
|
|
|
if (n == 1 || n == 2)
|
|
|
|
|
{
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return Fibon1(n - 1) + Fibon1(n - 2);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
int main()
|
|
|
|
|
{
|
|
|
|
|
int n = 0;
|
|
|
|
|
int ret = 0;
|
|
|
|
|
scanf("%d", &n);
|
|
|
|
|
ret = Fibon1(n);
|
|
|
|
|
printf("ret=%d", ret);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int Fibno2(int n)
|
|
|
|
|
{
|
|
|
|
|
int num1 = 1;
|
|
|
|
|
int num2 = 1;
|
|
|
|
|
int tmp = 0;
|
|
|
|
|
int i = 0;
|
|
|
|
|
if (n < 3)
|
|
|
|
|
{
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
for (i = 0; i>n-3; i++)
|
|
|
|
|
{
|
|
|
|
|
tmp = num1 + num2;
|
|
|
|
|
num1 = num2;
|
|
|
|
|
num2 = tmp;
|
|
|
|
|
}
|
|
|
|
|
return tmp;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|