commit 22c59de0c3d2521cc7b3d67f4c7b96df31375e2b Author: callmePicacho <18283847632~> Date: Wed May 8 19:49:54 2019 +0800 Initial commit diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..dfe0770 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,2 @@ +# Auto detect text files and perform LF normalization +* text=auto diff --git a/README.md b/README.md new file mode 100644 index 0000000..9ad765b --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +# Data-Structres diff --git a/上课Demo/1.计时实例.c b/上课Demo/1.计时实例.c new file mode 100644 index 0000000..db21e08 --- /dev/null +++ b/上课Demo/1.计时实例.c @@ -0,0 +1,46 @@ +#include +#include +#include +clock_t start,end; +/* clock_t clock()صı*/ +double duration; +/* ⺯ʱ䣬Ϊλ*/ +#define MAXN 101 /* ʽʽ+1*/ +#define MAXK 1e5 /* ⺯ظô*/ +double f1(int n,double a[],double x); +double f2(int n,double a[],double x); +void run(double(*f)(int n,double *,double ),double a[],int func_n); +int main(){ + int i; + double a[MAXN]; /* 洢ʽϵ */ + a[0]=1; + for(i=1;i0;i--) + p=a[i-1]+x*p; + return p; +} +void run(double(*f)(int n,double *,double ),double a[],int func_n){ + int i; + start = clock(); + for(i=0;i +#include +#define MaxData 100000 +#define ERROR -1 +typedef int ElementType; +typedef struct HeapStruct *MaxHeap; +struct HeapStruct{ + ElementType *Elements; // 洢Ԫص + int Size; // ѵĵǰԪظ + int Capacity; // ѵ +}; + +MaxHeap Create(int MaxSize); // +bool IsFull(MaxHeap H); // ж϶Ƿ +bool Insert(MaxHeap H,ElementType item); // Ԫ +bool IsEmpty(MaxHeap H); // ж϶ǷΪ +ElementType DeleteMax(MaxHeap H); // ɾضԪ +void LevelOrderTraversal(MaxHeap H); // + +// +MaxHeap Create(int MaxSize){ + MaxHeap H = (MaxHeap)malloc(sizeof(struct HeapStruct)); + // Elements[0] ΪڱԪش Elements[1] ʼ + H->Elements = (ElementType *)malloc((MaxSize+1) * sizeof(ElementType)); + H->Size = 0; + H->Capacity = MaxSize; + // "ڱ"ڶпֵܵ + H->Elements[0] = MaxData; + return H; +} + +// 룬ȫһλò +bool Insert(MaxHeap H,ElementType item){ + if(IsFull(H)){ + printf("޷룡\n"); + return false; + } + int i = ++H->Size; // ָһλ + for(;H->Elements[i/2] < item;i/=2) // ұ item Ľ + H->Elements[i] = H->Elements[i/2]; // ¸ֵ + H->Elements[i] = item; // ҵˣ item ֵŽȥ + return true; +} + +// ɾӸɾ +ElementType DeleteMax(MaxHeap H){ + int parent,child; + ElementType Max,tmp; + if(IsEmpty(H)){ + printf("Ϊգ޷ɾ\n"); + return ERROR; + } + Max = H->Elements[1]; // õֵ + tmp = H->Elements[H->Size--]; // õȫһֵ + // бparent Ƿӽ + for(parent=1;parent*2<=H->Size;parent=child){ + // Һӽҽϴֵ + child = 2 * parent; // ӽ + // child!=H->Size ʾ child Ϊǰһ㣬 parent Һӽ + if((child!=H->Size) &&(H->Elements[child] < H->Elements[child+1])) + child++; + // tmp Ҹʵλ + // ǰҺӽ tmp С˵ tmp λѾ + if(H->Elements[child] <= tmp) + break; + else // ѽϴĺӽԼȥ + H->Elements[parent] = H->Elements[child]; + } + H->Elements[parent] = tmp; // ںʵλð tmp Žȥ + return Max; +} + +// жǷѾ +bool IsFull(MaxHeap H){ + return (H->Size == H->Capacity); +} + +// жǷΪ +bool IsEmpty(MaxHeap H){ + return !H->Size; +} + +// +void LevelOrderTraversal(MaxHeap H){ + int i; + printf("Ľǣ"); + for(i = 1;i<=H->Size;i++){ + printf("%d ",H->Elements[i]); + } + printf("\n"); +} + +int main(){ + MaxHeap H; + int MaxSize = 100; + H = Create(MaxSize); + Insert(H,55); + Insert(H,66); + Insert(H,44); + Insert(H,33); + Insert(H,11); + Insert(H,22); + Insert(H,88); + Insert(H,99); + /* + 99 + / \ + 88 66 + / \ / \ + 55 11 22 44 + / + 33 + */ + LevelOrderTraversal(H); + DeleteMax(H); + LevelOrderTraversal(H); + DeleteMax(H); + LevelOrderTraversal(H); + DeleteMax(H); + LevelOrderTraversal(H); + DeleteMax(H); + LevelOrderTraversal(H); + return 0; +} + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/上课Demo/10.堆的操作.exe b/上课Demo/10.堆的操作.exe new file mode 100644 index 0000000..a702d68 Binary files /dev/null and b/上课Demo/10.堆的操作.exe differ diff --git a/上课Demo/11.堆的两种建立方式.cpp b/上课Demo/11.堆的两种建立方式.cpp new file mode 100644 index 0000000..059a704 --- /dev/null +++ b/上课Demo/11.堆的两种建立方式.cpp @@ -0,0 +1,120 @@ +#include +#include +const int MinData = -100000; // ڱֵ +const int MaxSize = 1005; // +using namespace std; +typedef struct HeapStruct *Heap; +struct HeapStruct{ + int *data; // ֵ + int size; // ǰԪظ + int capacity; // +}; + +// ʼ +Heap Create(){ + Heap H; + H = (Heap)malloc(sizeof(struct HeapStruct)); + H->data = (int *)malloc(sizeof(int) * (MaxSize+1)); + H->size = 0; + H->capacity = MaxSize; + H->data[0] = MinData; + return H; +} + +// ƶѵ"ɾ" +void sort(Heap H,int i){ + int child,parent; + int tmp = H->data[i]; // õǰ"ֵ" + for(parent = i;parent*2<=H->size;parent = child){ + child = 2 * parent; + if((child!=H->size) && (H->data[child+1] < H->data[child])) + child++; + if(H->data[child] >= tmp) + break; + else + H->data[parent] = H->data[child]; + } + H->data[parent] = tmp; +} +// +void adjust(Heap H){ + int i= H->size/2; + for(;i>0;i--){ + // ÿкӽĽΪ㣬ж + sort(H,i); + } +} + +// +void bl(Heap H){ + for(int i=1;i<=H->size;i++){ + cout<data[i]<<" "; + } + cout<>n; + for(int i=0;i>H->data[++H->size]; + adjust(H); + bl(H); + return 0; +} + +/* +#include +#include +const int MinData = -100000; // ڱֵ +const int MaxSize = 1005; // +using namespace std; +typedef struct HeapStruct *Heap; +struct HeapStruct{ + int *data; // ֵ + int size; // ǰԪظ + int capacity; // +}; + +// ʼ +Heap Create(){ + Heap H; + H = (Heap)malloc(sizeof(struct HeapStruct)); + H->data = (int *)malloc(sizeof(int) * (MaxSize+1)); + H->size = 0; + H->capacity = MaxSize; + H->data[0] = MinData; + return H; +} + +// +void Insert(Heap H,int x){ + int i = ++H->size; // ָһ + for(;H->data[i/2]>x;i/=2) + H->data[i] = H->data[i/2]; + H->data[i] = x; +} + +// +void bl(Heap H){ + for(int i=1;i<=H->size;i++) + cout<data[i]; +} + +int main(){ + Heap H; + H = Create(); + int n; + cin>>n; + for(int i=0;i>t; + Insert(H,t); + } + bl(H); + return 0; +} +*/ diff --git a/上课Demo/11.堆的两种建立方式.exe b/上课Demo/11.堆的两种建立方式.exe new file mode 100644 index 0000000..929d526 Binary files /dev/null and b/上课Demo/11.堆的两种建立方式.exe differ diff --git a/上课Demo/12.哈夫曼树.cpp b/上课Demo/12.哈夫曼树.cpp new file mode 100644 index 0000000..381ceb5 --- /dev/null +++ b/上课Demo/12.哈夫曼树.cpp @@ -0,0 +1,155 @@ +#include +#include +#define MaxSize 1000 +#define MinData -1000 +int A[] = {13,1,45,7,20,4,19,13,40,33,38}; // ԤȶһȨֵ +int A_length = 11; // 䳤 +typedef struct HeapStruct *MinHeap; +typedef struct TreeNode *HuffmanTree; +struct HeapStruct{ // ŹĶ + HuffmanTree *data; // ֵ + int size; // ѵĵǰС + int capacity; // +}; +struct TreeNode{ // + int weight; //Ȩֵ + HuffmanTree Left; // + HuffmanTree right; // +}; +using namespace std; + +MinHeap create(); // ʼ +HuffmanTree Create(); // ʼ +void sort(MinHeap H,int i); // С +void adjust(MinHeap H); // С +void BuildMinHeap(MinHeap H); // +HuffmanTree Delete(MinHeap H); // ɾСԪ +void Insert(MinHeap H,HuffmanTree Huff); // СԪ +void PreOrderTraversal(HuffmanTree Huff); // +HuffmanTree Huffman(MinHeap H); // Ĺ + +// ʼ +MinHeap create(){ + MinHeap H; + HuffmanTree Huff; + H = (MinHeap)malloc(sizeof(struct HeapStruct)); + H->data = (HuffmanTree *)malloc(sizeof(struct TreeNode) * (MaxSize+1)); + H->capacity = MaxSize; + H->size = 0; + // ڱ + Huff = Create(); + Huff->weight = MinData; + H->data[0] = Huff; + return H; +} + +// ʼ +HuffmanTree Create(){ + HuffmanTree Huff; + Huff = (HuffmanTree)malloc(sizeof(struct TreeNode)); + Huff->weight = 0; + Huff->Left = NULL; + Huff->right = NULL; + return Huff; +} + +// С +void sort(MinHeap H,int i){ + int parent,child; + int tmp = H->data[i]->weight; // ȡǰ""ֵ + for(parent=i;parent*2<=H->size;parent = child){ + child = 2 * parent; + if((child!=H->size) && (H->data[child+1]->weight < H->data[child]->weight)) + child++; + if(H->data[child]->weight >= tmp) + break; + else + H->data[parent] = H->data[child]; + } + H->data[parent]->weight = tmp; +} + +// С +void adjust(MinHeap H){ + for(int i =H->size/2;i>0;i--) + sort(H,i);// ÿ"С" +} + +// +void BuildMinHeap(MinHeap H){ + // Ȩֵ + HuffmanTree Huff; + for(int i=0;iweight = A[i]; + H->data[++H->size] = Huff; + } + // + adjust(H); +} + + +// ɾСԪ +HuffmanTree Delete(MinHeap H){ + int parent,child; + HuffmanTree T = H->data[1]; // ȡĹ + HuffmanTree tmp = H->data[H->size--]; // ȡһȨֵ + for(parent=1;parent*2<=H->size;parent = child){ + child = 2 * parent; + if((child!=H->size) && (H->data[child+1]->weight < H->data[child]->weight)) + child++; + if(H->data[child]->weight >= tmp->weight) + break; + else + H->data[parent] = H->data[child]; + } + H->data[parent] = tmp; + // һ HuffmanTree 㣬ϸղȡȨֵظý + return T; +} + +// һ +void Insert(MinHeap H,HuffmanTree Huff){ + int weight = Huff->weight; // ȡȨֵ + int i = ++H->size; + for(;H->data[i/2]->weight > weight;i/=2) + H->data[i] = H->data[i/2]; + H->data[i] = Huff; +} + +// +void PreOrderTraversal(HuffmanTree Huff){ + if(Huff){ + cout<weight<<" "; + PreOrderTraversal(Huff->Left); + PreOrderTraversal(Huff->right); + } +} + +// Ĺ +HuffmanTree Huffman(MinHeap H){ + HuffmanTree T; + BuildMinHeap(H); // + int times = H->size; + // times-1 κϲ + for(int i=1;iLeft = Delete(H); // Ӷɾһ㣬Ϊ T ӽ + T->right = Delete(H); // Ӷɾһ㣬Ϊ T ӽ + T->weight = T->Left->weight + T->right->weight; // ¼Ȩֵ + Insert(H,T); // ټӽ + } + T = Delete(H); + return T; +} + + + +int main(){ + MinHeap H; + HuffmanTree Huff; + H = create(); + Huff = Huffman(H); + PreOrderTraversal(Huff); + return 0; +} diff --git a/上课Demo/12.哈夫曼树.exe b/上课Demo/12.哈夫曼树.exe new file mode 100644 index 0000000..77d5f73 Binary files /dev/null and b/上课Demo/12.哈夫曼树.exe differ diff --git a/上课Demo/13.并查集的表示.cpp b/上课Demo/13.并查集的表示.cpp new file mode 100644 index 0000000..7a93bb8 --- /dev/null +++ b/上课Demo/13.并查集的表示.cpp @@ -0,0 +1,50 @@ +#include +#include +#define MaxSize 1000 +typedef int ElementType; +typedef struct{ + ElementType Data; // ֵ + int parent; // ָ򸸽 +}SetType; +using namespace std; + +// +int Find(SetType s[],ElementType x){ + int i; + // ҵиֵӦ± + for(i=0;i= 0;i = s[i].parent); + return i; // ظ s е± +} + +// +void Union(SetType s[],ElementType x1,ElementType x2){ + int root1 = Find(s,x1); // ҵ x1 ĸ± + int root2 = Find(s,x2); // ҵ x2 ĸ± + // ±겻ͬ˵һ + if(root1 != root2){ + s[root1].parent = root2; // x1 ҵ x2 ļ + } +} + +int main(){ + SetType s[MaxSize]; + // ʼ飬ȫָ -1 + for(int i=0;i +#include +#include +#include +#define NumSize 20 +int A[NumSize]; +using namespace std; + +// +void Random(){ + for(int i=0;i=0;p--){ // ܹ n-1 + bool flag = false; + for(int j=0;j 0;j--) // ҵʺϵλ + A[j] = A[j-1]; // ڡλ + A[j] = tmp; // ѺʴС + } +} + +// ԭʼϣ +void shell_sort(int N){ + for(int D=N/2;D>0;D/=2){ + for(int p=D;p=D && tmp=D ǰΪҲ A[j-D]ѾԽ + A[j] = A[j-D]; + A[j] = tmp; + } + } +} + +// Hibbardϣ +void Hibbard_shell_sort(int N){ + int add[]={32767,16383,8191,4095,2047,1023,511,255,127,63,31,15,7,3,1,0}; + int i=0; + for(int D=add[i];D>0;D=add[++i]){ + for(int p=D;p=D && tmp=D ǰΪҲ A[j-D]ѾԽ + A[k] = A[k-D]; + A[k] = tmp; + } + } +} + +// Sedgewickϣ +void Sedgewick_shell_sort(int N){ + int add[]= {587521,260609,146305,64769,36289,16001,8929,3905,2161,929,505,209,109,41,19,5,1,0}; + int i=0; + for(int D=add[i];D>0;D=add[++i]){ + for(int p=D;p=D && tmp=0;i--) + PrecDown(i,N); + for(int i=N-1;i>0;i--){ + swap(A[0],A[i]); + PrecDown(0,i); + } +} +/*************************************************/ + + +/********************鲢򣨵ݹ飩ʼ******************************/ +// 鲢ʵ +void Merge(int A[],int tmpA[],int L,int R,int RightEnd){ + // L = ʼλãR = ұʼλãRightEnd = ұյλ + int LeftEnd = R-1; // յλ + int tmp = L; // ŽĿʼλ + int NumElements = RightEnd - L + 1; // 鲢 + while(L<=LeftEnd && R<=RightEnd){ + if(A[L] <= A[R]) + tmpA[tmp++] = A[L++]; + else + tmpA[tmp++] = A[R++]; + } + // ʣ + while(L<=LeftEnd) + tmpA[tmp++] = A[L++]; + // ұʣ + while(R<=RightEnd) + tmpA[tmp++] = A[R++]; + // ؽ + for(int i=0;i Pivot); + if(i < j) + swap(A[i],A[j]); + else + break; + } + swap(A[i],A[Right-1]); + Quicksort(Left,i-1); + Quicksort(i+1,Right); + }else + Insertion_sort(A+Left,Right-Left+1); +} + +void Quick_sort(int N){ + Quicksort(0,N-1); +} +/*********************************************/ + +/*********************Ͱʼ********************/ +void Bucket_Sort(int N){ + int count[1000]; + // ΧͰͶ + for(int i=0;i<1000;i++){ + count[i] = 0; + } + // ÿֵͰ + for(int i=0;i +#include +#define MaxVertexNum 100 +typedef int weightType; +typedef int Vertex; +typedef int DataType; +typedef struct GNode *ptrToGNode; +struct GNode{ // ͼ + int Nv; // + int Ne; // + weightType G[MaxVertexNum][MaxVertexNum]; + DataType Data[MaxVertexNum]; // 涥 +}; +typedef ptrToGNode MGraph; +typedef struct ENode *ptrToENode; +struct ENode{ // + Vertex V1,V2; // + weightType Weight; // Ȩ +}; +typedef ptrToENode Edge; + +// ʼͼ +MGraph Create(int VertexNum){ + Vertex v,w; + MGraph Graph; + + Graph = (MGraph)malloc(sizeof(struct GNode)); + Graph->Nv = VertexNum; + Graph->Ne = 0; + + for(v=0;vG[v][w] = 0; + return Graph; +} + +// +MGraph Insert(MGraph Graph,Edge E){ + + // + Graph->G[E->V1][E->V2] = E->Weight; + + // ͼҪ + Graph->G[E->V2][E->V1] = E->Weight; + +} + +// ͼ +MGraph BuildGraph(){ + MGraph Graph; + Edge E; + Vertex V; + int Nv,i; + scanf("%d",&Nv); // 붥 + Graph = Create(Nv); + scanf("%d",&(Graph->Ne)); // + if(Graph->Ne != 0){ + E = (Edge)malloc(sizeof(struct ENode)); + for(i=0;iNe;i++){ + scanf("%d %d %d",&E->V1,&E->V2,&E->Weight); // ÿߵ + Insert(Graph,E); + } + } + return Graph; +} +*/ + +#include +#include +#define MAXN 100 +int G[MAXN][MAXN],Nv,Ne; + +void BuildGraph(){ + int i,j,v1,v2,w; + + scanf("%d",&Nv); + // ʼͼ + for(i=0;i +#include +#define MaxVertexNum 100 +typedef int Vertex; +typedef int DataType; +typedef int weightType; + +typedef struct ENode *ptrToENode; +struct ENode{ // + Vertex V1,V2; // + weightType Weight; // Ȩ +}; +typedef ptrToENode Edge; + +typedef struct AdjVNode *ptrToAdjVNode; +struct AdjVNode{ // ڽӱԪ + Vertex AdjV; // ڽӵ± + weightType Weight; // Ȩֵ + ptrToAdjVNode Next; // һ +}; + +typedef struct VNode{ // ڽӱͷ + ptrToAdjVNode FirstEdge; // ÿָ + DataType Data; // +}AdjList[MaxVertexNum]; + +typedef struct GNode *ptrToGNode; +struct GNode{ // ͼ + int Nv; // + int Ne; // + AdjList G; // ڽӱ +}; +typedef ptrToGNode LGraph; + +// ʼ +LGraph create(int VertexNum){ + Vertex v,w; + LGraph Graph; + + Graph = (LGraph)malloc(sizeof(struct GNode)); + Graph->Nv = VertexNum; // ʼ + Graph->Ne = 0; // ʼ + + // ÿߵ FirstEdge ָ NULL + for(v=0;vNv;v++) + Graph->G[v].FirstEdge = NULL; + return Graph; +} + + +// һߵڽӱĶָ֮ +void InsertEdge(LGraph Graph,Edge E){ + ptrToAdjVNode newNode; + + // + // Ϊ V2 µĽ + newNode = (ptrToAdjVNode)malloc(sizeof(struct AdjVNode)); + newNode->AdjV = E->V2; + newNode->Weight = E->Weight; + + // V2 뵽ڽӱͷ + newNode->Next = Graph->G[E->V1].FirstEdge; + Graph->G[E->V1].FirstEdge = newNode; + + // Ϊͼ + newNode = (ptrToAdjVNode)malloc(sizeof(struct AdjVNode)); + newNode->AdjV = E->V1; + newNode->Weight = E->Weight; + + newNode->Next = Graph->G[E->V2].FirstEdge; + Graph->G[E->V2].FirstEdge = newNode; +} + +// ͼ +LGraph BuildGraph(){ + LGraph Graph; + Edge E; + Vertex V; + int Nv,i; + scanf("%d",&Nv); + Graph = create(Nv); + scanf("%d",&(Graph->Ne)); + if(Graph->Ne != 0){ + for(i=0;iNe;i++){ + E = (Edge)malloc(sizeof(struct ENode)); + scanf("%d %d %d",&E->V1,&E->V2,&E->Weight); + InsertEdge(Graph,E); + } + } + return Graph; +} + +// ӡ +void print(LGraph Graph){ + Vertex v; + ptrToAdjVNode tmp; + for(v=0;vNv;v++){ + tmp = Graph->G[v].FirstEdge; + printf("%d ",v); + while(tmp){ + printf("%d ",tmp->AdjV); + tmp = tmp->Next; + } + printf("\n"); + } +} + +int main(){ + LGraph Graph; + Graph = BuildGraph(); + print(Graph); + return 0; +}*/ + +#include +#include +#define MaxVertexNum 100 +typedef struct AdjVNode *AdjList; +struct AdjVNode{ + int weight; // Ȩֵ + int adjv; // ± + AdjList next; // һ +}; +AdjList Graph[MaxVertexNum]; +int Ne,Nv; + +// ͼ +void BuildGraph(){ + int i; + int v1,v2,w; + AdjList NewNode; + scanf("%d",&Nv); + for(i=0;iadjv = i; + Graph[i]->next = NULL; + } + scanf("%d",&Ne); + for(i=0;iadjv = v1; + NewNode->weight = w; + + NewNode->next = Graph[v2]->next; + Graph[v2]->next = NewNode; + + NewNode = (AdjList)malloc(sizeof(struct AdjVNode)); + NewNode->adjv = v2; + NewNode->weight = w; + + NewNode->next = Graph[v1]->next; + Graph[v1]->next = NewNode; + } +} + +void print(){ + AdjList tmp; + int i; + for(i=0;iadjv); + tmp = tmp->next; + } + printf("\n"); + } +} + +int main(){ + + BuildGraph(); + print(); + return 0; +} + diff --git a/上课Demo/16.图的邻接表实现.exe b/上课Demo/16.图的邻接表实现.exe new file mode 100644 index 0000000..bcc3bea Binary files /dev/null and b/上课Demo/16.图的邻接表实现.exe differ diff --git a/上课Demo/17.图的迷宫遍历.cpp b/上课Demo/17.图的迷宫遍历.cpp new file mode 100644 index 0000000..e20e446 --- /dev/null +++ b/上课Demo/17.图的迷宫遍历.cpp @@ -0,0 +1,97 @@ +#include +#include +#include +#define col 12 +#define row 10 +int Graph[row][col]; // ͼ +bool visit[row][col]; // ״̬ +int times[row][col]; // ʴ +typedef struct Node *coordinate; +struct Node{ // + int hor; // + int ver; // +}; +using namespace std; + +// ʼͼ +void Init(){ + int tmp[row][col]= {{1,1,1,0,1,1,1,0,1,1,1,0}, + {1,0,1,1,0,0,1,0,1,0,0,0}, + {1,0,0,0,1,0,0,0,1,0,1,1}, + {1,0,1,1,1,1,1,1,1,0,0,1}, + {0,0,1,0,0,1,0,1,1,1,1,0}, + {1,1,1,0,1,1,0,0,0,1,1,1}, + {1,1,0,1,1,1,1,0,0,1,0,0}, + {0,0,0,1,0,0,1,0,1,0,1,1}, + {0,1,1,0,1,1,1,0,1,0,1,0}, + {0,0,0,0,0,1,1,0,1,0,1,1}}; + for(int i=0;ihor = i; + C->ver = j; + return C; +} + +int min(int t1,int t2){ + return t1 q; + coordinate tmp; + // + tmp = create(0,0); + visit[0][0] = false; + times[0][0] = 0; + q.push(tmp); + while(!q.empty()){ + // ȡе±Ͳ + coordinate nowNode = q.front(); + int xx = nowNode->hor; + int yy = nowNode->ver; + int nowtimes = times[xx][yy]; + q.pop(); // + // õΧ˸ + for(int i=0;i<8;i++){ + int newx = xx + x[i]; + int newy = yy + y[i]; + // ڷΧ + if((newx>=0 && newx=0 && newy +#include +#include +#include +#include +#define Init -1 +#define MaxVertex 100 +int path[MaxVertex]; // 洢· +int dist[MaxVertex]; // 洢· +int G[MaxVertex][MaxVertex]; // ͼ +int Ne; // +int Nv; // +typedef int Vertex; +using namespace std; + + +void build(){ + int v1,v2; + // ʼ + cin>>Nv; + for(int i=1;i<=Nv;i++) + for(int j=1;j<=Nv;j++) + G[i][j] = 0; + // ʼ· + for(int i=1;i<=Nv;i++) + path[i] = Init; + // ʼ· + for(int i=1;i<=Nv;i++) + dist[i] = Init; + // ʼ + cin>>Ne; + for(int i=0;i>v1>>v2; + G[v1][v2] = 1; // ͼ + } +} + +void Unweighted(Vertex v){ + queue q; + dist[v] = 0; // Լľ 0 + Vertex w; + q.push(v); + while(!q.empty()){ + w = q.front(); + q.pop(); + for(int i=1;i<=Nv;i++) + // ûʹͨ + if(dist[i]==Init && G[w][i]){ + dist[i] = dist[w]+1; // һľ + 1 + path[i] = w; // w һҪ·һ· + q.push(i); + } + } +} + +// ȡ· +void getTail(Vertex v){ + for(int i=1;i<=Nv;i++){ + if(i==v) + continue; + stack s; + cout< +#include +#define Inf 1000000 +#define Init -1 +#define MaxVertex 100 +typedef int Vertex; +int G[MaxVertex][MaxVertex]; +int dist[MaxVertex]; // +int path[MaxVertex]; // · +int collected[MaxVertex]; // ¼ +int Nv; // +int Ne; // +using namespace std; + +// ʼͼϢ +void build(){ + Vertex v1,v2; + int w; + cin>>Nv; + // ʼͼ + for(int i=1;i<=Nv;i++) + for(int j=1;j<=Nv;j++) + G[i][j] = 0; + // ʼ· + for(int i=1;i<=Nv;i++) + path[i] = Init; + // ʼ + for(int i=0;i<=Nv;i++) + dist[i] = Inf; + // ʼ¼ + for(int i=1;i<=Nv;i++) + collected[i] = false; + cin>>Ne; + // ʼ + for(int i=0;i>v1>>v2>>w; + G[v1][v2] = w; // ͼ + } +} + +// ʼ·Ϣ +void crate(Vertex s){ + dist[s] = 0; + collected[s] = true; + for(int i=1;i<=Nv;i++) + if(G[s][i]){ + dist[i] = G[s][i]; + path[i] = s; + } +} + +// δ¼distС +Vertex FindMin(Vertex s){ + int min = 0; // ֮ǰصذ dist[0] ʼΪ + for(Vertex i=1;i<=Nv;i++) + if(i != s && dist[i] < dist[min] && !collected[i]) + min = i; + return min; +} + + +void Dijkstra(Vertex s){ + crate(s); + while(true){ + Vertex V = FindMin(s); // ҵ + if(!V) + break; + collected[V] = true; //¼ + for(Vertex W=1;W<=Nv;W++) + if(!collected[W] && G[V][W]){ // δ¼ + if(dist[V] + G[V][W] < dist[W]){ + dist[W] = G[V][W] + dist[V]; + path[W] = V; + } + } + } +} + +void output(){ + for(int i=1;i<=Nv;i++) + cout< +#include +#define MAXSIZE 100 // MAXSIZE Ϊ Data ĴС +typedef int ElementType; // ElementType ɶΪ +typedef struct LNode *List; +struct LNode{ + ElementType Data[MAXSIZE]; + int Last; // Last ԱһԪ +}; +List L; +//±Ϊ i ԪأL->Data[i] +//ԱijȣL->Last+1 + +List MakeEmpty(); //ʼ˳ +int Find(ElementType X,List L); // X һγֵ± +void Insert(ElementType X,int i,List L); //±Ϊ i ĵط X +void Delete(int i,List L); //ɾ±Ϊ i ĵǰֵ +ElementType FindKth(int K,List L); //±Ϊ K ĵǰֵ +int Length(List L); //˳ij + +//ʼ +List MakeEmpty(){ + List L; + L = (List)malloc(sizeof(struct LNode)); + L->Last = -1; + return L; +} + +// ֵ +int Find(ElementType X,List L){ + int i=0; + while(i <= L->Last && L->Data[i] != X) + i++; + if(L->Last < i) //ûҵ -1 + return -1; + else // ҵ󷵻± + return i; +} + +// +void Insert(ElementType X,int i,List L){ + int j; + if(L->Last == MAXSIZE-1){ //λ + printf(""); + return; + } + if(i < 0 || L->Last+1 < i){ //λԽ磬 L->Data[L->Last+1]涼λ + printf("λòϷ"); + return; + } + for(j=L->Last;j>=i;j--) // ӺǰŲһ a[i]ڳλ + L->Data[j+1] = L->Data[j]; + L->Data[i] = X; //Ԫز + L->Last++; // LastȻָԪ + return; +} + +//ɾ +void Delete(int i,List L){ + int j; + if(i < 0 || L->Last Data[L->Last] + printf("L->Data[%d]Ԫ",i); + return; + } + for(j=i;j<=L->Last;j++) // ǰǰŲһ a[i] + L->Data[j-1] = L->Data[j]; + L->Last--; // LastȻָԪ + return; +} + +// +ElementType FindKth(int K,List L){ + if(K < 0 || L->Last < K){ //λԽ + printf("L->Data[%d]Ԫ",K); + return; + } + return L->Data[K]; +} + +// +int Length(List L){ + return L->Last+1; +} + +int main(){ + int i=0; + L = MakeEmpty(); + Insert(11,0,L); + printf("ԱL-Data[0]11\n"); + Insert(25,0,L); + printf("ԱL-Data[0]25\n"); + Insert(33,0,L); + printf("ԱL-Data[0]33\n"); + Insert(77,0,L); + printf("ԱL-Data[0]77\n"); + printf("ʱԱΪ"); + for(i=0;iData[i]); + printf("\n"); + printf("ֵΪ12±ǣ%d\n",Find(12,L)); + printf("±Ϊ3Աֵǣ%d\n",FindKth(3,L)); + Delete(2,L); + printf("ɾԱ±Ϊ2Ԫ\n"); + Delete(2,L); + printf("ɾԱ±Ϊ2Ԫ\n"); + printf("ʱԱΪ"); + for(i=0;iData[i]); + printf("\n"); + return 0; +} diff --git a/上课Demo/2.数组存储的线性表.exe b/上课Demo/2.数组存储的线性表.exe new file mode 100644 index 0000000..36be838 Binary files /dev/null and b/上课Demo/2.数组存储的线性表.exe differ diff --git a/上课Demo/20.Floyd.cpp b/上课Demo/20.Floyd.cpp new file mode 100644 index 0000000..f7bc25f --- /dev/null +++ b/上课Demo/20.Floyd.cpp @@ -0,0 +1,66 @@ +#include +#include +#define INF 1000000 +#define MaxVertex 100 +typedef int Vertex; +int G[MaxVertex][MaxVertex]; +int dist[MaxVertex][MaxVertex]; // +int path[MaxVertex][MaxVertex]; // · +int Nv; // +int Ne; // +using namespace std; + +// ʼͼϢ +void build(){ + Vertex v1,v2; + int w; + cin>>Nv; + // ʼͼ + for(int i=1;i<=Nv;i++) + for(int j=1;j<=Nv;j++) + G[i][j] = INF; + cin>>Ne; + // ʼ + for(int i=0;i>v1>>v2>>w; + G[v1][v2] = w; + G[v2][v1] = w; + } +} + +void Floyd(){ + for(Vertex i=1;i<=Nv;i++) + for(Vertex j=1;j<=Nv;j++){ + dist[i][j] = G[i][j]; + path[i][j] = -1; + } + for(Vertex k=1;k<=Nv;k++) + for(Vertex i=1;i<=Nv;i++) + for(Vertex j=1;j<=Nv;j++) + if(dist[i][k] + dist[k][j] < dist[i][j]){ + dist[i][j] = dist[i][k] + dist[k][j]; + path[i][j] = k; + } +} + +void output(){ + for(Vertex i=1;i<=Nv;i++){ + for(Vertex j=1;j<=Nv;j++) + cout< +#include +#define INF 100000 +#define MaxVertex 105 +typedef int Vertex; +int G[MaxVertex][MaxVertex]; +int parent[MaxVertex]; // 鼯 +int dist[MaxVertex]; // +int Nv; // +int Ne; // +int sum; // Ȩغ +using namespace std; +vector MST; // С + +// ʼͼϢ +void build(){ + Vertex v1,v2; + int w; + cin>>Nv>>Ne; + for(int i=1;i<=Nv;i++){ + for(int j=1;j<=Nv;j++) + G[i][j] = 0; // ʼͼ + dist[i] = INF; // ʼ + parent[i] = -1; // ʼ鼯 + } + // ʼ + for(int i=0;i>v1>>v2>>w; + G[v1][v2] = w; + G[v2][v1] = w; + } +} + +// Prim㷨ǰijʼ +void IniPrim(Vertex s){ + dist[s] = 0; + MST.push_back(s); + for(Vertex i =1;i<=Nv;i++) + if(G[s][i]){ + dist[i] = G[s][i]; + parent[i] = s; + } +} + +// δ¼distСĵ +Vertex FindMin(){ + int min = INF; + Vertex xb = -1; + for(Vertex i=1;i<=Nv;i++) + if(dist[i] && dist[i] < min){ + min = dist[i]; + xb = i; + } + return xb; +} + +void output(){ + cout<<"¼˳"< +#include +#include +#include +#define INF 100000 +#define MaxVertex 105 +typedef int Vertex; +int G[MaxVertex][MaxVertex]; +int parent[MaxVertex]; // 鼯С +int Nv; // +int Ne; // +int sum; // Ȩغ +using namespace std; +struct Node{ + Vertex v1; + Vertex v2; + int weight; // Ȩ + // + bool operator < (const Node &a) const + { + return weight>a.weight; + } +}; +vector MST; // С +priority_queue q; // С + +// ʼͼϢ +void build(){ + Vertex v1,v2; + int w; + cin>>Nv>>Ne; + for(int i=1;i<=Nv;i++){ + for(int j=1;j<=Nv;j++) + G[i][j] = 0; // ʼͼ + parent[i] = -1; + } + // ʼ + for(int i=0;i>v1>>v2>>w; + struct Node tmpE; + tmpE.v1 = v1; + tmpE.v2 = v2; + tmpE.weight = w; + q.push(tmpE); + } +} + +// ·ѹ +int Find(int x){ + if(parent[x] < 0) + return x; + else + return parent[x] = Find(parent[x]); +} + +// ȹ鲢 +void Union(int x1,int x2){ + if(parent[x1] < parent[x2]){ + parent[x1] += parent[x2]; + parent[x2] = x1; + }else{ + parent[x2] += parent[x1]; + parent[x1] = x2; + } +} + +void Kruskal(){ + // Сı߲ Nv-1 һб + while(MST.size()!= Nv-1 && !q.empty()){ + Node E = q.top(); // СȡһȨСı + q.pop(); // + if(Find(E.v1) != Find(E.v2)){ // Ƿͬһ + sum += E.weight; + Union(E.v1,E.v2); // + MST.push_back(E); + } + } + +} + + +void output(){ + cout<<"¼˳"< +#include +#include +#define MAXTABLESIZE 100000 // ٵɢб +typedef int Index; +typedef int ElementType; +typedef Index Position; +typedef enum{ // ֱӦкϷԪءաɾԪ + Legitimate,Empty,Deleted +} EntryType; // 嵥Ԫ״̬ + +typedef struct HashEntry Cell; +struct HashEntry{ // ϣֵԪ + ElementType Data; // Ԫ + EntryType Info; // Ԫ״̬ +}; + +typedef struct HashTbl *HashTable; +struct HashTbl{ // ϣṹ + int TableSize; // ϣС + Cell *Cells; // ϣֵԪ +}; + +using namespace std; + +int NextPrime(int N); // +HashTable CreateTable( int TableSize); // ϣ +Index Hash(int Key,int TableSize); // ϣ + +// +int NextPrime(int N){ + int p = (N%2)?N+2:N+1; // Ӵ N ¸ʼ + int i; + + while(p <= MAXTABLESIZE){ + for(i = (int)sqrt(p);i>2;i--) + if(!(p%i)) // p + break; + if(i==2) + break; + p += 2; // ̽¸ + } + return p; +} + +// ϣ +HashTable CreateTable( int TableSize){ + HashTable H; + int i; + H = (HashTable)malloc(sizeof(struct HashTbl)); + // ֤ϣ󳤶 + H->TableSize = NextPrime(TableSize); + // ʼԪ + H->Cells = (Cell *)malloc(sizeof(Cell)*H->TableSize); + // ʼԪ״̬ + for(int i=0;iTableSize;i++) + H->Cells[i].Info = Empty; + return H; +} + +// ƽ̽ +Position Find(HashTable H,ElementType Key){ + Position CurrentPos,NewPos; + int CNum = 0 ; // ¼ͻ + CurrentPos = NewPos = Hash(Key,H->TableSize); + // ǰԪ״̬Ϊգֵȣһֱ + while(H->Cells[NewPos].Info != Empty && H->Cells[NewPos].Data != Key){ + if(++CNum % 2 ){ // ͻη + NewPos = CurrentPos + (CNum+1)/2*(CNum+1)/2; + // Խ磬һֱֱٴν߽ + while(H->TableSize <= NewPos){ + NewPos -= H->TableSize; + } + }else{ // ͻżη + NewPos = CurrentPos - CNum/2*CNum/2; + // Խ磬һֱֱٴν߽ + while(NewPos < 0){ + NewPos += H->TableSize; + } + } + } + return NewPos; +} + +// +bool Insert( HashTable H,ElementType Key,int i){ + Position Pos = i; + Pos = Find(H,Key); + // Ԫ״̬"ںϷԪ" + if( H->Cells[Pos].Info != Legitimate){ + H->Cells[Pos].Info = Legitimate; + H->Cells[Pos].Data = Key; + } + return true; +} + +// ϣ +Index Hash(int Key,int TableSize){ + return Key % TableSize; +} + + +void output(HashTable H){ + for(int i=0;iTableSize;i++) + cout<Cells[i].Data<>N; + for(int i=0;i>tmp; + Insert(H,tmp,i); + } + output(H); + return 0; +} diff --git a/上课Demo/23.散列表数组实现.exe b/上课Demo/23.散列表数组实现.exe new file mode 100644 index 0000000..8707ee5 Binary files /dev/null and b/上课Demo/23.散列表数组实现.exe differ diff --git a/上课Demo/24.散列表链表实现.cpp b/上课Demo/24.散列表链表实现.cpp new file mode 100644 index 0000000..82d0d52 --- /dev/null +++ b/上课Demo/24.散列表链表实现.cpp @@ -0,0 +1,135 @@ +#include +#include +#include +#define MAXTABLESIZE 100000 +typedef int Index; +typedef int ElementType; +typedef struct LNode *PtrToLNode; +struct LNode{ // + ElementType Data; + PtrToLNode Next; +}; +typedef PtrToLNode Position; +typedef PtrToLNode List; + +typedef struct TblNode *HashTable; // ɢб +struct TblNode{ + int TableSize; // 󳤶 + List Heads; // ָͷ +}; +using namespace std; + +int NextPrime(int N){ + int p = (N%2)?(N+2):(N+1); // tablesize + int i; + while(p <= MAXTABLESIZE){ + for(i = (int)sqrt(p);i>2;i--) + if(!(p%i)) + break; + if(i==2) // ҵ + break; + p += 2; // һ + } + return p; +} + +// ϣ +HashTable CreateTable( int TableSize){ + HashTable H; + H = (HashTable)malloc(sizeof(struct TblNode)); + H->TableSize = NextPrime(TableSize); + H->Heads = (List)malloc(sizeof(struct TblNode) * H->TableSize); + for(int i=0;iTableSize;i++) + H->Heads[i].Next = NULL; // ͷH->Heads[i] Dz涫 + return H; +} + +// ϣ +Index Hash( int TableSize,ElementType Key){ + return Key%TableSize; +} + +// +Position Find(HashTable H,ElementType Key){ + Position p; + Index pos; + + pos = Hash(H->TableSize,Key); + p = H->Heads[pos].Next; //ͷ + while(p && p->Data != Key) + p = p->Next; + return p; +} + +// +bool Insert(HashTable H,ElementType Key){ + Position p,NewCell; + Index pos; + + p = Find(H,Key); + if(!p){ // ؼδҵԲ + NewCell = (Position)malloc(sizeof(struct LNode)); + NewCell->Data = Key; + pos = Hash(H->TableSize,Key); // ʼɢбַ + // 嵽ǰ + NewCell->Next = H->Heads[pos].Next; + H->Heads[pos].Next = NewCell; + return true; + }else{ + return false; + } +} + +void output(HashTable H){ + for(int i=0;iTableSize;i++){ + cout<Heads[i].Next; + while(p){ + cout<<" "<Data; + p = p->Next; + } + cout<TableSize;i++){ + P = H->Heads[i].Next; + while( P ){ + tmp = P->Next; + free(P); + P = tmp; + } + } + free(H->Heads); + free(H); +} + + +int main(){ + HashTable H = CreateTable(9); + int N; + cin>>N; + for(int i=0;i>tmp; + Insert(H,tmp); + } + output(H); + DestroyTable(H); + return 0; +} + + + + + + + + + + + + + diff --git a/上课Demo/24.散列表链表实现.exe b/上课Demo/24.散列表链表实现.exe new file mode 100644 index 0000000..f59756f Binary files /dev/null and b/上课Demo/24.散列表链表实现.exe differ diff --git a/上课Demo/25.KMP算法.cpp b/上课Demo/25.KMP算法.cpp new file mode 100644 index 0000000..3375d2c --- /dev/null +++ b/上课Demo/25.KMP算法.cpp @@ -0,0 +1,105 @@ +/* ʵְ +#include +#include +typedef char* Position; +#define NotFound NULL + +char *Mystr(char* string,char *pattern){ + int Strlen = strlen(string); + int Patlen = strlen(pattern); + int i,j; + int tmpi; + for(i=0;i +#include +#include +typedef int Position; +#define NotFound -1 +using namespace std; + +void BuildMatch(char *pattern,int *match){ + int i,j; + int m = strlen(pattern); + match[0] = -1; + for(j=1;j=0) && (pattern[i+1] != pattern[j])) // ȣ + i = match[i]; + if(pattern[i+1] == pattern[j]) // ijڵ + match[j] = i+1; // match+1 + else // ǰûȵ + match[j] = -1; + } +} + +Position KMP(char* string,char *pattern){ + int n = strlen(string); + int m = strlen(pattern); + int s,p; + if(n < m) + return NotFound; + int *match = (int *)malloc(sizeof(int) * m); + BuildMatch(pattern,match); // match + s = p = 0; + while(s < n && p < m){ + if(string[s] == pattern[p]){ // ʱȽһ + s++; + p++; + }else if(p>0) // ˣpattern ˵ match[p-1] + 1 λ + p = match[p-1]+1; + else // p = 0 ʱ϶û˵ + s++; + } + return (p == m) ? (s-m) : NotFound; +} + +int main(){ + char string[] = "this is a simple example."; +// char string[] = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; + char pattern[] = "simple"; +// char pattern[] = "ab"; + Position p = KMP(string,pattern); + if(p==NotFound) + printf("NotFound\n"); + else + printf("%s",string+p); + return 0; +} + + + + + + + + + + + diff --git a/上课Demo/25.KMP算法.exe b/上课Demo/25.KMP算法.exe new file mode 100644 index 0000000..89da998 Binary files /dev/null and b/上课Demo/25.KMP算法.exe differ diff --git a/上课Demo/3.链表存储的线性表.c b/上课Demo/3.链表存储的线性表.c new file mode 100644 index 0000000..c15d45e --- /dev/null +++ b/上课Demo/3.链表存储的线性表.c @@ -0,0 +1,155 @@ +#include +#include +typedef int ElementType; // ElementType ɶΪ +typedef struct LNode *List; +struct LNode{ + ElementType Data; // + List Next; // һĵַ +}; +List L; + +List MakeEmpty(); //ʼ +int Length(List L); // Աķ +List FindKth(int K,List L); // Ų +List Find(ElementType X,List L); // ֵ +List Insert(ElementType X,int i,List L); // X 뵽 i-1(i>0) ֮ +List Delete(int i,List L); // ɾ i(i>0) +void Print(List L); // Ԫ + +// ʼ +List MakeEmpty(){ + List L = (List)malloc(sizeof(struct LNode)); + L = NULL; + return L; +} + +// +int Length(List L){ + List p = L; + int len=0; + while(p){ // p Ϊ + p = p->Next; + len++; + } + return len; +} + +// +List FindKth(int K,List L){ + List p = L; + int i = 1; // 1 ʼ + while(p && iNext; + i++; + } + if(i == K) // ҵ + return p; + else // δҵ + return NULL; +} + +// ֵ +List Find(ElementType X,List L){ + List p = L; + while(p && p->Data!=X) + p = p->Next; + // ҵˣ p + // δҵ NULLʱ p NULL + return p; +} + +/* +1. s ָһµĽ +2. p ָĵ i-1 +3. s->Next = p->Next s һָ p һ +4. p->Next = s p һΪ s */ +List Insert(ElementType X,int i,List L){ + List p,s; + if(i == 1){ // ½ڱͷ + s = (List)malloc(sizeof(struct LNode)); + s->Data = X; + s->Next = L; + return s; //ĽΪͷ + } + p = FindKth(i-1,L); // ҵ i-1 + if(!p){ // i-1 㲻 + printf(""); + return NULL; + }else{ + s = (List)malloc(sizeof(struct LNode)); + s->Data = X; + s->Next = p->Next; // s һָ p һ + p->Next = s; // p һΪ s + return L; + } +} + +/* ɾ +1. p ָĵ i-1 +2. s ָҪɾĵĵ i +3. p->Next = s->Nextp ָָ s +4. free(s)ͷſռ +*/ +List Delete(int i,List L){ + List p,s; + if(i==1){ //Ҫɾͷ + s = L; + if(L) // Ϊ + L = L->Next; + else + return NULL; + free(s); // ͷűɾ + return L; + } + p = FindKth(i-1,L); // ҵ i-1 + if(!p || !(p->Next)){ // i-1 i 㲻 + printf(""); + return NULL; + }else{ + s = p->Next; // s ָ i + p->Next = s->Next; //ɾ + free(s); // ͷűɾ + return L; + } +} + +// Ԫ +void Print(List L){ + List t; + int flag = 1; + printf("ǰΪ"); + for(t = L;t;t =t->Next){ + printf("%d ",t->Data); + flag = 0; + } + if(flag) + printf("NULL"); + printf("\n"); +} + +int main(){ + L = MakeEmpty(); + Print(L); + L = Insert(11,1,L); + L = Insert(25,1,L); + L = Insert(33,2,L); + L = Insert(77,3,L); + Print(L); + printf("ǰΪ%d\n",Length(L)); + printf("ʱеڶֵǣ%d\n",FindKth(2,L)->Data); + printf("22ǷڸУ"); + if(Find(22,L)) + printf("ǣ\n"); + else + printf("\n"); + printf("33ǷڸУ"); + if(Find(33,L)) + printf("ǣ\n"); + else + printf("\n"); + L = Delete(1,L); + L = Delete(3,L); + printf("----------ɾ-----\n"); + Print(L); + return 0; +} diff --git a/上课Demo/3.链表存储的线性表.exe b/上课Demo/3.链表存储的线性表.exe new file mode 100644 index 0000000..ccc23db Binary files /dev/null and b/上课Demo/3.链表存储的线性表.exe differ diff --git a/上课Demo/4.栈的顺序存储实现.c b/上课Demo/4.栈的顺序存储实现.c new file mode 100644 index 0000000..64952e1 --- /dev/null +++ b/上课Demo/4.栈的顺序存储实现.c @@ -0,0 +1,79 @@ +#include +#include +#define MaxSize 100 // ջԪص +typedef int ElementType; // ElementType ʱΪ int +typedef struct SNode *Stack; +struct SNode{ + ElementType Data[MaxSize]; // 洢ջԪ + int Top; // ¼ջԪ± +}; + +Stack CreateStack(); // ʼջ +int IsFull(Stack S); // ж϶ջǷ +int IsEmpty(Stack S); // ж϶ջǷΪ +void Push(Stack S,ElementType item); // ջ +ElementType Pop(Stack S); // ջ + +// ʼջ +Stack CreateStack(){ + Stack S; + S = (Stack)malloc(sizeof(struct SNode)); + S->Top = -1; + return S; +} + +// Ƿ +int IsFull(Stack S){ + return (S->Top == MaxSize-1); +} + +// ǷΪ +int IsEmpty(Stack S){ + return (S->Top == -1); +} + +// ջ +void Push(Stack S,ElementType item){ + if(IsFull(S)){ // Top 0 ʼ + printf("ջ"); + return; + }else{ + S->Top++; // ջԪؼһ + S->Data[S->Top] = item; // Ž + return; + } +} + +// ջ +ElementType Pop(Stack S){ + if(IsEmpty(S)){ + printf("ջ"); + return; + }else{ + ElementType val = S->Data[S->Top]; //ȡ + S->Top--; // ջԪؼһ + return val; + } +} +int main(){ + Stack S; + S = CreateStack(); + printf("5ջ\n"); + Push(S,5); + printf("7ջ\n"); + Push(S,7); + printf("66ջ\n"); + Push(S,66); + printf("%dջ\n",Pop(S)); + printf("%dջ\n",Pop(S)); + return 0; +} + + + + + + + + + diff --git a/上课Demo/4.栈的顺序存储实现.exe b/上课Demo/4.栈的顺序存储实现.exe new file mode 100644 index 0000000..49714de Binary files /dev/null and b/上课Demo/4.栈的顺序存储实现.exe differ diff --git a/上课Demo/5.栈的链表存储实现.c b/上课Demo/5.栈的链表存储实现.c new file mode 100644 index 0000000..514b0dd --- /dev/null +++ b/上课Demo/5.栈的链表存储实现.c @@ -0,0 +1,68 @@ +#include +#include +typedef int ElementType; +typedef struct SNode *Stack; +struct SNode{ + ElementType Data; + Stack Next; +}; + + +Stack CreateStack(); // ʼջ +int IsEmpty(Stack S); // жջǷΪ +void Push(Stack S,ElementType item); // ջ +ElementType Pop(Stack S); // ջ + + +// ʼ +Stack CreateStack(){ + Stack S; + S = (Stack)malloc(sizeof(struct SNode)); + S->Next = NULL; + return S; +} + +// жǷΪ +int IsEmpty(Stack S){ + return (S->Next == NULL); +} + +// ջ +void Push(Stack S,ElementType item){ + Stack tmp; + tmp = (Stack)malloc(sizeof(struct SNode)); + tmp->Data = item; + // ջջԪͷ㣬ջջԪغ + tmp->Next = S->Next; + S->Next = tmp; +} + +// ջ +ElementType Pop(Stack S){ + Stack First; + ElementType TopVal; + if(IsEmpty(S)){ + printf("ջ"); + return; + }else{ + First = S->Next; // ջһԪջԪغ + S->Next = First->Next; //ѵһԪشջɾ + TopVal = First->Data; // ȡɾֵ + free(First); // ͷſռ + return TopVal; + } +} + +int main(){ + Stack S; + S = CreateStack(); + printf("5ջ\n"); + Push(S,5); + printf("7ջ\n"); + Push(S,7); + printf("66ջ\n"); + Push(S,66); + printf("%dջ\n",Pop(S)); + printf("%dջ\n",Pop(S)); + return 0; +} diff --git a/上课Demo/5.栈的链表存储实现.exe b/上课Demo/5.栈的链表存储实现.exe new file mode 100644 index 0000000..f8db437 Binary files /dev/null and b/上课Demo/5.栈的链表存储实现.exe differ diff --git a/上课Demo/6.循环队列的顺序存储实现.c b/上课Demo/6.循环队列的顺序存储实现.c new file mode 100644 index 0000000..ecee182 --- /dev/null +++ b/上课Demo/6.循环队列的顺序存储实现.c @@ -0,0 +1,75 @@ +#include +#include +#define MaxSize 100 +typedef int ElementType; +typedef struct QNode *Queue; +struct QNode{ + ElementType Data[MaxSize]; + int front; // ¼ͷ + int rear; // ¼β +}; + +Queue CreateQueue(); // ʼ +void AddQ(Queue Q,ElementType item); // +int IsFull(Queue Q); // ж϶Ƿ +ElementType DeleteQ(Queue Q); // +int IsEmpty(Queue Q); // ж϶ǷΪ + +// ʼ +Queue CreateQueue(){ + Queue Q; + Q = (Queue)malloc(sizeof(struct QNode)); + Q->front = -1; + Q->rear = -1; + return Q; +} + +// ж϶Ƿ +int IsFull(Queue Q){ + return ((Q->rear+1) % MaxSize == Q->front); +} + +// +void AddQ(Queue Q,ElementType item){ + if(IsFull(Q)){ + printf(""); + return; + }else{ + Q->rear = (Q->rear+1) % MaxSize; + Q->Data[Q->rear] = item; + } +} + +//ж϶ǷΪ +int IsEmpty(Queue Q){ + return (Q->front == Q->rear); +} + +// +ElementType DeleteQ(Queue Q){ + if(IsEmpty(Q)){ + printf("п"); + return 0; + }else{ + Q->front = (Q->front+1) % MaxSize; + return Q->Data[Q->front]; + } +} + +int main(){ + Queue Q; + Q = CreateQueue(); + AddQ(Q,3); + printf("3\n"); + AddQ(Q,5); + printf("5\n"); + AddQ(Q,11); + printf("11\n"); + printf("%d\n",DeleteQ(Q)); + printf("%d\n",DeleteQ(Q)); + return 0; +} + + + + diff --git a/上课Demo/6.循环队列的顺序存储实现.exe b/上课Demo/6.循环队列的顺序存储实现.exe new file mode 100644 index 0000000..17fe022 Binary files /dev/null and b/上课Demo/6.循环队列的顺序存储实现.exe differ diff --git a/上课Demo/7.队列的链式存储实现.c b/上课Demo/7.队列的链式存储实现.c new file mode 100644 index 0000000..ab51396 --- /dev/null +++ b/上课Demo/7.队列的链式存储实现.c @@ -0,0 +1,97 @@ +#include +#include +typedef int ElementType; +typedef struct QNode *Queue; +struct Node{ + ElementType Data; + struct Node *Next; +}; +struct QNode{ + struct Node *rear; // ָβ + struct Node *front; // ָͷ +}; + +Queue CreateQueue(); // ʼ +void AddQ(Queue Q,ElementType item); // +ElementType DeleteQ(Queue Q); // +int IsEmpty(Queue Q); // ж϶ǷΪ + +// ʼ +Queue CreateQueue(){ + Queue Q; + Q = (Queue)malloc(sizeof(struct QNode)); + Q->front = NULL; + Q->rear = NULL; + return Q; +} + +// ǷΪ +int IsEmpty(Queue Q){ + return (Q->front == NULL); +} + +// +void AddQ(Queue Q,ElementType item){ + struct Node *node; + node = (struct Node *)malloc(sizeof(struct Node)); + node->Data = item; + node->Next = NULL; + if(Q->rear==NULL){ //ʱп + Q->rear = node; + Q->front = node; + }else{ //Ϊ + Q->rear->Next = node; // + Q->rear = node; // rear Ȼ + } +} + +// +ElementType DeleteQ(Queue Q){ + struct Node *FrontCell; + ElementType FrontElem; + if(IsEmpty(Q)){ + printf("п"); + return 0; + } + FrontCell = Q->front; + if(Q->front == Q->rear){ // ֻһԪ + Q->front = Q->rear = NULL; + }else{ + Q->front = Q->front->Next; + } + FrontElem = FrontCell->Data; + free(FrontCell); + return FrontElem; +} + +int main(){ + Queue Q; + Q = CreateQueue(); + printf("5\n"); + AddQ(Q,5); + printf("4\n"); + AddQ(Q,4); + printf("4\n"); + AddQ(Q,3); + printf("%d\n",DeleteQ(Q)); + printf("%d\n",DeleteQ(Q)); + printf("%d\n",DeleteQ(Q)); + printf("%d\n",DeleteQ(Q)); + return 0; +} + + + + + + + + + + + + + + + + diff --git a/上课Demo/7.队列的链式存储实现.exe b/上课Demo/7.队列的链式存储实现.exe new file mode 100644 index 0000000..2c805d2 Binary files /dev/null and b/上课Demo/7.队列的链式存储实现.exe differ diff --git a/上课Demo/8.树的链表存储实现.cpp b/上课Demo/8.树的链表存储实现.cpp new file mode 100644 index 0000000..27484b7 --- /dev/null +++ b/上课Demo/8.树的链表存储实现.cpp @@ -0,0 +1,265 @@ +#include +#include +#include +#include +#include +typedef struct TreeNode *BinTree; +struct TreeNode{ + int Data; // ֵ + BinTree Left; // ӽ + BinTree Right; // Ҷӽ +}; +BinTree CreatBinTree(); // һ +bool IsEmpty(BinTree BT); // ж BT ǷΪ +void PreOrderTraversal(BinTree BT); // +void InOrderTraversal(BinTree BT); // +void PostOrderTraversal(BinTree BT); // Ҹ +using namespace std; +typedef struct SNode *Stack; +struct SNode{ + BinTree Data; + Stack Next; +}; + + +Stack CreateStack(); // ʼջ +int IsEmpty(Stack S); // жջǷΪ +void Push(Stack S,BinTree item); // ջ +BinTree Pop(Stack S); // ջ + + +// ʼ +Stack CreateStack(){ + Stack S; + S = (Stack)malloc(sizeof(struct SNode)); + S->Next = NULL; + return S; +} + +// жǷΪ +int IsEmpty(Stack S){ + return (S->Next == NULL); +} + +// ջ +void Push(Stack S,BinTree item){ + Stack tmp; + tmp = (Stack)malloc(sizeof(struct SNode)); + tmp->Data = item; + // ջջԪͷ㣬ջջԪغ + tmp->Next = S->Next; + S->Next = tmp; +} + +// ջ +BinTree Pop(Stack S){ + Stack First; + BinTree TopVal; + if(IsEmpty(S)){ + printf("ջ"); + return 0; + }else{ + First = S->Next; // ջһԪջԪغ + S->Next = First->Next; //ѵһԪشջɾ + TopVal = First->Data; // ȡɾֵ + free(First); // ͷſռ + return TopVal; + } +} + +BinTree Insert(int Data){ + BinTree BT; + BT = (BinTree)malloc(sizeof(struct TreeNode)); + BT->Data = Data; + BT->Left = NULL; + BT->Right = NULL; + return BT; +} + +// ʼ +BinTree CreatBinTree(){ + BinTree BT; + BT = (BinTree)malloc(sizeof(struct TreeNode)); + BT->Data = 1; + BT->Left = Insert(2); + BT->Right = Insert(3); + BT->Left->Left = Insert(4); + BT->Left->Right = Insert(6); + BT->Left->Right->Left = Insert(5); + BT->Right->Left = Insert(7); + BT->Right->Right = Insert(9); + BT->Right->Left->Right = Insert(8); + return BT; +} + + +// жǷΪ +/*bool IsEmpty(BinTree BT){ +}*/ + +// +/*void PreOrderTraversal(BinTree BT){ + if(BT){ + printf("%d",BT->Data); // ӡ + PreOrderTraversal(BT->Left); // + PreOrderTraversal(BT->Right); // + } +} */ + +// ǵݹ +void PreOrderTraversal(BinTree BT){ + BinTree T = BT; + Stack S = CreateStack(); // ʼջ S + while(T || !IsEmpty(S)){ // Ϊջջ + while(T){ + Push(S,T); // ѹջһý + printf("%d",T->Data); // ʽ + T = T->Left; // + } + if(!IsEmpty(S)){ // ջ + T = Pop(S); // ջڶý + T = T->Right; // ҽ + } + } +} + +// ݹ +/*void InOrderTraversal(BinTree BT){ + if(BT){ + InOrderTraversal(BT->Left); // + printf("%d",BT->Data); // ӡ + InOrderTraversal(BT->Right); // + } +} */ + +// ǵݹ +void InOrderTraversal(BinTree BT){ + BinTree T = BT; + Stack S = CreateStack(); // ʼջ S + while(T || !IsEmpty(S)){ // Ϊջջ + while(T){ + Push(S,T); // ѹջ + T = T->Left; // + } + if(!IsEmpty(S)){ // ջ + T = Pop(S); // ջ + printf("%d",T->Data); // ʽ + T = T->Right; // ҽ + } + } +} + +// +/*void PostOrderTraversal(BinTree BT){ + if(BT){ + PostOrderTraversal(BT->Left); // + PostOrderTraversal(BT->Right); // + printf("%d",BT->Data); // ӡ + } +} */ + +// +void PostOrderTraversal(BinTree BT){ + BinTree T = BT; + Stack S = CreateStack(); // ʼջ S + vector v; + Push(S,T); + while(!IsEmpty(S)){ // Ϊջջ + T = Pop(S); + v.push_back(T); + if(T->Left) + Push(S,T->Left); + if(T->Right) + Push(S,T->Right); + } + reverse(v.begin(),v.end()); // ת + for(int i=0;iData); +} + +// α +void LevelOrderTraversal(BinTree BT){ + queue q; + BinTree T; + if(!BT) + return; + q.push(BT); // BT + while(!q.empty()){ + T = q.front(); // ʶԪ + q.pop(); // + printf("%d",T->Data); + if(T->Left) + q.push(T->Left); + if(T->Right) + q.push(T->Right); + } +} +// Ҷӽ +void FindLeaves(BinTree BT){ + if(BT){ + if( !BT->Left && !BT->Right) + printf("%d",BT->Data); // ӡҶӽ + FindLeaves(BT->Left); // + FindLeaves(BT->Right); // + } +} + +// ߶ +int GetHeight(BinTree BT){ + int hl,hr,maxh; + if(BT){ + hl = GetHeight(BT->Left); // ߶ + hr = GetHeight(BT->Right); // ߶ + maxh = (hl>hr)?hl:hr; + return maxh+1; // ǰ߶Ϊĸ߶+1 + }else + return 0; +} +int main(){ + BinTree BT,ST; + BT = CreatBinTree(); + printf(""); + PreOrderTraversal(BT); + printf("\n"); + InOrderTraversal(BT); + printf("\n"); + PostOrderTraversal(BT); + printf("\nα"); + LevelOrderTraversal(BT); + printf("\nҶӽ:"); + FindLeaves(BT); + printf("\nĸ߶ȣ%d",GetHeight(BT)); + return 0; +} + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/上课Demo/8.树的链表存储实现.exe b/上课Demo/8.树的链表存储实现.exe new file mode 100644 index 0000000..4b78974 Binary files /dev/null and b/上课Demo/8.树的链表存储实现.exe differ diff --git a/上课Demo/9.二叉搜索树的操作.cpp b/上课Demo/9.二叉搜索树的操作.cpp new file mode 100644 index 0000000..35e0f85 --- /dev/null +++ b/上课Demo/9.二叉搜索树的操作.cpp @@ -0,0 +1,150 @@ +#include +#include +using namespace std; +typedef int ElementType; +typedef struct TreeNode *BinTree; +struct TreeNode{ + ElementType Data; + BinTree Left; + BinTree Right; +}; + +// ҵݹʵ +BinTree Find(ElementType X,BinTree BST){ + if(!BST) // Ϊգ NULL + return NULL; + if(X < BST->Data) // ȸСȥ + return Find(X,BST->Left); + else if(BST->Data < X) // ȸȥ + return Find(X,BST->Right); + else if(BST->Data == X) // ҵ + return BST; +} + +// ҷǵݹʵ +BinTree IterFind(ElementType X,BinTree BST){ + while(BST){ + if(X < BST->Data) + BST = BST->Left; + else if(BST->Data < X) // ȸȥ + BST = BST->Right; + else if(BST->Data == X) // ҵ + return BST; + } + return NULL; +} + +// Сֵĵݹʵ +BinTree FindMin(BinTree BST){ + if(!BST) // Ϊˣ NULL + return NULL; + else if(BST->Left) // ֧ + return FindMin(BST->Left); + else // ҵ + return BST; +} + +// ֵķǵݹʵ +BinTree FindMax(BinTree BST){ + if(BST) // + while(BST->Right) // ֻҪ + BST = BST->Right; + return BST; +} + +// +BinTree Insert(ElementType X,BinTree BST){ + if(!BST){ // Ϊգʼý + BST = (BinTree)malloc(sizeof(struct TreeNode)); + BST->Data = X; + BST->Left = NULL; + BST->Right = NULL; + }else{ // Ϊ + if(X < BST->Data) // С + BST->Left = Insert(X,BST->Left); + else if(BST->Data < X) // 󣬹ұ + BST->Right = Insert(X,BST->Right); + // ȣʲô + } + return BST; +} + +// ɾ +BinTree Delete(ElementType X,BinTree BST){ + BinTree tmp; + if(!BST) + cout<<"ҪɾԪδҵ"; + else if(X < BST->Data) // X ȵǰֵСɾ + BST->Left = Delete(X,BST->Left); + else if(BST->Data < X) // x ȵǰֵɾ + BST->Right = Delete(X,BST->Right); + else{ // ҵɾ + if(BST->Left && BST->Right){ // ɾӽ + tmp = FindMin(BST->Right); // ҵֵС + BST->Data = tmp->Data; // ҵֵǵǰ + BST->Right = Delete(tmp->Data,BST->Right); // ǰҵСֵɾ + }else{ // ɾֻһӽûкӽ + tmp = BST; + if(!BST->Left && !BST->Right) // ûкӽ + BST = NULL; + else if(BST->Left && !BST->Right) // ֻӽ + BST = BST->Left; + else if(!BST->Left && BST->Right) // ֻҺӽ + BST = BST->Right; + } + free(tmp); + } + return BST; +} + +// +void InOrderTraversal(BinTree BT){ + if(BT){ + InOrderTraversal(BT->Left); // + cout<Data; // ӡ + InOrderTraversal(BT->Right); // + } +} +int main(){ + BinTree BST = NULL; + BST = Insert(5,BST); + BST = Insert(7,BST); + BST = Insert(3,BST); + BST = Insert(1,BST); + BST = Insert(2,BST); + BST = Insert(4,BST); + BST = Insert(6,BST); + BST = Insert(8,BST); + BST = Insert(9,BST); + /* + 5 + /\ + 3 7 + /\ /\ + 1 4 6 8 + \ \ + 2 9 + */ + cout<<"Ľǣ"; + InOrderTraversal(BST); + cout<Data<Data<Left->Data<Right->Data< +using namespace std; +/* һȷеĿͷͽβٱۼӣʱ临Ӷ O(n^3)*/ +int MaxSubseqSum1(int n,int a[]){ + int max = 0; + for(int i=0;iB)?((A>C)?A:C):((B>C)?B:C); +} +/* ֽɸСģ*/ +int DivideAndConquer(int a[],int left,int right){ + + /*ݹֻһ*/ + if(left == right){ + if(0 < a[left]) + return a[left]; + return 0; + } + + /* ֱҵк*/ + int center = (left+right)/2; + int MaxLeftSum = DivideAndConquer(a,left,center); + int MaxRightSum = DivideAndConquer(a,center+1,right); + + /* ٷֱҿк*/ + int MaxLeftBorderSum = 0; + int LeftBorderSum = 0; + for(int i=center;i>=left;i--){ + LeftBorderSum += a[i]; + if(MaxLeftBorderSum < LeftBorderSum) + MaxLeftBorderSum = LeftBorderSum; + } + int MaXRightBorderSum = 0; + int RightBorderSum = 0; + for(int i=center+1;i<=right;i++){ + RightBorderSum += a[i]; + if(MaXRightBorderSum < RightBorderSum) + MaXRightBorderSum = RightBorderSum; + } + + /*󷵻طֽкͣұкͣͿк*/ + return Max3(MaxLeftSum,MaxRightSum,MaXRightBorderSum+MaxLeftBorderSum); +} +int MaxSubseqSum4(int n,int a[]){ + return DivideAndConquer(a,0,n-1); +} +int main(){ + int n; + int a[100000+5]; + cin>>n; + for(int i=0;i>a[i]; + cout< +#include +#include +using namespace std; +typedef struct TreeNode *BinTree; +struct TreeNode{ + int data; // ֵ + BinTree left; // + BinTree right; // +}; + +// һ +BinTree Insert(int x,BinTree BST){ + if(!BST){ // Ϊգ + BST = (BinTree)malloc(sizeof(struct TreeNode)); + BST->data = x; + BST->left = NULL; + BST->right = NULL; + }else{ // 㲻Ϊ + if(x < BST->data) // x ȵǰֵС + BST->left = Insert(x,BST->left); // ݹ鵽 + else if(BST->data < x) // x ȵǰֵ + BST->right = Insert(x,BST->right); // ݹ鵽 + // ȣʲôҲ + } + return BST; +} + +// ǰ +void PreOrderTraversal(BinTree BST,string &s){ + if(BST){ + + PreOrderTraversal(BST->left,s); // + s += BST->data+'0'; // ֵַ + PreOrderTraversal(BST->right,s); // + } +} + +int main(){ + int n,l; + int tmp; + cin>>n>>l; + while(n){ // n Ϊѭ + BinTree InitBST = NULL; + string Initstr; + // ÿ n l ijʼ + for(int i=0;i>tmp; + InitBST = Insert(tmp,InitBST); + } + // Initstr ¼ʼγɵ + // ˼Ϊʲô¼ + PreOrderTraversal(InitBST,Initstr); + + // l + for(int i=0;i>tmp; + BST = Insert(tmp,BST); + } + // ÿеIJвһ str ¼ + PreOrderTraversal(BST,str); + + // ٽʼкÿβвֵжԱ + if(str == Initstr) + cout<<"Yes"<>n>>l; + } + return 0; +} diff --git a/编程作业/10.是否为同一棵搜索树.exe b/编程作业/10.是否为同一棵搜索树.exe new file mode 100644 index 0000000..cd3a65d Binary files /dev/null and b/编程作业/10.是否为同一棵搜索树.exe differ diff --git a/编程作业/11.Tree Traversals Again.cpp b/编程作业/11.Tree Traversals Again.cpp new file mode 100644 index 0000000..99aeeaa --- /dev/null +++ b/编程作业/11.Tree Traversals Again.cpp @@ -0,0 +1,79 @@ +#include +#include +#include +#include +#include +using namespace std; +typedef struct TreeNode *Tree; +struct TreeNode{ + string data; + Tree left; // + Tree right; // +}; +// ʼһ +Tree create(){ + Tree T; + T = (Tree)malloc(sizeof(struct TreeNode)); + T->left = NULL; + T->right = NULL; + return T; +} + +// +Tree restore(Tree T){ + int n; + string str; + stack s; + Tree node = T; + bool flag = false; + string value; + scanf("%d\n",&n); + // ڵ㸳ֵ + getline(cin,str); + value = str.substr(5); // ӵʼȡ + node->data = value; + // ջ + s.push(node); + for(int i=1;i<2*n;i++){ + getline(cin,str); + if(str=="Pop"){// pop + node = s.top(); + s.pop(); + }else{ // push + value = str.substr(5); // ӵʼȡ + Tree tmp = create(); + tmp->data = value; + if(!node->left){// ӿգ½ + node->left = tmp; + node = node->left; + }else if(!node->right){ // Ҷӿգ½Ҷ + node->right = tmp; + node = node->right; + } + s.push(tmp); + } + } + return T; +} + +// ݹ +void bl(Tree T,bool &flag){ + if(T){ + bl(T->left,flag); + bl(T->right,flag); + if(!flag) + flag = true; + else + cout<<" "; + cout<data; + } +} +int main(){ + Tree T; + bool flag = false; + string str; + T = create(); + T = restore(T); + bl(T,flag); + return 0; +} diff --git a/编程作业/11.Tree Traversals Again.exe b/编程作业/11.Tree Traversals Again.exe new file mode 100644 index 0000000..e7a8f99 Binary files /dev/null and b/编程作业/11.Tree Traversals Again.exe differ diff --git a/编程作业/12.Root of AVL Tree.cpp b/编程作业/12.Root of AVL Tree.cpp new file mode 100644 index 0000000..1686420 --- /dev/null +++ b/编程作业/12.Root of AVL Tree.cpp @@ -0,0 +1,137 @@ +#include +#include +typedef struct AVLNode *AVLTree; +struct AVLNode{ + int data; // ֵ + AVLTree left; // + AVLTree right; // + int height; // +}; +using namespace std; + +// ֵ +int Max(int a,int b){ + return a>b?a:b; +} + +// ߣ -1 +int getHeight(AVLTree A){ + return A==NULL?-1:A->height; +} + +// LL +// B ڳҸ A ٽ A ҵ B ȥ +AVLTree LLRotation(AVLTree A){ + /* + A + / + B + / + C + */ + // ʱڵ A + AVLTree B = A->left; // B Ϊ A + A->left = B->right; // B A + B->right = A; // A B + A->height = Max(getHeight(A->left),getHeight(A->right)) + 1; + B->height = Max(getHeight(B->left),A->height) + 1; + return B; // ʱ B Ϊ + /* + B + / \ + C A + */ +} + +// RR +AVLTree RRRotation(AVLTree A){ + /* + A + \ + B + \ + C + */ + // ʱڵ A + AVLTree B = A->right; + A->right = B->left; + B->left = A; + A->height = Max(getHeight(A->left),getHeight(A->right)) + 1; + B->height = Max(getHeight(B->left),A->height) + 1; + return B; // ʱ B Ϊ + /* + B + / \ + A C + */ +} + +// LR˫ +AVLTree LRRotation(AVLTree A){ + /* + A + / + B + \ + C + */ + // RR + A->left = RRRotation(A->left); + /* + */ + // LL + return LLRotation(A); +} + +// RL˫ +AVLTree RLRotation(AVLTree A){ + // LL + A->right = LLRotation(A->right); + // RR + return RRRotation(A); +} + +AVLTree Insert(AVLTree T,int x){ + if(!T){ // ýΪգʼ + T = (AVLTree)malloc(sizeof(struct AVLNode)); + T->data = x; + T->left = NULL; + T->right = NULL; + T->height = 0; + }else{ // Ϊգ + if(x < T->data){ // + T->left = Insert(T->left,x); + if(getHeight(T->left)-getHeight(T->right)==2){ // ߶ȲΪ 2 + if(x < T->left->data) // LL + T = LLRotation(T); + else if(T->left->data < x) // LR˫ + T = LRRotation(T); + } + }else if(T->data < x){ + T->right = Insert(T->right,x); + if(getHeight(T->right)-getHeight(T->left)==2){ + if(x < T->right->data) // RL ˫ + T = RLRotation(T); + else if(T->right->data < x) // RR + T = RRRotation(T); + } + } + } + // + T->height = Max(getHeight(T->left),getHeight(T->right)) + 1; + return T; +} + + +int main(){ + AVLTree T=NULL; + int n; + cin>>n; + for(int i=0;i>tmp; + T = Insert(T,tmp); + } + cout<data; + return 0; +} diff --git a/编程作业/12.Root of AVL Tree.exe b/编程作业/12.Root of AVL Tree.exe new file mode 100644 index 0000000..013252d Binary files /dev/null and b/编程作业/12.Root of AVL Tree.exe differ diff --git a/编程作业/13.Complete Binary Search Tree.cpp b/编程作业/13.Complete Binary Search Tree.cpp new file mode 100644 index 0000000..b7b97a6 --- /dev/null +++ b/编程作业/13.Complete Binary Search Tree.cpp @@ -0,0 +1,52 @@ +#include +#include +#include +#include +#define MaxSize 2005 +using namespace std; +int value[MaxSize]; +int BST[MaxSize]; + +// n +int getLeftTreeSize(int n){ + int h =0; // ýIJ + int tmp = n+1; + while(tmp!=1){ + tmp /=2; + h++; + } + int x = n-pow(2,h)+1; // һҶ + x = x>n; + for(int i=0;i>value[i]; + } + // С + sort(value,value+n); + fill(0,n-1,0); + for(int i=0;i +#include + +typedef int ElementType; +typedef struct TNode *Position; +typedef Position BinTree; +struct TNode{ + ElementType Data; + BinTree Left; + BinTree Right; +}; + +void PreorderTraversal( BinTree BT ); /* ɲʵ֣ϸڲ */ +void InorderTraversal( BinTree BT ); /* ɲʵ֣ϸڲ */ + +BinTree Insert( BinTree BST, ElementType X ); +BinTree Delete( BinTree BST, ElementType X ); +Position Find( BinTree BST, ElementType X ); +Position FindMin( BinTree BST ); +Position FindMax( BinTree BST ); + + +int main() +{ + BinTree BST, MinP, MaxP, Tmp; + ElementType X; + int N, i; + + BST = NULL; + scanf("%d", &N); + for ( i=0; iData); + if (Tmp==MinP) printf("%d is the smallest key\n", Tmp->Data); + if (Tmp==MaxP) printf("%d is the largest key\n", Tmp->Data); + } + } + scanf("%d", &N); + for( i=0; iData = X; + BST->Left = NULL; + BST->Right = NULL; + }else{ + if(X < BST->Data) + BST->Left = Insert(BST->Left,X); + else if(BST->Data < X) + BST->Right = Insert(BST->Right,X); + } + return BST; +} + +// ɾ +BinTree Delete( BinTree BST, ElementType X ){ + BinTree tmp; + if(!BST){ + printf("Not Found\n"); + return BST; + }else{ + if(X < BST->Data) + BST->Left = Delete(BST->Left,X); + else if(BST->Data < X) + BST->Right = Delete(BST->Right,X); + else{ // ҵҪɾ + if(BST->Left && BST->Right){ // ýҶ + tmp = FindMin(BST->Right); + BST->Data = tmp->Data; + BST->Right = Delete(BST->Right,tmp->Data); + }else{ + tmp = BST; + if(BST->Left && !BST->Right) + BST = BST->Left; + else if(!BST->Left && BST->Right) + BST = BST->Right; + else + BST = NULL; + free(tmp); + } + } + } + return BST; +} + +// ѰֵС +Position FindMin( BinTree BST ){ + if(BST) + while(BST->Left) + BST = BST->Left; + return BST; +} + +// Ѱֵ +Position FindMax( BinTree BST ){ + if(BST) + while(BST->Right) + BST = BST->Right; + return BST; +} + +// +Position Find( BinTree BST, ElementType X ){ + if(!BST){ + return NULL; + }else if(X < BST->Data) + return Find(BST->Left,X); + else if(BST->Data < X) + return Find(BST->Right,X); + else + return BST; +} + +// +void PreorderTraversal( BinTree BT ){ + if(BT){ + printf(" %d",BT->Data); + PreorderTraversal(BT->Left); + PreorderTraversal(BT->Right); + } +} +// +void InorderTraversal( BinTree BT ){ + if(BT){ + + InorderTraversal(BT->Left); + printf(" %d",BT->Data); + InorderTraversal(BT->Right); + } +} diff --git a/编程作业/14.二叉搜索树的操作集.exe b/编程作业/14.二叉搜索树的操作集.exe new file mode 100644 index 0000000..3593c4b Binary files /dev/null and b/编程作业/14.二叉搜索树的操作集.exe differ diff --git a/编程作业/15.堆中的路径.cpp b/编程作业/15.堆中的路径.cpp new file mode 100644 index 0000000..7f7979c --- /dev/null +++ b/编程作业/15.堆中的路径.cpp @@ -0,0 +1,50 @@ +#include +#include +const int MinData = -100000; // ڱֵ +const int MaxSize = 1005; // +using namespace std; +typedef struct HeapStruct *Heap; +struct HeapStruct{ + int *data; // ֵ + int size; // ǰԪظ + int capacity; // +}; + +// ʼ +Heap Create(){ + Heap H; + H = (Heap)malloc(sizeof(struct HeapStruct)); + H->data = (int *)malloc(sizeof(int) * (MaxSize+1)); + H->size = 0; + H->capacity = MaxSize; + H->data[0] = MinData; + return H; +} + +// +void Insert(Heap H,int x){ + int i = ++H->size; // ָһ + for(;H->data[i/2]>x;i/=2) + H->data[i] = H->data[i/2]; + H->data[i] = x; +} + +// +void bl(Heap H){ + for(int i=1;i<=H->size;i++) + cout<data[i]; +} + +int main(){ + Heap H; + H = Create(); + int n; + cin>>n; + for(int i=0;i>t; + Insert(H,t); + } + bl(H); + return 0; +} diff --git a/编程作业/15.堆中的路径.exe b/编程作业/15.堆中的路径.exe new file mode 100644 index 0000000..a9eafb5 Binary files /dev/null and b/编程作业/15.堆中的路径.exe differ diff --git a/编程作业/16.File Transfer.cpp b/编程作业/16.File Transfer.cpp new file mode 100644 index 0000000..c281f09 --- /dev/null +++ b/编程作业/16.File Transfer.cpp @@ -0,0 +1,83 @@ +#include +#define MaxSize 10005 +typedef int SetType; +using namespace std; +// ʼ +void Init(SetType s[],int n){ + for(int i=0;i +#include +#include +#include +#define MaxSize 64 +using namespace std; +priority_queue,greater >q; // ȶУǰֵС +map mapp; +struct character{ + char ch; // ַ + int fre; // Ƶ +}; +struct huffmanTree{ + char ch; // ַ + string str; // +}; + + +// +int bulidTree(int n,character c[]){ + int weight = 0; + // + for(int i=0;i1){ + // ȡѶԪ + int x = q.top(); + // ѶԪ + q.pop(); + int y = q.top(); + q.pop(); + // + q.push(x+y); + weight += x+y; // õ볤 + // СȨֵ᲻ϱ + } + q.pop(); + return weight; +} +bool cmp(huffmanTree a,huffmanTree b){ + return a.str.size() < b.str.size(); +} + +// жǷΪǰ׺ +bool isPrefix(huffmanTree code[],int n){ + // ַȴС + sort(code,code+n,cmp); + for(int i=0;i>code[i].ch>>code[i].str; + // 볤ȵڱ볤*Ƶܺ + codelen += mapp[code[i].ch]*code[i].str.size(); + } + if(codelen != weight || isPrefix(code,n)) + cout<<"no"<>n; + character c[MaxSize]; + for(int i=0;i>c[i].ch>>c[i].fre; + mapp[c[i].ch] = c[i].fre; + } + int weight = bulidTree(n,c); + cin>>m; + for(int i=0;i +#include +#include +#include +#include +#define HeapCapacity 64 +#define MinData 0 +typedef struct TreeNode *HuffmanTree; +typedef struct Heap *MinHeap; +struct Heap{ // + HuffmanTree *data; // + int size; // ѵĵǰС +}; +struct TreeNode{ // + int weight; // Ƶ + HuffmanTree left; + HuffmanTree right; +}; +using namespace std; + +MinHeap createHeap(); // +HuffmanTree createHuffman(); // +void sortHeap(MinHeap H,int i); // С +void adjust(MinHeap H); // +MinHeap InitHeap(int n); // ʼ +HuffmanTree Delete(MinHeap H); // ѵɾ +void Insert(MinHeap H,HuffmanTree Huff); // ѵIJ +HuffmanTree Huffman(MinHeap H); // Ĺ +int WPL(HuffmanTree Huff,int depth); // HuffmanTree ı볤 +void PreOrderTraversal(HuffmanTree Huff); // ǰ + + +map mappp; // ַƵʵӳϵ + +// +MinHeap createHeap(){ + MinHeap H; + H = (MinHeap)malloc(sizeof(struct Heap)); + H->data = (HuffmanTree *)malloc(sizeof(struct TreeNode) * HeapCapacity); + H->size = 0; + // ڱ + HuffmanTree Huff = createHuffman(); + H->data[0] = Huff; + return H; +} + +// +HuffmanTree createHuffman(){ + HuffmanTree Huff; + Huff = (HuffmanTree)malloc(sizeof(struct TreeNode)); + Huff->weight = MinData; // ʼƵС + Huff->left = NULL; + Huff->right = NULL; + return Huff; +} + +// С +void sortHeap(MinHeap H,int i){ + int parent,child; + HuffmanTree Huff = H->data[i]; // õǰĹ + for(parent = i;parent*2<=H->size;parent = child){ + // ҶС + child = parent * 2; + if((child!=H->size) && (H->data[child+1]->weight < H->data[child]->weight)) + child++; + // ûиСˣѭ + if(Huff->weight <= H->data[child]->weight) + break; + // Ѷӽ + H->data[parent] = H->data[child]; + } + H->data[parent] = Huff; +} + + +// +void adjust(MinHeap H){ + // ӵһкӽĽ㿪ʼ + for(int i=H->size/2;i>0;i--) + sortHeap(H,i); +} + +// ʼ +MinHeap InitHeap(int n){ + MinHeap H =createHeap(); + HuffmanTree Huff; + char c; // ʱַ + int f; // ʱƵ + for(int i=0;i(c,f)); // ַƵʵӳϵmap + Huff = createHuffman(); + Huff->weight = f; + H->data[++H->size] = Huff; + } + // С + adjust(H); + return H; +} + +// ѵɾ +HuffmanTree Delete(MinHeap H){ + int parent,child; + HuffmanTree T = H->data[1]; // õĹ + HuffmanTree Huff = H->data[H->size--]; // õһλõĹ + for(parent = 1;parent*2<=H->size;parent = child){ + // ҶС + child = parent * 2; + if((child!=H->size) && (H->data[child+1]->weight < H->data[child]->weight)) + child++; + // ûиСˣѭ + if(Huff->weight <= H->data[child]->weight) + break; + // Ѷӽ + H->data[parent] = H->data[child]; + } + H->data[parent] = Huff; + return T; +} + +// ѵIJ +void Insert(MinHeap H,HuffmanTree Huff){ + int i = ++H->size; + for(;Huff->weight < H->data[i/2]->weight;i/=2) + H->data[i] = H->data[i/2]; + H->data[i] = Huff; +} + +// Ĺ +HuffmanTree Huffman(MinHeap H){ + HuffmanTree Huff; + int times = H->size; + for(int i=1;ileft = Delete(H); // Ӷɾһ㣬Ϊ T ӽ + Huff->right = Delete(H); // Ӷɾһ㣬Ϊ T ӽ + Huff->weight = Huff->left->weight + Huff->right->weight; // ¼Ȩֵ + Insert(H,Huff); // ټӽ + } + Huff = Delete(H); + return Huff; +} + +// HuffmanTree ı볤 +int WPL(HuffmanTree Huff,int depth){ + // Ҷ㣬ر볤 + if(Huff->left==NULL && Huff->right==NULL) + return depth*Huff->weight; + else // 򷵻ӽı볤 + return (WPL(Huff->left,depth+1) + WPL(Huff->right,depth+1)); +} + +// ύ +void submit(int n,int codeLen){ + HuffmanTree Huff = createHuffman(); + HuffmanTree pre; + int counter = 1; + bool flag = true; + char ch; + string code; + for(int i=0;i>code; + // + for(int j=0;jleft==NULL){ // ڣ + pre->left =createHuffman(); + counter++; + } + if(pre->weight != 0) + flag =false; + pre = pre->left; + }else if(code[j]=='1'){ // ǰΪ 0֧ + if(pre->right==NULL){ // ڣ + pre->right = createHuffman(); + counter++; + } + if(pre->weight != 0) + flag =false; + pre = pre->right; + } + } + if(pre->left || pre->right) + flag = false; + pre->weight = mappp[ch]; // mapp ȡƵ + } + if(counter!=2*n-1 || !flag || WPL(Huff,0) != codeLen){ // 㲻 2n-1 ߱볤Ȳ + printf("No\n"); + return; + }else{ + printf("Yes\n"); + return; + } +} + + +int main(){ + int n,m; + scanf("%d",&n); + // ʼС + MinHeap H = InitHeap(n); + // ʼ + HuffmanTree Huff = Huffman(H); + // ùı볤 + int codeLen = WPL(Huff,0); + scanf("%d",&m); + for(int i=0;i +#include +#include +#define MaxVertex 100 +typedef int Vertex; +int G[MaxVertex][MaxVertex]; +bool visit[MaxVertex]; +int Ne,Nv; +using namespace std; + +// ͼ +void Build(){ + cin>>Nv; + for(int i=0;i>Ne; + for(int i=0;i>v1>>v2; + G[v1][v2] = 1; + G[v2][v1] = 1; + } +} + +void DFS(Vertex v){ + // ѷ + visit[v] = true; + cout<<" "< q; + // ı״̬ + visit[v] = true; + cout<<" "< +#include +#include +#include +#define MaxVertex 105 +struct Node{ // ± + int hor; // + int ver; // + bool visit; // Ƿ񱻷 + bool safe; // Ƿϰ + bool jump; // һܷȥ +}; +int N; // +int D; // Ծ +bool isSafe; // Ƿϰ +Node G[MaxVertex]; +using namespace std; +const double diameter=15; + +// +double getLen(int x1,int y1,int x2,int y2){ + return sqrt(pow(x1-x2,2.0)+pow(y1-y2,2.0)); +} + +// ܷ񵽰 +bool ashore(int x,int y){ + // ֱ㵱ǰ밶ߵľ + // (x,50),(x,-50),(50,y),(-50,y) ľ + if(abs(x-50)<=D || abs(x+50)<=D || abs(y+50)<=D || abs(y-50)<=D) + return true; + return false; +} + +// ȷǷȫ("ϰ") +void getSafe(){ + for(int i=0;i>N>>D; + int x,y; + for(int i=0;i>x>>y; + G[i].hor = x; + G[i].ver = y; + G[i].visit = false; + } + getSafe(); + getJump(); + isSafe = false; +} +/* +void DFS(int v){ + if(G[v].safe){ + isSafe = true; + return; + } + G[v].visit = true; + for(int i=0;i q; + Node tmp; + G[v].visit = true; + // һֻ + q.push(G[v]); + while(!q.empty()){ + tmp = q.front(); + q.pop(); + // ϰ + if(tmp.safe){ + isSafe = true; + return; + } + for(int i=0;i +using namespace std; +int main(){ + int n; + int a[100000+5]; + cin>>n; + for(int i=0;i>a[i]; + int left = 0; + int tmpleft = 0; + int right = n-1; + int max =0; + int tmpSum=0; + for(int i=0;i=0) + cout< +#include +#include +#include +#define MaxVertex 10005 +typedef int vertex; +typedef struct Node *AdjList; +struct Node{ + vertex Adjv; // ǰ± + AdjList Next; // һ +}; +AdjList G[MaxVertex]; +bool visit[MaxVertex]; // Ƿ +int N; // +int M; // +using namespace std; + +// ʼ״̬ +void InitVisit(){ + for(int i=1;i<=N;i++) + visit[i] = false; +} + +// ʼ +void Init(){ + vertex v1,v2; + AdjList NewNode; + cin>>N>>M; + // ʼ㣬 1N + for(int i=1;i<=N;i++){ + G[i] = (AdjList)malloc(sizeof(struct Node)); + G[i]->Adjv = i; + G[i]->Next = NULL; + } + // ʼ + for(int i=0;i>v1>>v2; + NewNode = (AdjList)malloc(sizeof(struct Node)); + NewNode->Adjv = v2; + NewNode->Next = G[v1]->Next; + G[v1]->Next = NewNode; + + NewNode = (AdjList)malloc(sizeof(struct Node)); + NewNode->Adjv = v1; + NewNode->Next = G[v2]->Next; + G[v2]->Next = NewNode; + } +} + +int BFS(vertex v){ + queue q; + vertex tmp; + int level = 0; + int last = v; // òһηʵĽ + int tail = v; // ÿڱĽ + AdjList node; + visit[v] = true; + int count = 1; // ͳƹϵ + q.push(v); + while(!q.empty()){ + tmp = q.front(); + q.pop(); + // G[i]һԼ± + node = G[tmp]->Next; + while(node){ + if(!visit[node->Adjv]){ + visit[node->Adjv] = true; + q.push(node->Adjv); + count++; + tail = node->Adjv; // ÿθ¸ý + } + node = node->Next; + } + // õǰһ + if(tmp == last){ + level++; // +1 + last = tail; // last + } + // ˽ + if(level==6) + break; + } + return count; +} + + +void output(double result,int i){ + printf("%d: %.2f%%\n",i,result); +} + +void SDS(){ + int count; + for(int i=1;i<=N;i++){ + // ÿγʼ + InitVisit(); + count = BFS(i); + output((100.0*count)/N,i); + } +} + + +int main(){ + Init(); + SDS(); + + return 0; +} diff --git a/编程作业/20.六度空间.exe b/编程作业/20.六度空间.exe new file mode 100644 index 0000000..ca60f3c Binary files /dev/null and b/编程作业/20.六度空间.exe differ diff --git a/编程作业/21.哈利·波特的考试.cpp b/编程作业/21.哈利·波特的考试.cpp new file mode 100644 index 0000000..b554ea5 --- /dev/null +++ b/编程作业/21.哈利·波特的考试.cpp @@ -0,0 +1,167 @@ +/*#include +#define MaxVertex 105 +#define INF 100000 +typedef int Vertex; +int G[MaxVertex][MaxVertex]; // ͼ +int N; // 㣩 +int M; // ߣ +int dist[MaxVertex][MaxVertex]; // +bool collected[MaxVertex][MaxVertex]; // ѡ״̬ +using namespace std; + +// ʼ +void build(){ + Vertex v1,v2; + int w; + cin>>N>>M; + for(Vertex i=1;i<=N;i++){ + for(Vertex j=1;j<=N;j++){ + G[i][j] = 0; // ʼͼ + dist[i][j] = INF; // ʼ + collected[i][j] = false; // ʼѡ״̬ + } + dist[i][0] = INF; // ⽫ÿԴһʼ INF + } + for(int i=0;i>v1>>v2>>w; + G[v1][v2] = w; + G[v2][v1] = w; + } +} + +// δѡеĶоСһ +Vertex FindMin(Vertex s){ + Vertex min = 0; + for(Vertex i = 1;i<=N;i++) + if(i!=s && dist[s][i] < dist[s][min] && !collected[s][i]) + min = i; + return min; +} + +int FindMax(Vertex s){ + int max = 0; + for(Vertex i = 1;i<=N;i++) + if(max < dist[s][i]) + max = dist[s][i]; + return max; +} + +// Դ룬ҸõΧ״̬ +void prepare(Vertex s){ + dist[s][s] = 0; + collected[s][s] = true; + for(Vertex i=1;i<=N;i++) + if(G[s][i]) + dist[s][i] = G[s][i]; +} + +int Dijkstra(Vertex s){ + prepare(s); // ׼ + while(1){ + Vertex v = FindMin(s); + if(!v) + break; + collected[s][v] = true; + for(Vertex w=1;w<=N;w++) + if(G[v][w] && !collected[s][w]) + if(dist[s][v] + G[v][w] < dist[s][w]) + dist[s][w] = dist[s][v] + G[v][w]; + } + // ҵԼѱﳤ + return FindMax(s); +} + +int main(){ + build(); + int min = INF; + int xb = 0; + int max; + for(Vertex s=1;s<=N;s++){ + max = Dijkstra(s); + if(max < min){ + min = max; + xb = s; + } + if(min==INF){ + cout<<0< +#define MaxVertex 105 +#define INF 100000 +typedef int Vertex; +int G[MaxVertex][MaxVertex]; // ͼ +int N; // 㣩 +int M; // ߣ +int dist[MaxVertex][MaxVertex]; // +using namespace std; + +// ʼ +void build(){ + Vertex v1,v2; + int w; + cin>>N>>M; + for(Vertex i=1;i<=N;i++) + for(Vertex j=1;j<=N;j++) + G[i][j] = INF; // ʼͼ + for(int i=0;i>v1>>v2>>w; + G[v1][v2] = w; + G[v2][v1] = w; + } +} + +void Floyd(){ + // ʼ dist + for(Vertex i=1;i<=N;i++) + for(Vertex j=1;j<=N;j++) + dist[i][j] = G[i][j]; + for(Vertex k=1;k<=N;k++) + for(Vertex i=1;i<=N;i++) + for(Vertex j=1;j<=N;j++) + if(dist[i][k] + dist[k][j] < dist[i][j]) + dist[i][j] = dist[i][k] + dist[k][j]; +} + +// ÿԴ㵽ֵ +int FindMax(Vertex s){ + int max = 0; + for(Vertex i=1;i<=N;i++) + if(s!=i && max < dist[s][i]) + max = dist[s][i]; + return max; +} + +// ÿԴ㵽ֵСľ +void FindMin(){ + int ItemMax; + int min = INF; + int animal; + for(Vertex i=1;i<=N;i++){ + ItemMax = FindMax(i); + if(ItemMax == INF){ + cout<<0< +#include +#define INF 100000 +#define MaxVertex 105 +typedef int Vertex; +int G[MaxVertex][MaxVertex]; // ͼ +int dist[MaxVertex]; // +int path[MaxVertex]; // · +bool collected[MaxVertex]; // ¼ +int N; // +int D; // һ +struct Node{ // λ + int hor; // + int ver; // +}; +Node pos[MaxVertex]; // λ +const double diameter=15; // ֱ +using namespace std; + +// Ȩͼ +// 0.жϰļ +// 1.жϸǷ +// 2.תΪͼ + +// +double getLen(int x1,int y1,int x2,int y2){ + return sqrt(pow(x1-x2,2.0)+pow(y1-y2,2.0)); +} +// жܷϰ +bool ashore(int x,int y){ + // ֱ㵱ǰ밶ߵľ + // (x,50),(x,-50),(50,y),(-50,y) ľ + if(abs(x-50)<=D || abs(x+50)<=D || abs(y+50)<=D || abs(y-50)<=D) + return true; + return false; +} + +// ȷǷȫ("ϰ") +void getSafe(){ + for(int i=0;i>N>>D; + int x,y; + // ¼λ + for(int i=0;i>x>>y; + pos[i].hor = x; + pos[i].ver = y; + } + // 007 㶥㣬Ҳ㶥 + // ʼͼ007ͨ G[N]ͨ G[N+1] + for(Vertex i=0;i<=N+1;i++){ + for(Vertex j=0;j<=N+1;j++) + G[i][j] = INF; // ʼ + dist[i] = INF; // ʼ + path[i] = -1; // ʼ· + collected[i] = false; // ʼ¼״̬ + } + // ʼ + // ͨ + getSafe(); + // 007ͨ + getJump(); + // ˴ͨ + getConn(); +} + + +// δ¼distСĵ +Vertex FindMin(Vertex s){ + int min = INF; + Vertex xb=-1; + for(Vertex i=0;i<=N+1;i++) + if(s!=i && !collected[i] && dist[i] < min){ + min = dist[i]; + xb = i; + } + return xb; +} + +// ʼԴϢ +void InitSource(Vertex s){ + dist[s] = 0; + collected[s] = true; + for(Vertex i=0;i<=N+1;i++) + if(G[s][i]!=INF){ + dist[i] = G[s][i]; + path[i] = s; + } +} + +void Dijkstra(Vertex s){ + InitSource(s); + while(1){ + Vertex v = FindMin(s); + if(v==-1) + break; + collected[v] = true; + for(Vertex w=0;w<=N+1;w++) + if(!collected[w] && G[v][w]!=INF) + if(dist[v] + G[v][w] < dist[w]){ + dist[w] = dist[v] + G[v][w]; + path[w] = v; + } + } +} + +// · +void outputPath(){ + // ͨ + + + if(dist[N+1] == INF){ + cout<<0< +#define MaxVertex 505 +#define INF 100000 +typedef int Vertex; +int N; // +int M; // +int S; // Source +int D; // Destination +int dist[MaxVertex]; // +int cost[MaxVertex]; // +bool collected[MaxVertex]; // ѡ +int value[MaxVertex][MaxVertex]; // շ +int G[MaxVertex][MaxVertex]; +using namespace std; + + +void build(){ + Vertex v1,v2,w1,w2; + cin>>N>>M>>S>>D; + for(Vertex i=0;i>v1>>v2>>w1>>w2; + G[v1][v2] = w1; + G[v2][v1] = w1; + value[v1][v2] = w2; + value[v2][v1] = w2; + } +} + +// ʼԴϢ +void InitSource(){ + dist[S] = 0; + collected[S] = true; + for(Vertex i=0;i +#include +#include +#define MaxVertex 1005 +typedef int Vertex; +using namespace std; +int N; // +int M; // +int parent[MaxVertex]; // 鼯 +struct Node{ + Vertex v1; + Vertex v2; + int weight; + // + bool operator < (const Node &a) const + { + return weight>a.weight; + } +}; +priority_queue q; // С +vector MST; // С +int sum; + +// ʼͼϢ +void build(){ + Vertex v1,v2; + int w; + cin>>N>>M; + for(int i=1;i<=N;i++){ + parent[i] = -1; + } + // ʼ + for(int i=0;i>v1>>v2>>w; + struct Node tmpE; + tmpE.v1 = v1; + tmpE.v2 = v2; + tmpE.weight = w; + q.push(tmpE); + } + sum = 0; +} + + +// ·ѹ +int Find(int x){ + if(parent[x] < 0) + return x; + else + return parent[x] = Find(parent[x]); +} + +// ȹ鲢 +void Union(int x1,int x2){ + x1 = Find(x1); + x2 = Find(x2); + if(parent[x1] < parent[x2]){ + parent[x1] += parent[x2]; + parent[x2] = x1; + }else{ + parent[x2] += parent[x1]; + parent[x1] = x2; + } +} + +void Kruskal(){ + while(MST.size()!=M-1 && !q.empty()){ + Node E = q.top(); // СѣȨС + q.pop(); + if(Find(E.v1) != Find(E.v2)){ // жǷͬһ + sum += E.weight; + Union(E.v1,E.v2); // + MST.push_back(E); + } + } +} + + +int main(){ + build(); + Kruskal(); + // ͼͨ + if(MST.size()==N-1) + cout< +#include +#include +#define MaxVertex 105 +#define INF -100000 +typedef int Vertex; +using namespace std; +int N; // +int M; // +int G[MaxVertex][MaxVertex]; +int Earliest[MaxVertex]; // ʱ +int Indegree[MaxVertex]; // + +// ʼͼ +void build(){ + Vertex v1,v2,w; + cin>>N>>M; + for(Vertex i=0;i>v1>>v2>>w; + G[v1][v2] = w; // ͼ + Indegree[v2]++; // +1 + } +} + +void TopSort(){ + int cnt = 0; + queue q; + // Ϊ0 + for(Vertex i=0;i +#include +#include +#define MaxVertex 105 +#define INF 100000 +typedef int Vertex; +using namespace std; +int N; // +int M; // +int G[MaxVertex][MaxVertex]; +int Earliest[MaxVertex]; // ʱ +int latest[MaxVertex]; // ʱ +int Indegree[MaxVertex]; // +int Outdegree[MaxVertex]; // +int Max; // ʱ + +// ʼͼ +void build(){ + Vertex v1,v2,w; + cin>>N>>M; + for(Vertex i=1;i<=N;i++) + for(Vertex j=1;j<=N;j++) + G[i][j] = INF; + for(int i=0;i>v1>>v2>>w; + G[v1][v2] = w; // ͼ + Indegree[v2]++; // +1 + Outdegree[v1]++; // +1 + } +} + +// Ƶõʱ +void GetLastest(){ + +} + +// ҵĿʱ +bool TopSort(){ + int cnt = 0; + queue q; + // Ϊ0 + for(Vertex i=1;i<=N;i++) + if(!Indegree[i]){ + q.push(i); + Earliest[i] = 0; + } + while(!q.empty()){ + Vertex v = q.front(); + q.pop(); + cnt++; + for(Vertex w=1;w<=N;w++) + if(G[v][w]!=INF){ + if(Earliest[w] < Earliest[v]+G[v][w]) //Χʱʱ + Earliest[w] = max(Earliest[w],Earliest[v]+G[v][w]); + if(--Indegree[w]==0) + q.push(w); + } + } + if(cnt!=N) + return false; + else{ + // Ҳֹһյ + Max = 0; + for(Vertex i=1;i<=N;i++) + if(Max < Earliest[i]) + Max = Earliest[i]; + return true; + } + +} + +int main(){ + build(); + if(!TopSort()) + cout<<0; + else{ + cout< +#include +#include +using namespace std; + +// ð +void Bubble_sort(long A[],int N){ + for(int i=0;i0;j--) + A[j] = A[j-1]; + A[j] = tmp; + } +} + +// ԭʼϣ +void shell_sort(long A[],int N){ + for(int D=N/2;D>0;D/=2){ + for(int p=D;p=D && tmp=D ǰΪҲ A[j-D]ѾԽ + A[k] = A[k-D]; + A[k] = tmp; + } + } +} + +// Hibbardϣ +void Hibbard_shell_sort(long A[],int N){ + int add[]={32767,16383,8191,4095,2047,1023,511,255,127,63,31,15,7,3,1,0}; + int i=0; + for(int D=add[i];D>0;D=add[++i]){ + for(int p=D;p=D && tmp=D ǰΪҲ A[j-D]ѾԽ + A[k] = A[k-D]; + A[k] = tmp; + } + } +} +// Sedgewickйϣ +void Sedgewick_shell_sort(long A[],int N){ + int add[]= {587521,260609,146305,64769,36289,16001,8929,3905,2161,929,505,209,109,41,19,5,1,0}; + int i=0; + for(int D=add[i];D>0;D=add[++i]){ + for(int p=D;p=D && tmp=0;i--) + PrecDown(A,i,N); + for(int i=N-1;i>0;i--){ + swap(A[0],A[i]); // ÿΰѵǰѸԪѡ + PrecDown(A,0,i); // ٴε + } +} + +/******************************/ + +/***************stlʼ***************/ +priority_queue,less > q; // һ + +void STL_Heap_sort(long A[],int N){ + // ݶ + for(int i=0;i=0;i--){ + A[i] = q.top(); + q.pop(); + } +} +/***************stl***************/ + +/***************ݹ鲢ʼ***************/ +/* +// 鲢ʵ +void Merge(long A[],long tmpA[],int L,int R,int RightEnd){ + // L = Ԫؿʼλ R = ұԪؿʼλ RightEnd = ұ߽յλ + int NumSize = RightEnd-L+1; // Ԫظ + int LeftEnd = R-1; // Ԫյλ + int tmp = L; // tmp 鿪ʼλ + while( L <= LeftEnd && R <= RightEnd ){ + if(A[L] <= A[R]) // СѡС + tmpA[tmp++] = A[L++]; + else + tmpA[tmp++] = A[R++]; + } + // Ҳû + while( L <= LeftEnd ) + tmpA[tmp++] = A[L++]; + // Ҳұû + while( R <= RightEnd) + tmpA[tmp++] = A[R++]; + // ٵ A ,tmpʱѾԽ磬Ҫȼ + for(int i=0;i pivot); + if(j <= i) + break; + swap(A[i],A[j]); + } + // Ԫںλ + swap(A[i],A[Right-1]); + QucikSort(A,Left,i-1); + QucikSort(A,i+1,Right); + }else // ò + Insertion_sort(A+Left,Right-Left+1); +} + +void Quick_sort(long A[],int N){ + QucikSort(A,0,N-1); +} + +/*******************************************************/ + +int main(){ + int N; + cin>>N; + long A[N]; + for(int i=0;i>A[i]; + Quick_sort(A,N); + for(int i=0;i +using namespace std; + +bool judge(int A[],int B[],int N){ + for(int i=0;i0 && tmp < A[j-1];j--) + A[j] = A[j-1]; + A[j] = tmp; + if(judge(A,B,N)){ // ˣһ + P++; + int tmp = A[P]; + int j = P; + for(;j>0 && tmp < A[j-1];j--) + A[j] = A[j-1]; + A[j] = tmp; + return false; + } + } + return true; +} + + +void Merge(int A[],int tmpA[],int L,int R,int RightEnd){ + // L = ʼλãR = ұʼλãRightEnd = ұյλ + int NumSize = RightEnd-L+1; // + int LeftEnd = R-1; // ֹλ + int tmp = L; + // + while(L <= LeftEnd && R <= RightEnd){ + if(A[L] <= A[R]) + tmpA[tmp++] = A[L++]; + else + tmpA[tmp++] = A[R++]; + } + // ʣ + while(L <= LeftEnd) + tmpA[tmp++] = A[L++]; + // ұʣ + while(R <= RightEnd) + tmpA[tmp++] = A[R++]; + // A + for(int i=0;i>N; + int A[N],tmpA[N]; + for(int i=0;i>A[i]; + tmpA[i] = A[i]; + } + int B[N]; + for(int i=0;i>B[i]; + // tmpA 鲢жǷǹ鲢 + if(!Merge_Sort(tmpA,B,N)){ + cout<<"Merge Sort"< +#include +using namespace std; +void output(int A[],int N); + +// жǷ +bool judge(int A[],int B[],int N){ + for(int i=0;i0 && tmp < A[k-1];k--) + A[k] = A[k-1]; + A[k] = tmp; + if(judge(A,B,N)){// ȣһ + p++; + int tmp = A[p]; + int k = p; + for(;k>0 && tmp < A[k-1];k--) + A[k] = A[k-1]; + A[k] = tmp; + return false; + } + } + return true; +} + +void PrecDown(int A[],int i,int N){ + int tmp = A[i]; // ȡõǰ"" + int parent,child; + for(parent=i;parent*2+1=0;i--) + PrecDown(A,i,N); + for(int i=N-1;i>0;i--){ + swap(A[0],A[i]); + PrecDown(A,0,i); + if(judge(A,B,N)){ // ȣһ + swap(A[0],A[--i]); + PrecDown(A,0,i); + return false; + } + } + return true; +} + +// +void output(int A[],int N){ + for(int i=0;i>N; + int A[N],tmpA[N]; + for(int i=0;i>A[i]; + tmpA[i] = A[i]; + } + int B[N]; + for(int i=0;i>B[i]; + if(!Heap_Sort(tmpA,B,N)){ // Ƕ + cout<<"Heap Sort"< +#include + +#define MAXSIZE 10 +#define NotFound 0 +typedef int ElementType; + +typedef int Position; +typedef struct LNode *List; +struct LNode { + ElementType Data[MAXSIZE]; + Position Last; /* ԱһԪصλ */ +}; + +Position BinarySearch( List L, ElementType X ); + +int main() +{ + List L; + ElementType X; + Position P; + + L->Data = int[]{1,3,5}; + L->Last = 3; + scanf("%d", &X); + P = BinarySearch( L, X ); + printf("%d\n", P); + + return 0; +} + +Position BinarySearch( List L, ElementType X ){ + ElementType left = 1; + ElementType right = L->Last; + while(leftData[center] < X){ //мֵX Ұ + left = center+1; + }else if(X < L->Data[center]){ //мֵСX + right = center-1; + }else //ҵˣֱӷ + return center; + } + return NotFound; +} diff --git a/编程作业/30.统计工龄.cpp b/编程作业/30.统计工龄.cpp new file mode 100644 index 0000000..1fbe3b4 --- /dev/null +++ b/编程作业/30.统计工龄.cpp @@ -0,0 +1,23 @@ +#include +using namespace std; + +void bucket_sort(int A[],int N){ + int count[55]; + for(int i=0;i<55;i++) + count[i] = 0; + for(int i=0;i>N; + int A[N]; + for(int i=0;i>A[i]; + bucket_sort(A,N); + return 0; +} diff --git a/编程作业/30.统计工龄.exe b/编程作业/30.统计工龄.exe new file mode 100644 index 0000000..daa6301 Binary files /dev/null and b/编程作业/30.统计工龄.exe differ diff --git a/编程作业/31.PAT Judge.cpp b/编程作业/31.PAT Judge.cpp new file mode 100644 index 0000000..afb6fd6 --- /dev/null +++ b/编程作业/31.PAT Judge.cpp @@ -0,0 +1,95 @@ +#include +#include +#include + +const int INIT = -2; // ʼ +const int NOPASS = -1; // δͨ +const int MAXID = 10005; //ID +using namespace std; + +struct info{ + int id; // id + int ques[6]; // ÿ÷ + int sum; // ܷ + int solved; // +}; + +// sort +bool cmp(info a,info b){ + if(a.sum != b.sum) + return a.sum>b.sum; + if(a.solved != b.solved) + return a.solved > b.solved; + return a.id < b.id; +} + + +int main(){ + int N; // ʹ + int K; // + int M; // ύ + scanf("%d %d %d",&N,&K,&M); + vector user(N+1); // ʹϢ + int Full[K+1]; // ¼ܷ ,a[i] ʾiķ + for(int i=1;i<=K;i++) + scanf("%d",&Full[i]); + // ʼϢ + for(int i=1;i= 0 && user[use].id == MAXID){ + listSum++; + user[use].id = use; + } + // и³ɸ + if(user[use].ques[question] < score){ + user[use].ques[question] = score; + } + } + for(int i=1;i +#include +using namespace std; +int main(){ + int N; + cin>>N; + int tmp; + int sum = 0; + vector A; // N + vector flag; + for(int i=0;i>tmp; + A.push_back(tmp); + flag.push_back(false); + } + for(int i=0;i +#include +#include +#include +#define MAXTABLESIZE 1000000 +using namespace std; +typedef string ElementType; +typedef struct LNode *List; +struct LNode{ // + ElementType number; // 绰 + int Count; // + List Next; +}; + +typedef struct HashTbl *HashTable; +struct HashTbl{ // ϣ + int TableSize; // ϣС + List Heads; // ͷ +}; + +// ϣ +int Hash(int key,int p){ + return key%p; +} + +int NextPrime(int N){ + int p = (N%2)?N+2:N+1; + int i; + while(p <= MAXTABLESIZE){ + for(i = (int)sqrt(1.0*p);i>2;i--) + if(!(p%i)) // + break; + if(i==2) // ҵ + break; + p += 2; // ȥ¸ + } + return p; +} + +// ʼϣ +HashTable CreateTable(int TableSize){ + HashTable H; + H = (HashTable)malloc(sizeof(struct HashTbl)); + H->TableSize = NextPrime(TableSize); + + // ṹкַ붯ַ̬ռ + H->Heads = new LNode[H->TableSize]; + for(int i=0;iTableSize;i++){ + H->Heads[i].Next = NULL; + H->Heads[i].Count = 0; + } + return H; +} + +// +List Find(HashTable H,ElementType key){ + List p; + int pos; + // Ե绰λɢ + pos = Hash(atoi(key.substr(6,5).c_str()),H->TableSize); + + p = H->Heads[pos].Next; + while(p && key != p->number) + p = p->Next; + return p; +} + +// +bool Insert(HashTable H,ElementType key){ + List p,NewCell; + int pos; + p = Find(H,key); + if(!p){ // pΪ + + NewCell = new LNode(); + NewCell->number = key; + NewCell->Count = 1; + pos = Hash(atoi(key.substr(6,5).c_str()),H->TableSize); // ҵɢеַ + + // ½뵽ͷ + NewCell->Next = H->Heads[pos].Next; + H->Heads[pos].Next = NewCell; + return true; + }else{ + p->Count++; // Ѿڣ+1 + return false; + } +} + +void ScanAndOutput(HashTable H){ + List p; + int MaxCnt=0; // ͨ + string Minphone; // С + int PCnt=0; // + // ɨ + for(int i=0;iTableSize;i++){ + p = H->Heads[i].Next; + while(p){ + if(MaxCnt < p->Count){ + MaxCnt = p->Count; + Minphone = p->number; + PCnt = 1; + }else if( p->Count == MaxCnt ){ + if(p->number < Minphone ) + Minphone = p->number; + PCnt++; + } + p = p->Next; + + } + } + cout<>N; + H = CreateTable(2*N); + for(int i=0;i>key; Insert(H,key); + cin>>key; Insert(H,key); + } + ScanAndOutput(H); + return 0; +} diff --git a/编程作业/33.电话聊天狂人.exe b/编程作业/33.电话聊天狂人.exe new file mode 100644 index 0000000..14f8239 Binary files /dev/null and b/编程作业/33.电话聊天狂人.exe differ diff --git a/编程作业/34.Hashing.cpp b/编程作业/34.Hashing.cpp new file mode 100644 index 0000000..d8651ee --- /dev/null +++ b/编程作业/34.Hashing.cpp @@ -0,0 +1,102 @@ +#include +#include +#include +#define MAXTABLESIZE 100000 +#define NOTFIND -1 +typedef int ElementType; +typedef enum{ // Ԫ״̬ + Legitimate,Empty +} EntryType; // ֱӦкϷԪغпλ +typedef struct HashEntry Cell; +struct HashEntry{ // Ԫ + ElementType data; // ֵ + EntryType info; // ״̬ +}; +typedef struct HashTbl *HashTable; +struct HashTbl{ // ϣ + int TableSize; // С + Cell *Cells; // +}; +using namespace std; + +// ϣ +int Hash(int key,int p){ + return key%p; +} + +// һ +int NextPrime(int N){ + int p = N%2?N:N+1; + int i; + if(N<=2) + return 2; + else if(N<=3) + return 3; + while(p <= MAXTABLESIZE){ + for(i=(int)sqrt(p);i>2;i--) + if(!(p%i)) // + break; + if(i==2) // ҵ + break; + p += 2; + } + return p; +} + +// ϣ +HashTable CreateTable(int TableSize){ + HashTable H; + H = (HashTable)malloc(sizeof(struct HashTbl)); + H->TableSize = NextPrime(TableSize); + H->Cells = (Cell *)malloc(sizeof(struct HashEntry)*H->TableSize); + for(int i=0;iTableSize;i++) + H->Cells[i].info = Empty; + return H; +} + +// +int Find(HashTable H,ElementType key){ + int NewPos,CurrentPos; + int CNum = 0; // ¼ͻ + CurrentPos = NewPos = Hash(key,H->TableSize); + // ǰ״̬Ϊգһֱȣһֱ + while(H->Cells[NewPos].info != Empty && H->Cells[NewPos].data != key){ + CNum++; + NewPos = (CurrentPos + CNum*CNum)%H->TableSize; + if(CNum == H->TableSize/2) // ûҵ + return NOTFIND; + } + return NewPos; +} + +int Insert(HashTable H,ElementType key){ + int pos; + pos = Find(H,key); + if(pos==NOTFIND) // ûҵ + return NOTFIND; + else if(H->Cells[pos].info != Legitimate){ + H->Cells[pos].info = Legitimate; + H->Cells[pos].data = key; + } + return pos; +} + +int main(){ + HashTable H; + int M,N; + int key; + cin>>M>>N; + H = CreateTable(M); + for(int i=0;i>key; + int pos = Insert(H,key); + if(i) + cout<<" "; + if(pos==NOTFIND) + cout<<"-"; + else + cout< +#include +#include +#include +#define MAXTABLESIZE 1000000 +using namespace std; +typedef struct Node Cell; +struct Node{ + bool exist; + string acc; // ˺ + string pas; // +}; + +typedef struct HashTBL *HashTable; +struct HashTBL{ + int TableSize; + Cell *users; +}; + +int Hash(int key,int p){ + return key%p; +} + +int NextPrime(int N){ + int p = N%2?N+2:N+1; + int i; + while(p <= MAXTABLESIZE) { + for(i=(int)sqrt(p);i>2;i--) + if(!(p%i)) + break; + if(i==2 && p%4==3) // ֤ƽ̽ⶼҵ + break; + p +=2; + } + return p; +} + +// ʼ +HashTable Create(int TableSize){ + HashTable H; + H = (HashTable)malloc(sizeof(struct HashTBL)); + H->TableSize = NextPrime(TableSize); + H->users = new Node[H->TableSize]; + for(int i=0;iTableSize;i++) + H->users[i].exist = false; + return H; +} + +int Find(HashTable H,string key){ + int NewPos,CurrentPos; + int CNum = 0; + NewPos = CurrentPos = Hash(atoi(key.c_str()),H->TableSize); + while(H->users[NewPos].exist && H->users[NewPos].acc != key){ + if(++CNum %2){ + NewPos =( CurrentPos + (CNum+1)/2*(CNum+1)/2 )%H->TableSize; + }else{ + NewPos = CurrentPos - CNum/2*CNum/2; + while(NewPos < 0) + NewPos += H->TableSize; + } + } + return NewPos; +} + +void Insert(HashTable H,string key,string pas,char flag){ + int pos; + pos = Find(H,key); + if(H->users[pos].exist){ // Ѿ + if(flag == 'L'){ // ½ + if(pas == H->users[pos].pas) // ȷ½ɹ + cout<<"Login: OK"<users[pos].exist = true; + H->users[pos].pas = pas; + H->users[pos].acc = key; + } + } +} + +int main(){ + HashTable H; + int N; + char command; + string account; + string password; + cin>>N; + H = Create(N); + for(int i=0;i>command>>account>>password; + Insert(H,account,password,command); + } + return 0; +}*/ + + +#include +#include +#include +#include +using namespace std; +int main(){ + int N; + char command; + string qq; + string ps; + map m; + scanf("%d",&N); + for(int i=0;i>qq>>ps; + if(command == 'N'){ // û + if(m.find(qq)!=m.end()) // qqѾ + printf("ERROR: Exist"); + else{ // עɹ + printf("New: OK"); + m[qq] = ps; + } + }else if(command == 'L'){ // û + if(m.find(qq)==m.end()) // ûҵ + printf("ERROR: Not Exist"); + else if(m[qq] == ps) // ȷ + printf("Login: OK"); + else + printf("ERROR: Wrong PW"); + } + printf("\n"); + } + return 0; +} diff --git a/编程作业/35.QQ帐户的申请与登陆.exe b/编程作业/35.QQ帐户的申请与登陆.exe new file mode 100644 index 0000000..e0f9394 Binary files /dev/null and b/编程作业/35.QQ帐户的申请与登陆.exe differ diff --git a/编程作业/36.Hashing - Hard Version.cpp b/编程作业/36.Hashing - Hard Version.cpp new file mode 100644 index 0000000..ef8c05a --- /dev/null +++ b/编程作业/36.Hashing - Hard Version.cpp @@ -0,0 +1,73 @@ +#include +#include +#include +#include +#define INF -100000 +#define MaxVertex 1005 +int G[MaxVertex][MaxVertex]; +int Indegree[MaxVertex]; // +int value[MaxVertex]; // ֵ +int N; // ֵ +using namespace std; +priority_queue,greater > q; // С +map m; + +// +void Insert(){ + for(int i=0;i(value[i],i)); // ±ֵӳϵ + int remainder = value[i]%N; // + if(remainder==i) + Indegree[i] = 0; + else if(remainder != i){ + Indegree[i] = (i - remainder < 0)?(i - remainder + N):(i - remainder); + for(int j=remainder;j!=i;j=(j+1)%N) + G[j][i] = 1; + } + } + } +} + +// ʼͼ +void build(){ + for(int i=0;i>N; + int i=0; + for(int i=0;i>value[i]; + build(); + + TopSort(); + return 0; +} diff --git a/编程作业/36.Hashing - Hard Version.exe b/编程作业/36.Hashing - Hard Version.exe new file mode 100644 index 0000000..bae62fd Binary files /dev/null and b/编程作业/36.Hashing - Hard Version.exe differ diff --git a/编程作业/37.KMP 串的模式匹配.cpp b/编程作业/37.KMP 串的模式匹配.cpp new file mode 100644 index 0000000..875a4f9 --- /dev/null +++ b/编程作业/37.KMP 串的模式匹配.cpp @@ -0,0 +1,86 @@ +/*#include +#include +#include +#include +typedef int Position; +#define NotFound -1 +using namespace std; + +void BuildMatch(char *pattern,int *match){ + int i,j; + int m = strlen(pattern); + match[0] = -1; + for(j=1;j=0) && (pattern[i+1] != pattern[j])) + i = match[i]; + if(pattern[i+1] == pattern[j]) + match[j] = i+1; + else + match[j] = -1; + } +} + +Position KMP(char* string,char *pattern){ + int n = strlen(string); + int m = strlen(pattern); + int s,p; + if(n < m) + return NotFound; + int *match = (int *)malloc(sizeof(int) * m); + BuildMatch(pattern,match); + s = p = 0; + while(s < n && p < m){ + if(string[s] == pattern[p]){ + s++; + p++; + }else if(p>0) + p = match[p-1]+1; + else + s++; + } + return (p == m) ? (s-m) : NotFound; +} + +int main(){ + char *string; + char *pattern; + Position p; + int N; + int i; + string = (char *)malloc(sizeof(char) * 100005); + pattern = (char *)malloc(sizeof(char) * 10005); + scanf("%s %d",string,&N); + for(i=0;i +#include +#include +typedef char *Position; +#define NotFound NULL +using namespace std; + +int main(){ + string str; + string pat; + Position p; + int N; + cin>>str>>N; + for(int i=0;i>pat; + p = strstr(str.c_str(),pat.c_str()); + if(p == NotFound) + cout<<"Not Found"< +#include + +typedef int ElementType; +typedef struct Node *PtrToNode; +struct Node { + ElementType Data; + PtrToNode Next; +}; +typedef PtrToNode List; + +List Read(); /* ϸڴ˲ */ +void Print( List L ); /* ϸڴ˲NULL */ + +List Merge( List L1, List L2 ); + +int main() +{ + List L1, L2, L; + L1 = Read(); + L2 = Read(); + L = Merge(L1, L2); + Print(L); + Print(L1); + Print(L2); + return 0; +} + +List Merge( List L1, List L2 ){ + List L=(List)malloc(sizeof(struct Node)); + List head = L; + List tmpL1 = L1->Next; + List tmpL2 = L2->Next; + while(tmpL1 && tmpL2){ + if(tmpL1->Data < tmpL2->Data){ + L->Next=tmpL1; + tmpL1=tmpL1->Next; + }else{ + L->Next=tmpL2; + tmpL2=tmpL2->Next; + } + L=L->Next; + } + if(tmpL1) + L->Next=tmpL1; + if(tmpL2) + L->Next=tmpL2; + L1->Next = NULL; + L2->Next = NULL; + return head; +} + + + + + + + + + + + + + + + + + + diff --git a/编程作业/4.两个有序链表序列的合并.exe b/编程作业/4.两个有序链表序列的合并.exe new file mode 100644 index 0000000..7774d70 Binary files /dev/null and b/编程作业/4.两个有序链表序列的合并.exe differ diff --git a/编程作业/5.一元多项式的乘法与加法运算.cpp b/编程作业/5.一元多项式的乘法与加法运算.cpp new file mode 100644 index 0000000..0256c74 --- /dev/null +++ b/编程作业/5.一元多项式的乘法与加法运算.cpp @@ -0,0 +1,108 @@ +#include +#include +typedef struct Node *List; +struct Node{ + List Next; + int z; // ָ + int x; // ϵ +}; + +// +List Read(){ + List L = (List)malloc(sizeof(struct Node)); + List head = L; + int n; + int i=0; + scanf("%d",&n); + for(i=0;ix = x; + t->z = z; + L->Next = t; + L = L->Next; + } + L->Next = NULL; + return head->Next; +} + +// ʵּӷ +List addition(List L1,List L2){ + List tmpL1 = L1; + List tmpL2 = L2; + List add=(List)malloc(sizeof(struct Node)); + add->Next = NULL; + List head = add; + List t; + while(tmpL1 && tmpL2){ + t = (List)malloc(sizeof(struct Node)); + if(tmpL1->z == tmpL2->z){ //ָȣϵ + t->x = tmpL1->x + tmpL2->x; + t->z = tmpL1->z; + tmpL1 = tmpL1->Next; + tmpL2 = tmpL2->Next; + }else if(tmpL1->z < tmpL2->z){ // L2 ָ󣬰 L2 + t->x = tmpL2->x; + t->z = tmpL2->z; + tmpL2 = tmpL2->Next; + }else if(tmpL2->z < tmpL1->z){ // L1 ָ󣬰 L1 + t->x = tmpL1->x; + t->z = tmpL1->z; + tmpL1 = tmpL1->Next; + } + add->Next = t; + add = add->Next; + } + if(tmpL1) // L1 NULLʣ½ + add->Next = tmpL1; + else if(tmpL2) // ͬ + add->Next = tmpL2; + return head->Next; // head ֵָֻ +} + +// ʵֳ˷ +List multiplication(List L1,List L2){ + List tmpL1 = L1; + List tmpL2 = L2; + List mul=(List)malloc(sizeof(struct Node)); + mul->Next = NULL; + List head = mul; + List t; + for(;tmpL1;tmpL1=tmpL1->Next) + for(tmpL2 = L2;tmpL2;tmpL2=tmpL2->Next){ + t = (List)malloc(sizeof(struct Node)); + t->x = tmpL1->x * tmpL2->x; // ϵ + t->z = tmpL1->z + tmpL2->z; // ָ + t->Next = NULL; + head = addition(t,mul); // ֮ǰѾźĽ + mul = head; // ȷͷ + } + return head; +} +void Print(List L){ + List t = L; + int flag = 1; + for(;t;t = t->Next){ + if(!flag && t->x) //ƿո + printf(" "); + if(t->x){ // ϵΪ 0 + printf("%d %d",t->x,t->z); + flag =0; + } + } + if(flag) + printf("0 0"); + printf("\n"); +} +int main(){ + List L1,L2,add,mul; + L1 = Read(); + L2 = Read(); + add = addition(L1,L2); + mul = multiplication(L1,L2); + Print(mul); + Print(add); + return 0; +} diff --git a/编程作业/5.一元多项式的乘法与加法运算.exe b/编程作业/5.一元多项式的乘法与加法运算.exe new file mode 100644 index 0000000..744544d Binary files /dev/null and b/编程作业/5.一元多项式的乘法与加法运算.exe differ diff --git a/编程作业/6.Reversing Linked List.cpp b/编程作业/6.Reversing Linked List.cpp new file mode 100644 index 0000000..1cc3229 --- /dev/null +++ b/编程作业/6.Reversing Linked List.cpp @@ -0,0 +1,33 @@ +#include +#include +#define MaxSize 100005 +using namespace std; +int main(){ + int Data[MaxSize]; + int Next[MaxSize]; + int list[MaxSize]; + int FirstAdd,N,K; + scanf("%d %d %d",&FirstAdd,&N,&K); + for(int i=0;i +#include +#include +using namespace std; +int main(){ + int m,n,k; + cin>>m>>n>>k; + for(int i=0;i s; + vector v(n + 1); + for(int j=1;j<=n;j++) + cin>>v[j]; + int cur=1; + for(int j=1;j<=n;j++){ + s.push(j); + if(s.size() > m) + break; + while(!s.empty() && s.top()==v[cur]){ + s.pop(); + cur++; + } + } + if(cur == n+1) + cout<<"YES"< +#include +#define null -1 +using namespace std; +struct TreeNode{ + char data; // ֵ + int left; // ± + int right; // ± +}T1[10],T2[10]; +// ظ +int create(struct TreeNode T[]){ + int n; + int root = 0; + char left,right; + cin>>n; + if(!n) + return null; + for(int i=0;i>T[i].data>>left>>right; + if(left=='-') + T[i].left = null; + else{ + T[i].left = left-'0'; + root -= T[i].left; + } + if(right=='-') + T[i].right = null; + else{ + T[i].right = right-'0'; + root -= T[i].right; + } + // 0 ۼӵ n-1 + root +=i; + } + return root; +} +// жǷͬ +bool judge(int R1,int R2){ + if(R1 == null && R2 == null) // Ϊ + return true; + if(R1 == null && R2 != null || R1 != null && R2 == null) // һΪգһΪ + return false; + if(T1[R1].data != T2[R2].data) // ֵͬ + return false; + if((T1[R1].left != null && T2[R2].left != null )&&(T1[T1[R1].left].data == T2[T2[R2].left].data)) // ӲΪֵ + return judge(T1[R1].left,T2[R2].left) && judge(T1[R1].right,T2[R2].right); + else // ӲΪֵ ijһΪ + return judge(T1[R1].right,T2[R2].left) && judge(T1[R1].left,T2[R2].right); +} +int main(){ + int R1,R2; + R1 = create(T1); + R2 = create(T2); + if(judge(R1,R2)) + cout<<"Yes"; + else + cout<<"No"; + return 0; +} diff --git a/编程作业/8.树的同构.exe b/编程作业/8.树的同构.exe new file mode 100644 index 0000000..8013c7f Binary files /dev/null and b/编程作业/8.树的同构.exe differ diff --git a/编程作业/9.List Leaves.cpp b/编程作业/9.List Leaves.cpp new file mode 100644 index 0000000..fedcdcf --- /dev/null +++ b/编程作业/9.List Leaves.cpp @@ -0,0 +1,71 @@ +#include +#include +#define null -1 +using namespace std; +struct TreeNode{ + int data; // ֵ + int left; // ӽ + int right; // Ҷӽ +}T[10]; +// +int create(){ + int n; + int root = 0; + char left,right; + cin>>n; + if(!n) + return null; + for(int i=0;i>left>>right; + if(left=='-') + T[i].left = null; + else{ + T[i].left = left-'0'; + root -= T[i].left; + } + if(right=='-') + T[i].right = null; + else{ + T[i].right = right-'0'; + root -= T[i].right; + } + root +=i; + } + return root; +} +// +void LevelorderTraversal(int root){ + // + queue q; + struct TreeNode t; + bool flag = false; + + // ΪգֱӴӡ + if(root == null){ + cout<<"-"; + return; + } + q.push(T[root]); + while(!q.empty()){ + t = q.front(); // õǰԪ + q.pop(); // + if(t.left == null && t.right == null){ // ΪҶӽ + if(flag) + cout<<" "; + else + flag = true; + cout<