diff --git "a/2224020150/chapter10/\345\211\215k\344\270\252\351\253\230\351\242\221\345\205\203\347\264\240.c" "b/2224020150/chapter10/\345\211\215k\344\270\252\351\253\230\351\242\221\345\205\203\347\264\240.c" new file mode 100644 index 0000000000000000000000000000000000000000..aed55ee14da0c7b28c87306f47dfb58e9a09811a --- /dev/null +++ "b/2224020150/chapter10/\345\211\215k\344\270\252\351\253\230\351\242\221\345\205\203\347\264\240.c" @@ -0,0 +1,63 @@ +#include +#define MAX_SIZE 100 + +// 定义结构体存储每个元素及其对应的次数 +typedef struct { + int element; + int count; +} ElementCountPair; + +void findTopKFrequentElements(int arr[], int n, int k) { + // 创建一个包含所有不同元素的hash表 + ElementCountPair hashTable[MAX_SIZE]; + + for (int i = 0; i < MAX_SIZE; ++i) { + hashTable[i].element = -1; + hashTable[i].count = 0; + } + + // 统计每个元素在数组中出现的次数并更新到hash表中 + for (int i = 0; i < n; ++i) { + int index = abs(arr[i]) % MAX_SIZE; // 使用取模运算确保索引值在合理范围内 + + while (hashTable[index].element != -1 && hashTable[index].element != arr[i] && hashTable[index].count > 0) { + if ((++index) == MAX_SIZE) { + index = 0; + } + } + + if (hashTable[index].element == -1 || hashTable[index].element == arr[i]) { + hashTable[index].element = arr[i]; + hashTable[index].count++; + } else { + printf("Error: Hash table is full.\n"); + return; + } + } + + // 将hash表按照元素出现次数从多到少进行排序 + for (int i = 0; i < MAX_SIZE-1; ++i) { + for (int j = 0; j < MAX_SIZE-i-1; ++j) { + if (hashTable[j+1].count > hashTable[j].count) { + ElementCountPair temp = hashTable[j]; + hashTable[j] = hashTable[j+1]; + hashTable[j+1] = temp; + } + } + } + + // 输出前k个高频元素 + for (int i = 0; i < k; ++i) { + printf("%d ", hashTable[i].element); + } +} + +int main() { + int arr[] = {-2, 3, 4, -5, 6, 7, 8, 9, 10}; + int n = sizeof(arr)/sizeof(arr[0]); + int k = 3; + + findTopKFrequentElements(arr, n, k); + + return 0; +} \ No newline at end of file diff --git "a/2224020150/chapter10/\346\214\211\345\245\207\345\201\266\346\216\222\345\272\217\346\225\260\347\273\2042.c" "b/2224020150/chapter10/\346\214\211\345\245\207\345\201\266\346\216\222\345\272\217\346\225\260\347\273\2042.c" new file mode 100644 index 0000000000000000000000000000000000000000..4f1fe48c61aeec59e2e7b4ffa13ed4ff4ff464f2 --- /dev/null +++ "b/2224020150/chapter10/\346\214\211\345\245\207\345\201\266\346\216\222\345\272\217\346\225\260\347\273\2042.c" @@ -0,0 +1,18 @@ + int* sortArrayByParityII(int* A, int ASize, int* returnSize){ + int *ret = (int *)malloc(sizeof(int)* ASize); + int i, j, k; + j = 0; + k = 1; + for(i=0; i +#include + +// 定义链表节点结构体 +struct ListNode { + int val; + struct ListNode *next; +}; + +// 创建新的链表节点 +struct ListNode* createListNode(int value) { + struct ListNode* newNode = (struct ListNode*)malloc(sizeof(struct ListNode)); + if (!newNode) exit(-1); // 内存分配失败时退出程序 + + newNode->val = value; + newNode->next = NULL; + + return newNode; +} + +// 将两个有序链表合并为一个有序链表 +struct ListNode* mergeTwoSortedLists(struct ListNode* l1, struct ListNode* l2) { + if (!l1 && !l2) return NULL; // 如果两个链表都为空则返回NULL + else if (!l1) return l2; // 如果第一个链表为空则直接返回第二个链表 + else if (!l2) return l1; // 如果第二个链表为空则直接返回第一个链表 + + struct ListNode* dummyHead = createListNode(0); // 创建虚拟头节点 + struct ListNode* curr = dummyHead; // 当前指向dummyHead的指针 + + while (l1 != NULL && l2 != NULL) { + if (l1->val <= l2->val) { + curr->next = l1; + l1 = l1->next; + } else { + curr->next = l2; + l2 = l2->next; + } + + curr = curr->next; + } + + if (l1 == NULL) { + curr->next = l2; + } else { + curr->next = l1; + } + + return dummyHead->next; +} + +// 使用归并排序算法对链表进行排序 +struct ListNode* sortLinkedList(struct ListNode* head) { + if (!head || !head->next) return head; // 若链表为空或只包含一个元素,则无需排序 + + struct ListNode* slow = head; // 设置slow指针作为中间位置的标志 + struct ListNode* fast = head; // 设置fast指针作为快速移动的指针 + + while (fast->next && fast->next->next) { + slow = slow->next; + fast = fast->next->next; + } + + struct ListNode* mid = slow->next; // 断开链表成左右两部分 + slow->next = NULL; + + struct ListNode* left = sortLinkedList(head); // 对左边子链表进行排序 + struct ListNode* right = sortLinkedList(mid); // 对右边子链表进行排序 + + return mergeTwoSortedLists(left, right); // 合并已经排好序的左右两个子链表 +} + +int main() { + // 创建原始链表 [4 -> 3 -> 2 -> 1] + struct ListNode* node1 = createListNode(4); + struct ListNode* node2 = createListNode(3); + struct ListNode* node3 = createListNode(2); + struct ListNode* node4 = createListNode(1); + + node1->next = node2; + node2->next = node3; + node3->next = node4; + + printf("Before sorting:\n"); + printLinkedList(node1); + + struct List \ No newline at end of file diff --git "a/2224020150/chapter10/\346\234\200\345\260\217\347\232\204k\344\270\252\346\225\260.c" "b/2224020150/chapter10/\346\234\200\345\260\217\347\232\204k\344\270\252\346\225\260.c" new file mode 100644 index 0000000000000000000000000000000000000000..50b1eadfca848b2e4f8e21977b10ccda690fde79 --- /dev/null +++ "b/2224020150/chapter10/\346\234\200\345\260\217\347\232\204k\344\270\252\346\225\260.c" @@ -0,0 +1,93 @@ +#include +#include +int partition(int arr[],int low,int high) +{ + int val = arr[low]; + while(low < high) + { + while(low < high && arr[high] >= val) + { + high--; + } + arr[low] = arr[high]; + while(low < high && arr[low] <= val) + { + low++; + } + arr[high] = arr[low]; + } + arr[low] = val; + return low; +} +//获取最小的k个数 +void GetLeastNumbers(int input[],int n,int output[],int k) +{ + if(input == NULL || output == NULL || n<=0 || k<=0 || k > n) + { + return; + } + int low = 0,high = n-1; + int index = partition(input,low,high); + while(index != k-1) + { + if(index > k-1) + { + high = index - 1; + index = partition(input,low,high); + }else + { + low = index+1; + index = partition(input,low,high); + } + } + for(int i = 0; i < k; i++) + { + output[i] = input[i]; + } +} +void QuickSort(int arr[],int low,int high) +{ + if(arr == NULL || low >= high) + { + return; + } + int index = partition(arr,low,high); + QuickSort(arr,low,index-1); + QuickSort(arr,index+1,high); +} +int main() +{ + int n,k; + int output[200000]; + while(scanf("%d %d",&n,&k) != EOF) + { + if(n <= 0 || k <= 0 || k > n) + { + continue; + } + int *arr = (int*)malloc(sizeof(int)*n); + if(!arr) + { + exit(-1); + } + for(int i = 0; i < n; i++) + { + scanf("%d",arr+i); + } + GetLeastNumbers(arr,n,output,k); + QuickSort(output,0,k-1); + for(int j = 0; j < k; j++) + { + if(j == k-1) + { + printf("%d\n",output[j]); + }else + { + printf("%d ",output[j]); + } + + } + free(arr); + } + return 0; +} \ No newline at end of file diff --git "a/2224020150/chapter10/\346\234\200\346\216\245\350\277\221\345\216\237\347\202\271\347\232\204k\344\270\252\347\202\271.c" "b/2224020150/chapter10/\346\234\200\346\216\245\350\277\221\345\216\237\347\202\271\347\232\204k\344\270\252\347\202\271.c" new file mode 100644 index 0000000000000000000000000000000000000000..270414e523a43edf1e7d38ef863ccb1680da0c41 --- /dev/null +++ "b/2224020150/chapter10/\346\234\200\346\216\245\350\277\221\345\216\237\347\202\271\347\232\204k\344\270\252\347\202\271.c" @@ -0,0 +1,20 @@ +int cmp(const void *a, const void *b){ + return (*(int **)a)[0] * (*(int **)a)[0] + (*(int **)a)[1]*(*(int **)a)[1] - + (*(int **)b)[0] * (*(int **)b)[0] - (*(int **)b)[1]*(*(int **)b)[1]; +} + +int** kClosest(int** points, int pointsSize, int* pointsColSize, int K, int* returnSize, int** returnColumnSizes){ + qsort(points, pointsSize, sizeof(int *), cmp); + + int *rcs = (int *)malloc(sizeof(int) * K); + int i; + for(i=0; i +#include +#define N 5 +int main() +{ + char a[N][80],b[80];//定义一个二维数组来存放字符串 + int min=0;//存放最小字符串所在的位置 + for(int i=0;i<5;i++) + { + scanf("%s",&a[i]); //录入字符串 + } + for(int i=0;i<5;i++)//选择排序 将这几个字符串由小到大排列 + { + min=i; + for(int j=i+1;j<5;j++) + { + if(strcmp(a[min],a[j])>0) + { + min=j;//找出本次循环最小的一个 + } + } + if(min!=i)//放到开头 + { + strcpy(b,a[min]); + strcpy(a[min],a[i]); + strcpy(a[i],b); + } + } + printf("After sorted:\n"); + for(int i=0;i<5;i++) + { + printf("%s\n",&a[i]); + } +} diff --git "a/2224020150/chapter2/\344\270\244\344\270\244\344\272\244\346\215\242\351\223\276\350\241\250\344\270\255\347\232\204\347\273\223\347\202\271.c" "b/2224020150/chapter2/\344\270\244\344\270\244\344\272\244\346\215\242\351\223\276\350\241\250\344\270\255\347\232\204\347\273\223\347\202\271.c" new file mode 100644 index 0000000000000000000000000000000000000000..ebb579f8d6b044a3968a17e8a08f9c1304ebd20c --- /dev/null +++ "b/2224020150/chapter2/\344\270\244\344\270\244\344\272\244\346\215\242\351\223\276\350\241\250\344\270\255\347\232\204\347\273\223\347\202\271.c" @@ -0,0 +1,34 @@ +struct ListNode { + int val; + struct ListNode *next; + }; +struct ListNode* swapPairs(struct ListNode* head){ + struct ListNode *new = head; + struct ListNode *pre = NULL; + struct ListNode *cur = NULL; + + // 如果链表本身就是空的,那么就直接返回 + if (head == NULL) { + return head; + } + struct ListNode *result = head; + if (head->next != NULL) { + result = head->next; + } + + struct ListNode *tmp = NULL; + + while (new != NULL && new->next != NULL) { + // 设置 pre 和 cur 的位置 + pre = new; + cur = new->next; + new = new->next->next; + // 交换 + pre->next = cur->next; + cur->next = pre; + if (tmp != NULL) tmp->next = cur; + tmp = pre; + } + + return result; +} \ No newline at end of file diff --git "a/2224020150/chapter2/\345\210\240\351\231\244\346\216\222\345\272\217\351\223\276\350\241\250\344\270\255\347\232\204\351\207\215\345\244\215\345\205\203\347\264\240.c" "b/2224020150/chapter2/\345\210\240\351\231\244\346\216\222\345\272\217\351\223\276\350\241\250\344\270\255\347\232\204\351\207\215\345\244\215\345\205\203\347\264\240.c" new file mode 100644 index 0000000000000000000000000000000000000000..4f52f492edfb6a645cef487e4c0e747112ce44a4 --- /dev/null +++ "b/2224020150/chapter2/\345\210\240\351\231\244\346\216\222\345\272\217\351\223\276\350\241\250\344\270\255\347\232\204\351\207\215\345\244\215\345\205\203\347\264\240.c" @@ -0,0 +1,27 @@ + +struct ListNode { + int val; + struct ListNode *next; + }; + + + +struct ListNode* deleteDuplicates(struct ListNode* head){ + if ( !head ) + { + return head; + } + struct ListNode* h= head; + while(head->next!=NULL) + { + if(head->val==head->next->val) + { + head->next=head->next->next; + } + else + { + head=head->next; + } + } + return h; +} diff --git "a/2224020150/chapter2/\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.c" "b/2224020150/chapter2/\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.c" new file mode 100644 index 0000000000000000000000000000000000000000..6da9784b7dbbc822d4f219b4fa0375c0ab4bd73b --- /dev/null +++ "b/2224020150/chapter2/\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.c" @@ -0,0 +1,26 @@ +struct ListNode { + int val; + struct ListNode *next; +}; +struct ListNode* removeElements(struct ListNode* head, int val){ + if (head == NULL) return head; + do { + if (head->val == val) head = head->next; + else break; + } + while (head != NULL); + if (head == NULL) return head; + struct ListNode *p = head; + struct ListNode *pf = head->next; + while (pf != NULL) { + if (pf->val == val) { + p->next = pf->next; + pf = pf->next; + } + else { + p = p->next; + pf = pf->next; + } + } + return head; +} diff --git "a/2224020150/chapter2/\347\277\273\350\275\254\351\223\276\350\241\250.c" "b/2224020150/chapter2/\347\277\273\350\275\254\351\223\276\350\241\250.c" new file mode 100644 index 0000000000000000000000000000000000000000..ade80478b4a0aabc6e0019ecb252db284e0618fe --- /dev/null +++ "b/2224020150/chapter2/\347\277\273\350\275\254\351\223\276\350\241\250.c" @@ -0,0 +1,17 @@ +struct ListNode { + int val; + struct ListNode *next; + }; +struct ListNode* ReverseList(struct ListNode* pHead ) { + struct ListNode *pre = NULL; + struct ListNode *cur = pHead; + struct ListNode *next = NULL; + while(cur) + { + next = cur->next; + cur->next = pre; + pre = cur; + cur = next; + } + return pre; +} \ No newline at end of file diff --git "a/2224020150/chapter3/\345\237\272\346\234\254\350\256\241\347\256\227\345\231\2502.c" "b/2224020150/chapter3/\345\237\272\346\234\254\350\256\241\347\256\227\345\231\2502.c" new file mode 100644 index 0000000000000000000000000000000000000000..71d774026d1c426b9fce019f5c1a465fdbfb3440 --- /dev/null +++ "b/2224020150/chapter3/\345\237\272\346\234\254\350\256\241\347\256\227\345\231\2502.c" @@ -0,0 +1,33 @@ +int calculate(char* s) { + int n = strlen(s); + int stk[n], top = 0; + char preSign = '+'; + int num = 0; + for (int i = 0; i < n; ++i) { + if (isdigit(s[i])) { + num = num * 10 + (int)(s[i] - '0'); + } + if (!isdigit(s[i]) && s[i] != ' ' || i == n - 1) { + switch (preSign) { + case '+': + stk[top++] = num; + break; + case '-': + stk[top++] = -num; + break; + case '*': + stk[top - 1] *= num; + break; + default: + stk[top - 1] /= num; + } + preSign = s[i]; + num = 0; + } + } + int ret = 0; + for (int i = 0; i < top; i++) { + ret += stk[i]; + } + return ret; +} \ No newline at end of file diff --git "a/2224020150/chapter3/\346\234\200\345\260\217\346\240\210.c" "b/2224020150/chapter3/\346\234\200\345\260\217\346\240\210.c" new file mode 100644 index 0000000000000000000000000000000000000000..759ed625eedc4cca92ade91a0557e284bbf25cb3 --- /dev/null +++ "b/2224020150/chapter3/\346\234\200\345\260\217\346\240\210.c" @@ -0,0 +1,66 @@ +#include +#include +#define MAXSIZE 1600 + +typedef struct { + int *data; + int top; +} MinStack; + +/** initialize your data structure here. */ + +MinStack* minStackCreate() { + MinStack *obj=(MinStack *)malloc(sizeof(MinStack)); + obj->data=(int *)malloc(MAXSIZE*sizeof(int)); + obj->top=-1; + return obj; +} + +void minStackPush(MinStack* obj, int x) { + if(obj->top==MAXSIZE-1){ + + }else if(obj->top==-1){ + obj->top++; + obj->data[obj->top]=x; + obj->top++; + obj->data[obj->top]=x; + }else{ + int tmp=obj->data[obj->top]; + obj->top++; + obj->data[obj->top]=x; + if(tmptop++; + obj->data[obj->top]=tmp; + }else{ + obj->top++; + obj->data[obj->top]=x; + } + } +} + +void minStackPop(MinStack* obj) { + if(obj->top==-1){ + + }else{ + obj->top--; + obj->top--; + } +} + +int minStackTop(MinStack* obj) { + if(obj->top==-1){ + return; + } + return obj->data[obj->top-1]; +} + +int minStackGetMin(MinStack* obj) { + return obj->data[obj->top]; +} + +void minStackFree(MinStack* obj) { + free(obj->data); + obj->data=NULL; + free(obj); + obj=NULL; +} \ No newline at end of file diff --git "a/2224020150/chapter3/\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267.c" "b/2224020150/chapter3/\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267.c" new file mode 100644 index 0000000000000000000000000000000000000000..8b6f5d7c90cfebffb3a2587dae8d182220221459 --- /dev/null +++ "b/2224020150/chapter3/\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267.c" @@ -0,0 +1,147 @@ +typedef char SDataType; +typedef struct Stack +{ + SDataType* data; + int capacity; + int top; +}ST; + +//栈的初始化 +void StackInit(ST* ps) +{ + ps->data = (SDataType*)malloc(sizeof(SDataType*) * 4); + if (ps->data == NULL) + { + printf("malloc fail\n"); + exit(-1); + } + ps->capacity = 4; + ps->top = 0;//初始化给0top指向的是栈顶元素的下一个 + //给-1则指向栈顶 +} + +//摧毁栈 +void StackDestory(ST* ps) +{ + assert(ps); + free(ps->data); + ps->data = NULL; + ps->top = ps->capacity = 0; +} + +//栈顶插入删除数据 +//入栈 +void StackPush(ST* ps,SDataType x) +{ + assert(ps); + if (ps->capacity == ps->top) + { + SDataType* newps = (SDataType*)realloc(ps->data,sizeof(SDataType*) * 2 * ps->capacity); + + if (newps == NULL) + { + printf("realloc fail\n"); + return; + } + else + { + ps->data = newps; + ps->capacity = 2 * ps->capacity; + } + } + + ps->data[ps->top] = x; + ps->top++; +} + +//出栈 +void StackPop(ST* ps) +{ + assert(ps); + assert(ps->top > 0); + ps->top--; +} + +//输出栈顶的数据 +SDataType StackTop(ST* ps) +{ + assert(ps); + assert(ps > 0); + return ps->data[ps->top - 1]; +} + +//栈中元素的个数 +int StackSize(ST* ps) +{ + assert(ps); + return ps->top; +} + +//判断是否栈空 +bool StackEmpty(ST* ps) +{ + assert(ps); + return ps->top == 0; +} + +bool isValid(char* s) +{ + //1、如果是左括号,入栈 + //2、右括号,出栈顶的左括号跟右括号判断是否匹配 + //如果匹配就继续如果不匹配退出 + ST st; + + StackInit(&st); + + while (*s != '\0') + { + switch (*s) + { + case '{': + case '(': + case '[': + { + StackPush(&st, *s); + ++s; + break; + } + + case '}': + case ']': + case ')': + { + if (StackEmpty(&st)) + { + StackDestory(&st); + return false; + } + else + { + char top = StackTop(&st); + StackPop(&st); + + if ((*s == '}' && top != '{') + || (*s == ')' && top != '(') + || (*s == ']' && top != '[')) + { + StackDestory(&st); + return false; + } + else + { + ++s; + } + + break; + } + } + + default: + break; + } + } + + bool ret = StackEmpty(&st); + StackDestory(&st); + return ret; +} diff --git "a/2224020150/chapter3/\347\224\250\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227.c" "b/2224020150/chapter3/\347\224\250\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227.c" new file mode 100644 index 0000000000000000000000000000000000000000..1a9b48e5cb6fd7c312768ceb41f2f21c091c25de --- /dev/null +++ "b/2224020150/chapter3/\347\224\250\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227.c" @@ -0,0 +1,82 @@ +typedef struct{ + int* data; + int top; +}MyStack; + +typedef struct{ + MyStack* stackPush; + MyStack* stackPop; +}MyQueue; + +#define maxQueueNum 100 //队列空间 +MyQueue* myQueueCreate(){ + MyQueue* queue = (MyQueue*)malloc(sizeof(MyQueue)); + + queue->stackPush = (MyStack*)malloc(sizeof(MyStack)); + queue->stackPop = (MyStack*)malloc(sizeof(MyStack)); + + queue->stackPush->data = (int*)malloc(sizeof(int) * maxQueueNum); + queue->stackPush->top = -1; + + queue->stackPop->data = (int*)malloc(sizeof(int) * maxQueueNum); + queue->stackPop->top = -1; + + return queue; +} + +//入队 +void myQueuePush(MyQueue* obj){ + if((obj->stackPush->top + obj->stackPop->top) == maxStackNum - 2) //队列空间满 + return ; + obj->stackPush->data[++obj->stackPush->top] = x; +} + +//出队 +int myQueuePop(MyQueue* obj){ + if(obj->stackPop->top != -1){ //辅助栈不为空,直接弹出 + return obj->stackPop->data[obj->stackPop->top--]; + } + while(obj->stackPush->top != -1){ //辅助栈为空,将当前栈依次弹出并压到辅助栈中 + obj->stackPop->data[++obj->stackPop->top] = obj->stackPush->data[obj->stackPush->top--]; + } + if(obj->stackPop->top == -1){ //辅助栈仍为空,说明对应队列为空,无值返回 + return -1; + } + else + { + return obj->stackPop->data[obj->stckPop->top--]; + } +} + +//取队首值 +int myQueuePeek(MyQueue* obj){ + if((obj->stackPush->top == -1) && (obj->stackPop->top == -1))//队列为空 + return -1; + + if(obj->stackPop->top != -1){ + return obj->stackPop->data[obj->stackPop->top]; + } + else + { + while(obj->stackPush->top != -1){ + obj->stackPop->data[++obj->stackPop->top] = obj->stackPush->data[obj->stackPush->top--]; + } + return obj->stackPop->data[obj->stackPop->top]; + } +} + +//判断队列是否为空 +bool myQueueEmpty(MyQueue* obj){ + if((obj->stackPush->top == -1) && (obj->stackPop->top == -1)) + return true; + else + return false; +} + +void myQueueFree(MyQueue* obj) { + free(obj->stackPush->data); + free(obj->stackPop->data); + free(obj->stackPush); + free(obj->stackPop); + free(obj); +} \ No newline at end of file diff --git "a/2224020150/chapter3/\347\224\250\351\230\237\345\210\227\345\256\236\347\216\260\346\240\210.c" "b/2224020150/chapter3/\347\224\250\351\230\237\345\210\227\345\256\236\347\216\260\346\240\210.c" new file mode 100644 index 0000000000000000000000000000000000000000..57934eef0d850189964c20f719e78f7b38d0fdf8 --- /dev/null +++ "b/2224020150/chapter3/\347\224\250\351\230\237\345\210\227\345\256\236\347\216\260\346\240\210.c" @@ -0,0 +1,186 @@ +typedef int QDataType; +//队列的一个节点 +typedef struct QueueNode +{ + struct QueueNode* next; + QDataType data; +}QueueNode; +//真正的队列: +typedef struct Queue +{ + QueueNode* head;//指向队头 + QueueNode* tail;//指向队尾 +}Queue; +void QueueInit(Queue* pq);//队列初始化 +void QueueDestroy(Queue* pq);//摧毁队列 +void QueuePush(Queue* pq, QDataType x);//入队 +void QueuePop(Queue* pq);//出队 +QDataType QueueFront(Queue* pq);//队头数据 +QDataType QueueBack(Queue* pq);//队尾数据 +int QueueSize(Queue* pq);//队列数据个数 +bool QueueEmpty(Queue* pq);//队列是否为空 +void QueueInit(Queue* pq)//队列初始化 +{ + assert(pq); + pq->head = pq->tail = NULL; + //开始都为空 +} + +void QueueDestroy(Queue* pq)//摧毁队列 +{ + assert(pq); + QueueNode* cur = pq->head; + while (cur) + { + QueueNode* next = cur->next; + free(cur); + cur = next; + } + pq->head = pq->tail = NULL; +} + +void QueuePush(Queue* pq, QDataType x)//入队 +{ + assert(pq); + + //新开节点 + QueueNode* newnode = (QueueNode*)malloc(sizeof(QueueNode)); + if (newnode == NULL) + { + printf("malloc fail\n"); + exit(-1); + } + else + { + newnode->data = x; + newnode->next = NULL; + } + + //如果队列为空,头和尾相同 + if (pq->head == NULL) + { + pq->head = pq->tail = newnode; + } + + //不同,则将尾的下一个指向newnode,然后把newnode赋给tail + else + { + pq->tail->next = newnode; + pq->tail = newnode; + } +} + +void QueuePop(Queue* pq)//出队 +{ + assert(pq); + assert(!QueueEmpty(pq)); + QueueNode* next = pq->head->next; + free(pq->head); + pq->head = next; + if (pq->head == NULL) + { + pq->tail = NULL; + } +} + +QDataType QueueFront(Queue* pq)//队头数据 +{ + assert(pq); + assert(!QueueEmpty(pq)); + return pq->head->data; +} + +QDataType QueueBack(Queue* pq)//队尾数据 +{ + assert(pq); + assert(!QueueEmpty(pq)); + return pq->tail->data; +} + +int QueueSize(Queue* pq)//队列数据个数 +{ + int size = 0; + QueueNode* cur = pq->head; + while (cur != NULL) + { + size++; + cur = cur->next; + } + return size; +} + +bool QueueEmpty(Queue* pq)//队列是否为空 +{ + assert(pq); + if (pq->head == NULL) + { + return true; + } + return false; +} + +typedef struct { + Queue q1; + Queue q2; +} MyStack; + + +MyStack* myStackCreate() { + MyStack* NewStack = (MyStack*)malloc(sizeof(MyStack)); + QueueInit(&NewStack->q1); + QueueInit(&NewStack->q2); + return NewStack; +} + +void myStackPush(MyStack* obj, int x) { + if(QueueEmpty(&obj->q1)) + { + QueuePush(&obj->q2,x); + } + else + { + QueuePush(&obj->q1,x); + } +} + +int myStackPop(MyStack* obj) { + Queue* emptyQ = &obj->q1; + Queue* nonemptyQ = &obj->q2; + if(!QueueEmpty(&obj->q1)) + { + emptyQ = &obj->q2; + nonemptyQ = &obj->q1; + } + + while(QueueSize(nonemptyQ) > 1) + { + QueuePush(emptyQ,QueueFront(nonemptyQ)); + QueuePop(nonemptyQ); + } + QDataType top = QueueFront(nonemptyQ); + QueuePop(nonemptyQ); + return top; +} + +int myStackTop(MyStack* obj) { + if(QueueEmpty(&obj->q1)) + { + return QueueBack(&obj->q2); + } + return QueueBack(&obj->q1); +} + +bool myStackEmpty(MyStack* obj) { + if(QueueEmpty(&obj->q1) && QueueEmpty(&obj->q2)) + { + return true; + } + return false; +} + +void myStackFree(MyStack* obj) { + free(obj); + QueueDestory(&obj->q1); + QueueDestory(&obj->q2); + obj = NULL; +} \ No newline at end of file diff --git "a/2224020150/chapter3/\351\200\206\346\263\242\345\205\260\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274.c" "b/2224020150/chapter3/\351\200\206\346\263\242\345\205\260\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274.c" new file mode 100644 index 0000000000000000000000000000000000000000..aa59d6d25911defe55af29ebc822feb11795e710 --- /dev/null +++ "b/2224020150/chapter3/\351\200\206\346\263\242\345\205\260\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274.c" @@ -0,0 +1,73 @@ +#include +#include +#define MaxSize 199 + +struct SNode { + int Data[MaxSize]; + int Top; +}; +typedef struct SNode* Stack; + +Stack CreateStack() { + Stack p; + p = (Stack)malloc(sizeof(struct SNode)); + p->Top = -1; + return p; +} + +void Push(Stack S, int x) { + if (S->Top == MaxSize) { + printf("Stack Full\n"); + } + else { + S->Data[++S->Top] = x; + } +} + +int Pop(Stack S) { + if (S->Top == -1) { + printf("Stack is Empty!\n"); + } + else { + int t; + t = S->Data[S->Top]; + S->Top--; + return t; + } +} + +int main() { + Stack S; + S = CreateStack(); + char ch; + ch = getchar(); + int a, b, an, t; + + while (ch != EOF) { + if (ch >= '0' && ch <= '9') { + Push(S, ch - '0'); + ch = getchar(); + while (ch >= '0' && ch <= '9') { + t = Pop(S); + Push(S, t * 10 + ch - '0'); + ch = getchar(); + } + } + else if (ch == '+' || ch == '-' || ch == '*' || ch == '/') { + a = Pop(S); + b = Pop(S); + switch (ch) { + case '+':an = b + a; break; + case '-':an = b - a; break; + case '*':an = b * a; break; + case '/':an = b / a; break; + } + Push(S, an); + } + else if (ch == '\n') { + break; + } + ch = getchar(); + } + printf("%d", Pop(S)); +} \ No newline at end of file diff --git "a/2224020150/chapter4/\345\256\236\347\216\260strStr().c" "b/2224020150/chapter4/\345\256\236\347\216\260strStr().c" new file mode 100644 index 0000000000000000000000000000000000000000..65082438ff73c993713ecd3988a74be5990ba1c8 --- /dev/null +++ "b/2224020150/chapter4/\345\256\236\347\216\260strStr().c" @@ -0,0 +1,55 @@ +#include +#include +#include +char* my_strstr(const char* p1, const char*p2) +{ + assert(p1 != NULL);//断言该指针不为空指针 + assert(p2 != NULL); + char*s1 = NULL;//创建一个空指针; + char*s2 = NULL; + char*cur = p1; + if (*p2 == '\0') + { + return (char*)p1; + } + + while (*cur)//cur代表指向的是子串第一次出现的首元素地址 + { + s1 = cur; //把cur指向字符串的首元素地址赋给s1 + s2 = (char*)p2; + while ((*s1 != 0) && (*s2 != 0) && *s1 == *s2) + { + s1++; + s2++; + } + if (*s2 == '\0') + { + return (char*)cur; + } + if (*s1 == '\0') + { + return NULL; + } + cur++; + } + + return NULL; + +} + +int main() +{ + const char* arr1 = "abbbcdefg"; + const char* arr2 = "bbc"; + char* ret = my_strstr(arr1, arr2); + if (ret == NULL) + { + printf("子串不存在\n"); + } + else + { + printf("找到了 子串为:%s\n", ret); + } + + return 0; +} diff --git "a/2224020150/chapter4/\346\234\200\351\225\277\345\205\254\345\205\261\345\211\215\347\274\200.c" "b/2224020150/chapter4/\346\234\200\351\225\277\345\205\254\345\205\261\345\211\215\347\274\200.c" new file mode 100644 index 0000000000000000000000000000000000000000..e1e2782a4620a5edcd0fd317666686e023574688 --- /dev/null +++ "b/2224020150/chapter4/\346\234\200\351\225\277\345\205\254\345\205\261\345\211\215\347\274\200.c" @@ -0,0 +1,48 @@ +#include +#include +#include + +char *longestCommmonPrefix(char **strs,int strSize) +{ + int i,j; + int flag = 0,max=0; + for(i=1; i<=strlen(*strs) && flag!=2; i++) + { + flag = 0; + for(j=1; jstrlen(strs[j])) + { + flag = 2; + break; + } + } + if(flag == 0) + max = i; + } + + char *result = (char *)malloc((max+1)*sizeof(char)); + memset(result,0,max+1); + memcpy(result,strs[0],max); + + return result; +} + +int main() +{ + /* + example 1: char *strs[3]= {"flowers","flow","flight"}; + example 2: char *strs[3]= {"dog","racecar","car"}; + */ + char *strs[3]= {"flowers","flow","flight"}; + char *prefix = longestCommmonPrefix(strs,3); + printf("%s\n",prefix); + free(prefix); + prefix = NULL; + return 0; +} \ No newline at end of file diff --git "a/2224020150/chapter4/\351\207\215\345\244\215\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262.c" "b/2224020150/chapter4/\351\207\215\345\244\215\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262.c" new file mode 100644 index 0000000000000000000000000000000000000000..6e29cd2309babb85a4f8b05459d1b95edddf6c34 --- /dev/null +++ "b/2224020150/chapter4/\351\207\215\345\244\215\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262.c" @@ -0,0 +1,37 @@ +#include +#include +#include +#include +void Next(int* next, int size,const char* s); +bool HaveCompletelyRepeatString(char* s); +int main(int argc, char** argv) { + + char* s = "asddasddasdd"; + bool ans = HaveCompletelyRepeatString(s); + printf("%s have substing ? %c",s, ans ? 'Y' : 'N'); + return 0; +} + +void Next(int* next, int size,const char* s) { + next[0] = -1; + int j = -1; + for (int i = 1;i= 0 && s[i] != s[j + 1]) + j = next[j]; + if (s[i] == s[j + 1]) + ++j; + next[i] = j; + } +} +bool HaveCompletelyRepeatString(char* s) { + if (*s == '\0') + return false; + int size = 0; + while (s[size] != '\0') + ++size; + int next[256]; + Next(next,size, s); + if (next[size-1]!=-1&&size%(size-next[size-1]-1) == 0) + return true; + return false; +} diff --git "a/2224020150/chapter4/\351\252\214\350\257\201\345\233\236\346\226\207\344\270\262.c" "b/2224020150/chapter4/\351\252\214\350\257\201\345\233\236\346\226\207\344\270\262.c" new file mode 100644 index 0000000000000000000000000000000000000000..630e24866f61c0b4c19f8bca6d2cd2c16fb3c93b --- /dev/null +++ "b/2224020150/chapter4/\351\252\214\350\257\201\345\233\236\346\226\207\344\270\262.c" @@ -0,0 +1,26 @@ +#include +#include + +int isPalindrome(char str[]) { + int len = strlen(str); // 获取字符串长度 + + for (int i = 0; i <= len / 2 - 1; ++i) { + if (str[i] != str[len-i-1]) { // 判断对称位置上的字符是否相等 + return 0; // 如果不相等则返回0表示非回文串 + } + } + + return 1; // 所有对称位置上的字符都相等,返回1表示回文串 +} + +int main() { + char str[] = "abcba"; // 输入要验证的字符串 + + if (isPalindrome(str)) { + printf("该字符串为回文串\n"); + } else { + printf("该字符串不是回文串\n"); + } + + return 0; +} \ No newline at end of file diff --git "a/2224020150/chapter5/\345\233\236\346\226\207\351\223\276\350\241\250.c" "b/2224020150/chapter5/\345\233\236\346\226\207\351\223\276\350\241\250.c" new file mode 100644 index 0000000000000000000000000000000000000000..f6eba85f5655a3d5ff36afd3ce4fb68334df3b05 --- /dev/null +++ "b/2224020150/chapter5/\345\233\236\346\226\207\351\223\276\350\241\250.c" @@ -0,0 +1,30 @@ +struct ListNode + { + int val; + struct ListNode *next; + }; +typedef struct ListNode Node; +bool isPalindrome(struct ListNode* head) { + if(head==NULL) return true; + Node *fast=head; + Node *slow=head; + Node *p,*q,*temp=NULL; + while(fast->next && fast->next->next) { + fast=fast->next->next; + slow=slow->next; + } + slow=slow->next; + while(slow!=NULL) { //逆置链表 + p=slow; + slow=slow->next; + p->next=temp; + temp=p; + } + while(temp!=NULL) { //比较回文 + if(temp->val!=head->val) + return false; + temp=temp->next; + head=head->next; + } + return true; +} diff --git "a/2224020150/chapter5/\346\226\220\346\263\242\351\202\243\345\245\221\346\225\260.c" "b/2224020150/chapter5/\346\226\220\346\263\242\351\202\243\345\245\221\346\225\260.c" new file mode 100644 index 0000000000000000000000000000000000000000..a267668c7f220c37d91d305bc90aca6406815cc0 --- /dev/null +++ "b/2224020150/chapter5/\346\226\220\346\263\242\351\202\243\345\245\221\346\225\260.c" @@ -0,0 +1,21 @@ +#include +int Fibon(int n) //递归函数 +{ + if (n == 1|| n == 2) + return 1; + else + return Fibon(n-1)+ Fibon(n-2); +} +​ +int main() +{ + int n,res=0; + scanf("%d",&n); + for (int i = 1; i <= n; i++) + { + res = Fibon(i); +// printf("%d ",res); + } + printf("\n第%d项斐波那契数列的值为:%d",n,res); + return 0; +} \ No newline at end of file diff --git "a/2224020150/chapter6/\346\234\200\345\244\247\350\277\236\347\273\2551\347\232\204\344\270\252\346\225\260.c" "b/2224020150/chapter6/\346\234\200\345\244\247\350\277\236\347\273\2551\347\232\204\344\270\252\346\225\260.c" new file mode 100644 index 0000000000000000000000000000000000000000..87a5ad794dda2c34c620b000b2f60a1825e160ac --- /dev/null +++ "b/2224020150/chapter6/\346\234\200\345\244\247\350\277\236\347\273\2551\347\232\204\344\270\252\346\225\260.c" @@ -0,0 +1,75 @@ +int get(int* nums, int numsSize) +{ + //遍历元素,找到元素0 + int i = 0; + int j = 0; + int max = 0; + int sum = 0; + for (i = 0; i < numsSize; i++) + { + int win = 0; + int cont = 0; + if (nums[i] == 0) + { + //计算左边1的个数 + for (j = i-1; j >= 0; j--) + { + if (nums[j] == 1) + { + win++; + if (max < win) + { + max = win; + } + } + else + { + break; + } + } + for (j = i + 1; j sum ? max : sum; +} +int main() +{ + //定义一个二进制数组 + int nums[1] = { 0 }; + //计算元素个数 + int numsSize = sizeof(nums) / sizeof(nums[0]); + //输入元素 + int i = 0; + for (i = 0; i < numsSize; i++) + { + scanf("%d,", &nums[i]); + } + //调用函数实现 + int arr = get(nums, numsSize); + printf("%d\n", arr); + return 0; +} \ No newline at end of file diff --git "a/2224020150/chapter6/\347\237\251\351\230\265\345\257\271\350\247\222\347\272\277\345\205\203\347\264\240\347\232\204\345\222\214.c" "b/2224020150/chapter6/\347\237\251\351\230\265\345\257\271\350\247\222\347\272\277\345\205\203\347\264\240\347\232\204\345\222\214.c" new file mode 100644 index 0000000000000000000000000000000000000000..d56ed2bb3f01dbaf25c8224d024d50d58b436862 --- /dev/null +++ "b/2224020150/chapter6/\347\237\251\351\230\265\345\257\271\350\247\222\347\272\277\345\205\203\347\264\240\347\232\204\345\222\214.c" @@ -0,0 +1,16 @@ +#include + +int main() { + int matrix[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; + int sum1 = 0, sum2 = 0; + + for (int i = 0; i < 3; i++) { + sum1 += matrix[i][i]; // 左上到右下的对角线 + sum2 += matrix[i][2 - i]; // 左下到右上的对角线 + } + + printf("Sum of diagonal elements (left to right): %d\n", sum1); + printf("Sum of diagonal elements (right to left): %d\n", sum2); + + return 0; +} diff --git "a/2224020150/chapter6/\347\247\273\345\212\2500.c" "b/2224020150/chapter6/\347\247\273\345\212\2500.c" new file mode 100644 index 0000000000000000000000000000000000000000..f2c032a3efb1e85ca17512994f42588d86b937be --- /dev/null +++ "b/2224020150/chapter6/\347\247\273\345\212\2500.c" @@ -0,0 +1,18 @@ +#include +int main() { + int nums[] = { 0,1,0,3,12 }; + int numsSize = 5; + int temp; + for (int j = 0; j < numsSize; j++) printf("%d ", nums[j]); + for (int i = 0; i < numsSize - 1; i++) { + for (int j = 0; j < numsSize-1; j++) { + if (nums[j] == 0) { + temp = nums[j]; + nums[j] = nums[j + 1]; + nums[j + 1] = temp; + } + } + } + for (int j = 0; j < numsSize; j++) printf("%d ", nums[j]); + return 0; +} diff --git "a/2224020150/chapter6/\351\207\215\345\241\221\347\237\251\351\230\265.c" "b/2224020150/chapter6/\351\207\215\345\241\221\347\237\251\351\230\265.c" new file mode 100644 index 0000000000000000000000000000000000000000..16ce4c11c8bbd546a5d53e45f3a0961512d79afa --- /dev/null +++ "b/2224020150/chapter6/\351\207\215\345\241\221\347\237\251\351\230\265.c" @@ -0,0 +1,21 @@ +int** matrixReshape(int** nums, int numsSize, int* numsColSize, int r, int c, int* returnSize, int** returnColumnSizes) { + int m = numsSize; + int n = numsColSize[0]; + if (m * n != r * c) { + *returnSize = numsSize; + *returnColumnSizes = numsColSize; + return nums; + } + *returnSize = r; + *returnColumnSizes = malloc(sizeof(int) * r); + int** ans = malloc(sizeof(int*) * r); + + for (int i = 0; i < r; i++) { + (*returnColumnSizes)[i] = c; + ans[i] = malloc(sizeof(int) * c); + } + for (int x = 0; x < m * n; ++x) { + ans[x / c][x % c] = nums[x / n][x % n]; + } + return ans; +} diff --git "a/2224020150/chapter7/N\345\217\211\346\240\221\347\232\204\345\205\210\346\240\271\351\201\215\345\216\206.c" "b/2224020150/chapter7/N\345\217\211\346\240\221\347\232\204\345\205\210\346\240\271\351\201\215\345\216\206.c" new file mode 100644 index 0000000000000000000000000000000000000000..7af368e263e3c2984f63db3dc0d81fea9ccd8940 --- /dev/null +++ "b/2224020150/chapter7/N\345\217\211\346\240\221\347\232\204\345\205\210\346\240\271\351\201\215\345\216\206.c" @@ -0,0 +1,15 @@ +void Recur(struct Node* root,int* ret,int* pos){ + if(root==NULL) return; + ret[(*pos)++]=root->val; + for(int i=0;inumChildren;i++){ + Recur(root->children[i],ret,pos); + } +} + +int* preorder(struct Node* root, int* returnSize) { + int * ret = (int *)malloc(sizeof(int) * 10000); + int pos=0; + Recur(root,ret,&pos); + *returnSize=pos; + return ret; +} diff --git "a/2224020150/chapter7/\344\272\214\345\217\211\346\240\221\347\232\204\344\270\255\345\272\217\357\274\214\345\261\202\346\254\241\351\201\215\345\216\206.c" "b/2224020150/chapter7/\344\272\214\345\217\211\346\240\221\347\232\204\344\270\255\345\272\217\357\274\214\345\261\202\346\254\241\351\201\215\345\216\206.c" new file mode 100644 index 0000000000000000000000000000000000000000..9ff609a6f2be8a46c1eea84e647aba46d1bdcd04 --- /dev/null +++ "b/2224020150/chapter7/\344\272\214\345\217\211\346\240\221\347\232\204\344\270\255\345\272\217\357\274\214\345\261\202\346\254\241\351\201\215\345\216\206.c" @@ -0,0 +1,113 @@ +#include +#include +typedef char TElemType; + +const int MAXSIZES = 50; + +// 二叉树的存储结构 +typedef struct BiTNode { + TElemType data; + struct BiTNode *lchild, *rchild; +}BiTNode, *BiTree; + +typedef BiTree QElemType; + +// 队列的存储结构 +typedef struct { + QElemType data[MAXSIZES]; + int front; + int rear; +}SqQueue; + +// 通过前序遍历创建二叉树 +void CreateBiTree(BiTree *T) +{ + TElemType ch; + scanf("%c", &ch); + if (ch == '#') + { + *T = NULL; + } + else + { + *T = (BiTree) malloc(sizeof(BiTNode)); + if (!T) + { + exit(0); + } + (*T)->data = ch; + CreateBiTree(&(*T)->lchild); + CreateBiTree(&(*T)->rchild); + } +} + +// 初始化队列 +void InitQueue(SqQueue *Q) +{ + Q->front = Q->rear = 0; +} + +// 辅助队列的入队操作 +void EnQueue(SqQueue *Q, QElemType q) +{ + if ((Q->rear + 1) % MAXSIZES == Q->front) + return; + Q->data[Q->rear] = q; + Q->rear = (Q->rear + 1) % MAXSIZES; +} + +// 辅助队列的出队操作 +void DeQueue(SqQueue *Q, QElemType *q) +{ + if (Q->front == Q->rear) + return; + *q = Q->data[Q->front]; + Q->front = (Q->front + 1) % MAXSIZES; +} + +// 二叉树的中序遍历 +void InOrderTraverse(BiTree T) +{ + if (!T) + { + return; + } + + InOrderTraverse(T->lchild); + printf("%c", T->data); + InOrderTraverse(T->rchild); +} + +// 二叉树的层次遍历 +void LevelOrder(BiTree T) +{ + SqQueue Q; + InitQueue(&Q); // 初始化辅助队列 + BiTree p; + EnQueue(&Q, T); // 将根节点入队 + while(Q.front != Q.rear) // 队列不空则循环 + { + DeQueue(&Q, &p); // 队头结点出队 + printf("%c", p->data); + if (p->lchild != NULL) + { + EnQueue(&Q, p->lchild); // 左子树不空,则左子树根节点入队 + } + if (p->rchild != NULL) + { + EnQueue(&Q, p->rchild); // 右子树不空,则右子树根节点入队 + } + } +} + +int main() +{ + BiTree t; + printf("请按照前序扩展二叉树进行输入:(#表示空结点)\n"); + CreateBiTree(&t); + printf("中序遍历: "); + InOrderTraverse(t); + printf("\n层次遍历: "); + LevelOrder(t); + return 0; +} diff --git "a/2224020150/chapter7/\344\272\214\345\217\211\346\240\221\347\232\204\345\220\216\345\272\217\351\201\215\345\216\206.c" "b/2224020150/chapter7/\344\272\214\345\217\211\346\240\221\347\232\204\345\220\216\345\272\217\351\201\215\345\216\206.c" new file mode 100644 index 0000000000000000000000000000000000000000..09dffb70385881e1d6d7f02c04e30d2cb1cf788f --- /dev/null +++ "b/2224020150/chapter7/\344\272\214\345\217\211\346\240\221\347\232\204\345\220\216\345\272\217\351\201\215\345\216\206.c" @@ -0,0 +1,53 @@ +#include +#include +typedef struct node{ + int data; + struct node* left; + struct node* right; +} Node; +//先序遍历 +void preorder(Node* node){ + if(node !=NULL){ + printf("%d",node -> data); + preorder(node -> left); + printf("%d",node -> data); + preorder(node -> right); + } + } + //中序遍历 +void inorder(Node* node) +{ +if(node!=NULL){ + inorder(node -> left); + printf("%d\n",node ->data); + inorder (node -> right); +} +} +//后序遍历 +void postorder(Node* node){ +if(node!=NULL){ +postorder(node ->left); +postorder(node ->right); +printf("%d\n",node ->data); +} +} +int main(){ +Node n1; +Node n2; +Node n3; +Node n4; +n1.data = 5; +n2.data=6; +n3.data =7; +n4.data=8; +n1.left = &n2; +n1.right = &n3; +n2.left =&n4; +n2.right=NULL; +n3.left =NULL; +n3.right =NULL; +n4.left=NULL; +n4. right=NULL; + +postorder(&n1); +} diff --git "a/2224020150/chapter7/\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\244\247\345\256\275\345\272\246.c" "b/2224020150/chapter7/\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\244\247\345\256\275\345\272\246.c" new file mode 100644 index 0000000000000000000000000000000000000000..321a73e605a35256304eafafbba5e298836f39ba --- /dev/null +++ "b/2224020150/chapter7/\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\244\247\345\256\275\345\272\246.c" @@ -0,0 +1,74 @@ +#include +#include + +// 定义二叉树节点结构体 +struct TreeNode { + int val; + struct TreeNode* left; + struct TreeNode* right; +}; + +int maxWidth(struct TreeNode* root) { + if (root == NULL) { // 如果根节点为空,则返回0作为最小宽度 + return 0; + } + + int width = 1; // 初始化当前层的宽度为1 + int max_width = 1; // 记录最大宽度 + + // 使用队列来进行广度优先搜索(BFS) + struct TreeNode** queue = malloc(sizeof(struct TreeNode*) * 2); // 创建一个长度为2的指向TreeNode类型的数组 + int front = -1; // 队头指针 + int rear = -1; // 队尾指针 + + queue[++rear] = root; // 将根节点入队 + + while (front != rear) { + int size = rear - front + 1; // 获取当前层的节点数量 + + for (int i = 0; i < size; ++i) { + struct TreeNode* node = queue[++front]; // 从队首移除元素并赋值给node变量 + + if (node->left != NULL) { // 左子节点不为空时加入队列 + queue[++rear] = node->left; + } + + if (node->right != NULL) { // 右子节点不为空时加入队列 + queue[++rear] = node->right; + } + } + + width += size; // 更新当前层的宽度 + max_width = fmax(max_width, width); // 更新最大宽度 + } + + free(queue); // 释放内存 + + return max_width; // 返回最大宽度 +} + +int main() { + // 创建一个测试二叉树 + struct TreeNode* root = (struct TreeNode*)malloc(sizeof(struct TreeNode)); + root->val = 3; + + struct TreeNode* node1 = (struct TreeNode*)malloc(sizeof(struct TreeNode)); + node1->val = 9; + root->left = node1; + + struct TreeNode* node2 = (struct TreeNode*)malloc(sizeof(struct TreeNode)); + node2->val = 20; + root->right = node2; + + struct TreeNode* node3 = (struct TreeNode*)malloc(sizeof(struct TreeNode)); + node3->val = 15; + node1->left = node3; + + struct TreeNode* node4 = (struct TreeNode*)malloc(sizeof(struct TreeNode)); + node4->val = 7; + node1->right = node4; + + printf("Binary tree maximum width is %d\n", maxWidth(root)); + + return 0; +} \ No newline at end of file diff --git "a/2224020150/chapter7/\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\244\247\346\267\261\345\272\246.c" "b/2224020150/chapter7/\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\244\247\346\267\261\345\272\246.c" new file mode 100644 index 0000000000000000000000000000000000000000..9f4389ee8d09a3defe57f94f2f09ba266fa67821 --- /dev/null +++ "b/2224020150/chapter7/\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\244\247\346\267\261\345\272\246.c" @@ -0,0 +1,26 @@ +/** + * Definition for a binary tree node. + * struct TreeNode { + * int val; + * struct TreeNode *left; + * struct TreeNode *right; + * }; + */ +int maxDepth(struct TreeNode* root){ + int max = 0; + if(NULL==root){ + return 0; + } + if(NULL==root->left&&NULL==root->right){//是叶子结点 + return 1; + } + if(root->left){//有左子树 + int n = 1+maxDepth(root->left); + max = max>n?max:n; + } + if(root->right){//有右子树 + int n = 1+maxDepth(root->right); + max = max>n?max:n; + } + return max; +} diff --git "a/2224020150/chapter7/\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.c" "b/2224020150/chapter7/\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.c" new file mode 100644 index 0000000000000000000000000000000000000000..2c7d40c5122d1108e44b6a192ea10a3d058d53c6 --- /dev/null +++ "b/2224020150/chapter7/\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.c" @@ -0,0 +1,41 @@ +#include +#include +#define MaxVertexNum 100 +#define ElemType int +#define VertexType int +void FindSameGradeparents(int num[],int n,int a,int b){ + int index1=-1,index2=-1; + for(int i=1;i<=n;i++){ + if(num[i]==a) + index1=i; + if(num[i]==b) + index2=i; + } + //如果存在元素不在数组中,则没有公共祖先 + if(index1==-1 || index2==-1){ + printf("They don`t have a same gradeparents.\n"); + return ; + } + //如果元素都在数组中,则进行寻找最近的公共祖先 + //公告祖先必定存在(最远的为根结点),所以判断条件为num[index1]!=num[index2] + while(num[index1]!=num[index2]){ + index1/=2; + index2/=2; + } + if(num[index1] == num[index2]) + printf("The same gradeparents is %d\n",num[index1]); +} +int main(){ + int n; + printf("Input n:\n"); + scanf("%d",&n); + int num[n+1]; + for(int i=1;i<=n;i++){ + scanf("%d",&num[i]); + } + printf("Find the Same gradeparents.\n"); + int a,b; + scanf("%d %d",&a,&b); + FindSameGradeparents(num,n,a,b); + return 0; +} \ No newline at end of file diff --git "a/2224020150/chapter7/\344\273\216\345\205\210\345\272\217\344\270\216\344\270\255\345\272\217\351\201\215\345\216\206\345\272\217\345\210\227\346\236\204\351\200\240\344\272\214\345\217\211\346\240\221.c" "b/2224020150/chapter7/\344\273\216\345\205\210\345\272\217\344\270\216\344\270\255\345\272\217\351\201\215\345\216\206\345\272\217\345\210\227\346\236\204\351\200\240\344\272\214\345\217\211\346\240\221.c" new file mode 100644 index 0000000000000000000000000000000000000000..35dcb792f06660c774da4214bff33e3232c98391 --- /dev/null +++ "b/2224020150/chapter7/\344\273\216\345\205\210\345\272\217\344\270\216\344\270\255\345\272\217\351\201\215\345\216\206\345\272\217\345\210\227\346\236\204\351\200\240\344\272\214\345\217\211\346\240\221.c" @@ -0,0 +1,117 @@ +#include +#include +#include + +typedef char DataType; +typedef struct BiTNode +{ + DataType data; + struct BiTNode *lchild, *rchild; +}LinkBiTree; + + +// 根据先序、中序序列构建二叉树 +void CreateBiTree(LinkBiTree **T, char *PreStr, char *InStr, int L1, int R1, int L2, int R2) +{ + /* int i; + printf("先序序列:"); + for (i = L1; i <= R1; i++){printf("%c ", PreStr[i]);} + printf("\n中序序列:"); + for (i = L2; i <= R2; i++){printf("%c ", InStr[i]);} + printf("\n"); */ + + // 创建新的结点 + (*T) = (LinkBiTree *)malloc(sizeof(LinkBiTree)); + // 新结点数据为设置 先序序列 的第一个数据,即根节点数据 + (*T)->data = PreStr[L1]; + + // 根据先序第一个元素为根结点元素,寻找中序序列中的根结点位置 + int root; + for (root = 0; root <= R2; root++) + { + if (PreStr[L1] == InStr[root]) + { + printf("先序序列根节点(%c)在中序的位置 = %d\n", PreStr[L1], root); + break; + } + } + + + + // 判断中序序列中的左边是否存在左子序列 + if (root - L2 != 0) + { + // 划分两个区间,先序序列左子树区间 和 中序序列左子树区间,递归构建左子树 + + /* printf("\t划分先序序列的左子树区间:[%d, %d] = > ", L1 + 1, L1 + (root - L2)); + for (i = L1 + 1; i <= L1 + (root - L2); i++){printf("%c ", PreStr[i]);} + printf("\n"); + + printf("\t划分中序序列的左子树区间:[%d, %d] = > ", L2, root - 1); + for (i = L2; i <= root - 1; i++){printf("%c ", InStr[i]);} + printf("\n"); */ + + CreateBiTree(&(*T)->lchild, PreStr, InStr, L1 + 1, L1 + (root - L2), L2, root - 1); + } + else + { + (*T)->lchild = NULL; + } + + // 判断中序序列中的右边是否存在右子序列 + if (R2 - root != 0) + { + // 划分两个区间,先序序列右子树区间 和 中序序列右子树区间,递归构建右子树 + + /* printf("\t划分先序序列的右子树区间:[%d, %d] = > ", L1 + 1, L1 + (root - L2)); + for (i = R1 - (R2 - root) + 1; i <= R1; i++){printf("%c ", PreStr[i]);} + printf("\n"); + + printf("\t划分中序序列的右子树区间:[%d, %d] = > ", L2, root - 1); + for (i = root + 1; i <= R2; i++){printf("%c ", InStr[i]);} + printf("\n"); */ + + CreateBiTree(&(*T)->rchild, PreStr, InStr, R1 - (R2 - root) + 1, R1, root + 1, R2); + } + else + { + (*T)->rchild = NULL; + } +} + +// 后序遍历二叉树 +void PostOrderTraverse(LinkBiTree *T) +{ + if (T) + { + PostOrderTraverse(T->lchild); + PostOrderTraverse(T->rchild); + printf("%c ", T->data); + } +} + + +int main() +{ + char PreStr[30], InStr[30]; + + // 参考用例:ABCDEFG + printf("请输入先序序列:"); //测试用例:ABDEHJKLMNCFGI + scanf("%s", PreStr); + //参考用例:CBDAEFG + printf("请输入中序序列:"); //测试用例:DBJHLKMNEAFCGI + scanf("%s", InStr); + + int len1 = strlen(PreStr); + int len2 = strlen(InStr); + + LinkBiTree *T; + CreateBiTree(&T, PreStr, InStr, 0, len1 - 1, 0, len2 - 1); + + + printf("后序遍历二叉树:"); + PostOrderTraverse(T); + printf("\n"); + + return 0; +} diff --git "a/2224020150/chapter7/\345\234\250\346\257\217\344\270\252\346\240\221\350\241\214\344\270\255\346\211\276\346\234\200\345\244\247\345\200\274.c" "b/2224020150/chapter7/\345\234\250\346\257\217\344\270\252\346\240\221\350\241\214\344\270\255\346\211\276\346\234\200\345\244\247\345\200\274.c" new file mode 100644 index 0000000000000000000000000000000000000000..11c29441a0f81f19b5a5f2b023376acad38921fc --- /dev/null +++ "b/2224020150/chapter7/\345\234\250\346\257\217\344\270\252\346\240\221\350\241\214\344\270\255\346\211\276\346\234\200\345\244\247\345\200\274.c" @@ -0,0 +1,23 @@ +int max(int a,int b) +{ + return a>b?a:b; +} + +int work(struct TreeNode* r, int* ar, int f){ + ar[f]=max(ar[f],r->val); + if(r->left==NULL&&r->right==NULL)return f+1; + else if(r->left==NULL)return work(r->right,ar,f+1); + else if(r->right==NULL)return work(r->left,ar,f+1); + else return max(work(r->left,ar,f+1),work(r->right,ar,f+1)); +} + +int* largestValues(struct TreeNode* root, int* returnSize){ + int *array=(int *)malloc(5000*sizeof(int)); + for(int i=0;i<5000;i++)array[i]=-2147483648; + if(root==NULL){ + *returnSize=0; + return array; + } + *returnSize=work(root,array,0); + return array; +} \ No newline at end of file diff --git "a/2224020150/chapter7/\350\267\257\345\276\204\346\200\273\345\222\214.c" "b/2224020150/chapter7/\350\267\257\345\276\204\346\200\273\345\222\214.c" new file mode 100644 index 0000000000000000000000000000000000000000..e01b6a90561eca01313178e73a88611cf1f3011d --- /dev/null +++ "b/2224020150/chapter7/\350\267\257\345\276\204\346\200\273\345\222\214.c" @@ -0,0 +1,22 @@ +void hasPathSum1(struct TreeNode* root, int targetSum,int *sign){ +//sign为判断变量,初始值为0代表没找到,一旦找到就赋值为1; + if(root!=NULL){ + if(root->left==NULL&&root->right==NULL){ +//到叶子结点后判断; + if(root->val==targetSum){ + *sign=1; + } + }else{ +//没到叶子结点就把左孩子传下去,然后把当前结点值减去, +//将问题转化为为寻找左孩子的路径和是否为目标值; + if(root->left!=NULL) hasPathSum1(root->left,targetSum-root->val,sign); + if(root->right!=NULL) hasPathSum1(root->right,targetSum-root->val,sign); + } + } +} +bool hasPathSum(struct TreeNode* root, int targetSum){ + int sign=0; + hasPathSum1(root,targetSum,&sign); + if(sign==1)return true; + else return false; +} \ No newline at end of file diff --git "a/2224020150/chapter8/\344\272\214\350\277\233\345\210\266\347\237\251\351\230\265\344\270\255\347\232\204\346\234\200\347\237\255\350\267\257\345\276\204.c" "b/2224020150/chapter8/\344\272\214\350\277\233\345\210\266\347\237\251\351\230\265\344\270\255\347\232\204\346\234\200\347\237\255\350\267\257\345\276\204.c" new file mode 100644 index 0000000000000000000000000000000000000000..b383e5ebb68cc2123b80f4c7120e05886f4e8f49 --- /dev/null +++ "b/2224020150/chapter8/\344\272\214\350\277\233\345\210\266\347\237\251\351\230\265\344\270\255\347\232\204\346\234\200\347\237\255\350\267\257\345\276\204.c" @@ -0,0 +1,58 @@ +#include +#define N 256 +int dirs[8][2] = {{-1, 0}, {-1, 1}, {0, 1}, {1, 1}, + {1, 0}, {1, -1}, {0, -1}, {-1, -1}}; +// 八联通,按次序{{西}、{西北}、{北}、{东北}、{东}、{东南}、{南}、{西南}} + +bool inArea(int x, int y, int R, int C){ // 判断顶点(x, y)坐标是否越界 + return x >= 0 && x < R && y >= 0 && y < C; +} + +int shortestPathBinaryMatrix(int **grid, int gridSize, int *gridColSize) +{ + int R = gridSize; + int C = *gridColSize; + // 这俩个名字太长了,写到程序里非常冗余 + + /* 建立备忘录 */ + bool visited[N][N] = {0}; + int dis[N][N] = {0}; + + /* 判断边界条件 */ + if (grid[0][0] == 1) + return -1; + if ((R == 0 && C == 0) || (R == 1 && C == 1)) + return 1; + + /* 模拟队列 */ + int* pQueue = (int*)malloc(gridSize * (*gridColSize) * sizeof(int)); + int front = -1; // 队尾 + int rear = -1; // 队头 + + pQueue[++rear] = 0; // 将第 1 个顶点入队,从左上角(0,0)开始遍历 + visited[0][0] = true; // 标记为已访问 + dis[0][0] = 1; // 最短路径的长度为 1 + while ( front != rear ) // 队列不为空 + { + int cur = pQueue[++front]; // 出队 + int curx = cur / C, cury = cur % C; // 因为取出来是一个一维索引,但我们的数组是二维的,所以把一维索引转为二维索引 + for (int d = 0; d < 8; d++) // 八个方向都要看一下 + { + int nextx = curx + dirs[d][0]; // 更新下一个顶点的 x + int nexty = cury + dirs[d][1]; // 更新下一个顶点的 y + // 排除:1、越界 2、已访问 3、阻塞 + if (inArea(nextx, nexty, R, C) && !visited[nextx][nexty] && grid[nextx][nexty] == 0){ + pQueue[++rear] = nextx * C + nexty; // 顶点入队,但因为 pQueue 存储的是一维索引,所以要把二维索引转为一维索引 + visited[nextx][nexty] = true; // 标记为已访问 + dis[nextx][nexty] = dis[curx][cury] + 1; // 下一个顶点的值 = 原顶点的值 + 1,也就是最短路径的长度 + + if (nextx == R - 1 && nexty == C - 1){ // 终于遍历到了右下角 + free(pQueue); pQueue = NULL; // 防止野指针 + return dis[nextx][nexty]; // 最短路径的长度保存在最后一个元素 + } + } + } + } + free(pQueue); pQueue = NULL; // 防止野指针 + return -1; // 运行到这里,表示没找到 +} diff --git "a/2224020150/chapter8/\345\210\244\346\226\255\344\272\214\345\210\206\345\233\276.c" "b/2224020150/chapter8/\345\210\244\346\226\255\344\272\214\345\210\206\345\233\276.c" new file mode 100644 index 0000000000000000000000000000000000000000..105b0334afbf2882ff1ea61db0c29a0558b0bc64 --- /dev/null +++ "b/2224020150/chapter8/\345\210\244\346\226\255\344\272\214\345\210\206\345\233\276.c" @@ -0,0 +1,34 @@ +#include +int main() +{ + int arr[] = { 1,2,3,4,5,6,7,8,9,10,11,12,13 }; + int left = 0, right = sizeof(arr) / sizeof(arr[0]) - 1; + int x = 0,flag = 0; + + scanf("%d", &x);//要找的数 + + while (left <= right)//若要找的数在此数组中,此条件会一直成立; + //若要找的数不在此数组中,最终left会大于right,从循环中跳出 + { + int mid = (left + right) / 2; + if (x == arr[mid]) + { + printf("%d\n", mid); + flag = 1; + break; + } + else if (x > arr[mid]) + { + left = mid + 1; + } + else + { + right = mid - 1; + } + } + if (flag == 0)//只有当要找的数在数组中找不到时flag == 0 + { + printf("找不到\n"); + } + return 0; +} \ No newline at end of file diff --git "a/2224020150/chapter8/\346\211\276\345\210\260\345\260\217\351\225\207\347\232\204\346\263\225\345\256\230.c" "b/2224020150/chapter8/\346\211\276\345\210\260\345\260\217\351\225\207\347\232\204\346\263\225\345\256\230.c" new file mode 100644 index 0000000000000000000000000000000000000000..029b82719d017a48f4ca6294d3d32a44286a288f --- /dev/null +++ "b/2224020150/chapter8/\346\211\276\345\210\260\345\260\217\351\225\207\347\232\204\346\263\225\345\256\230.c" @@ -0,0 +1,19 @@ +int findJudge(int N, int** trust, int trustSize, int* trustColSize){ +//找到一个节点,这个节点被所有其他节点指向 + int people[1001][2] = {0}; + + for(int i = 0 ; i < trustSize;i++) + { + people[trust[i][0]][0]++;//出度 + people[trust[i][1]][1]++;//入度 + } + + for(int j = 1 ;j<=N;j++) + { + if((people[j][0] == 0)&&(people[j][1] == N-1)) + { + return j; + } + } + return -1; +} diff --git "a/2224020150/chapter8/\350\257\276\347\250\213\350\241\250.c" "b/2224020150/chapter8/\350\257\276\347\250\213\350\241\250.c" new file mode 100644 index 0000000000000000000000000000000000000000..d8a92e24c1f4875eece379cd94026fc7afa0e548 --- /dev/null +++ "b/2224020150/chapter8/\350\257\276\347\250\213\350\241\250.c" @@ -0,0 +1,207 @@ +#include +#include +#include +#include +#include +#include +#pragma comment(lib,"WINMM.LIB") +char course[99][99][99]; +void menu() +{ + printf("***************课表系统菜单****************\n"); + printf("***************1.显示课表******************\n"); + printf("***************2.修改课表******************\n"); + printf("***************3.保存课表******************\n"); + printf("***************4.新建课表******************\n"); + printf("***************5.查看当前课程**************\n"); + printf("***************0.退出**********************\n"); +} +//课程表输出函数 +void print() +{ + FILE *fp; + char time[5][99]={"8:00~8:55","10:00~10:55","14:00~14:55","16:00~16:55","19:00~19:55"};//设置时间 + int i,j; + if((fp=fopen("g:\\cource.txt","r"))==NULL) + { + printf("can't open this file\n"); + exit(1); + } + printf("----------------------------------------------------------------------\n"); + printf("时间 星期一 星期二 星期三 星期四 星期五 星期六 星期天\n"); + printf("----------------------------------------------------------------------\n"); + for(i=0;i<5;i++) //将课程表从文件中读出 + { + printf("%5s\t",time[i]); + for(j=0;j<7;j++) + { + fscanf(fp,"%s",course[i][j]); + printf("%-8s",course[i][j]); + } + printf("\n"); + printf("---------------------------------------------------------------------\n"); + } + fclose(fp); + //recenttime(); + printf("\n\n"); + +} +void change()//修改函数 +{ + char temp[10]; + char s; + int date,time; + char recourse[10]; + printf("输入你想要改的课程时间(星期1~7)"); + scanf("%d",&date); + while(date<1||date>7) + { + printf("输入时间不合理,重新输入"); + scanf("%d",&date); + } + printf("输入你想要改的课程课次(1~5)"); + scanf("%d",&time); + while(date<1||date>5) + { + printf("输入时间不合理,重新输入"); + scanf("%d",&time); + } + printf("你要将原来的课程修改为:"); + scanf("%s",recourse); + strcpy(temp,recourse); + getchar(); + strcpy(course[time-1][date-1],recourse); + printf("是否保存(1 表示保存,否则不保存)"); + scanf("%c",&s); + if(s=='1') + savecourse(); + else + { + strcpy(course[time-1][date-1],temp); + } +} +int savecourse()//保存函数 +{ + FILE *fp; + int i,j; + if((fp=fopen("g:\\cource.txt","w"))==NULL) + { + printf("can't open this file"); + exit(1); + } + for(i=0;i<5;i++) + { + for(j=0;j<7;j++) + fprintf(fp,"%-8s",course[i][j]); + fprintf(fp,"\n"); + } + fclose(fp); + printf("保存成功!\n"); +} +void newcourse()//课表重置函数 +{ + int i,j; + printf("请一次输入一周的课程按回车开始\n"); + for(i=0;i<7;i++) + { + printf("请输入第%d天的课程:",i+1); + for(j=0;j<7;j++) + scanf("%s",course[i][j]); + } + savecourse(); +} +void recenttime() +{ + struct tm *ptr; +time_t it; +int y,d,m,iWeek,h,min,l,c; +char s[99]; +it=time(NULL); +ptr=localtime(&it); +y=ptr->tm_year+1900; +d=ptr->tm_mday; +m=ptr->tm_mon+1; +h=ptr->tm_hour; +min=ptr->tm_min; //时间转换程序 +printf("当前时间为%4d年%02d月%02d日 %d:%d:%d ",ptr->tm_year+1900,ptr->tm_mon+1,ptr->tm_mday,ptr->tm_hour,ptr->tm_min,ptr->tm_sec); + iWeek=(d+2*m+3*(m+1)/5+y+y/4-y/100+y/400)%7; + switch(iWeek) + { + case 0: strcpy(s,"星期一");printf("%s\n",s); break; + case 1: strcpy(s,"星期二");printf("%s\n",s); break; + case 2: strcpy(s,"星期三");printf("%s\n",s); break; + case 3: strcpy(s,"星期四");printf("%s\n",s); break; + case 4: strcpy(s,"星期五");printf("%s\n",s); break; + case 5: strcpy(s,"星期六");printf("%s\n",s); break; + case 6: strcpy(s,"星期天");printf("%s\n",s); break; + } + if((h<8||h>19)||h==9||h==11||h==12||h==13||h==15||h==17||h==18) + { + printf("当前无课");exit(1); + } + switch(h) + { + case 8:l=0;break; + case 10:l=1;break; + case 14:l=2;break; + case 16:l=3;break; + case 19:l=4;break; + } + if(strcmp(s,"星期一")==0) c=0; + if(strcmp(s,"星期二")==0) c=1; + if(strcmp(s,"星期三")==0) c=2; + if(strcmp(s,"星期四")==0) c=3; + if(strcmp(s,"星期五")==0) c=4; + if(strcmp(s,"星期六")==0) c=5; + if(strcmp(s,"星期天")==0) c=6; + + printf("要上的课程为 %s\n",course[l][c]); + +} + + + +int main() +{ + int select; + int sign=1; + char contin; + FILE *fp; + int i,j; + if((fp=fopen("g:\\cource.txt","r"))==NULL) + { + printf("can't open this file"); + exit(1); + } + for(i=0;i<5;i++) + { + for(j=0;j<7;j++) + fscanf(fp,"%s",course[i][j]); + + } + + fclose(fp); + while(sign) + { + menu(); + //music(); + printf("请输入操作:"); + scanf("%d",&select); + while(select<0||select>5) + { + printf("你输入的操作不合法,请重新输入"); + scanf("%d",&select); + + } + switch(select) + { + case 1:print();break; + case 2:change();break; + case 3:savecourse();break; + case 4:newcourse();break; + case 5:recenttime();break; + case 0:printf("谢谢使用");sign=0;break; + } + } + +} diff --git "a/2224020150/chapter8/\350\277\236\346\216\245\346\211\200\346\234\211\347\202\271\347\232\204\346\234\200\345\260\217\350\264\271\347\224\250.c" "b/2224020150/chapter8/\350\277\236\346\216\245\346\211\200\346\234\211\347\202\271\347\232\204\346\234\200\345\260\217\350\264\271\347\224\250.c" new file mode 100644 index 0000000000000000000000000000000000000000..3bdf766490a29f4e3c827973daf44aa7a107f163 --- /dev/null +++ "b/2224020150/chapter8/\350\277\236\346\216\245\346\211\200\346\234\211\347\202\271\347\232\204\346\234\200\345\260\217\350\264\271\347\224\250.c" @@ -0,0 +1,53 @@ +#include +#define INFINITY INT_MAX // 定义无限大值为INT_MAX + +// 图结构体 +typedef struct { + int vertex; // 顶点数量 + int edge[10][10]; // 边表示两个顶点之间的距离或者费用 +} Graph; + +int minCost(Graph graph) { + int costMatrix[graph.vertex][graph.vertex] = {{INFINITY}}; // 初始化成无限大值 + + for (int i = 0; i < graph.vertex; ++i) { + for (int j = 0; j < graph.vertex; ++j) { + if (i == j || graph.edge[i][j] != -1) { + costMatrix[i][j] = graph.edge[i][j]; // 如果存在边则将其赋值到costMatrix中 + } else { + costMatrix[i][j] = INFINITY; // 否则设置为无限大值 + } + } + } + + // Floyd-Warshall算法 + for (int k = 0; k < graph.vertex; ++k) { + for (int i = 0; i < graph.vertex; ++i) { + for (int j = 0; j < graph.vertex; ++j) { + if (costMatrix[i][k] + costMatrix[k][j] < costMatrix[i][j]) { + costMatrix[i][j] = costMatrix[i][k] + costMatrix[k][j]; // 更新路径上的最小费用 + } + } + } + } + + return costMatrix[0][graph.vertex - 1]; // 返回起点到终点的最小费用 +} + +int main() { + Graph graph; + graph.vertex = 5; // 顶点数量 + + // 创建图的边表示关系(这里只展示了部分) + graph.edge[0][1] = 2; + graph.edge[0][3] = 4; + graph.edge[1][2] = 6; + graph.edge[1][4] = 8; + graph.edge[2][3] = 7; + graph.edge[2][4] = 9; + graph.edge[3][4] = 5; + + printf("最小费用为 %d\n", minCost(graph)); + + return 0; +} \ No newline at end of file diff --git "a/2224020150/chapter9/\344\272\214\345\210\206\346\237\245\346\211\276.c" "b/2224020150/chapter9/\344\272\214\345\210\206\346\237\245\346\211\276.c" new file mode 100644 index 0000000000000000000000000000000000000000..e9ca66c578011a1ae39c40da3eb3cea066418108 --- /dev/null +++ "b/2224020150/chapter9/\344\272\214\345\210\206\346\237\245\346\211\276.c" @@ -0,0 +1,32 @@ +#include + +int binarySearch(int arr[], int left, int right, int target) { + while (left <= right) { + int mid = left + (right - left) / 2; // 计算中间位置索引 + + if (arr[mid] == target) { + return mid; // 如果目标值等于数组中间元素,返回该索引 + } else if (arr[mid] > target) { + right = mid - 1; // 如果目标值小于数组中间元素,在左侧子数组进行查找 + } else { + left = mid + 1; // 如果目标值大于数组中间元素,在右侧子数组进行查找 + } + } + + return -1; // 若未找到目标值,则返回-1表示不存在 +} + +int main() { + int arr[] = {4, 7, 9, 10, 15}; + int n = sizeof(arr) / sizeof(arr[0]); + int target = 9; + int result = binarySearch(arr, 0, n - 1, target); + + if (result != -1) { + printf("目标值 %d 在数组中的索引为 %d\n", target, result); + } else { + printf("目标值 %d 不存在于数组中\n", target); + } + + return 0; +} \ No newline at end of file diff --git "a/2224020150/chapter9/\344\272\214\345\217\211\346\216\222\345\272\217\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.c" "b/2224020150/chapter9/\344\272\214\345\217\211\346\216\222\345\272\217\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.c" new file mode 100644 index 0000000000000000000000000000000000000000..7a1bad8a39117dd656b54bd1bb4f4b5b976350bb --- /dev/null +++ "b/2224020150/chapter9/\344\272\214\345\217\211\346\216\222\345\272\217\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.c" @@ -0,0 +1,27 @@ +/** + * Definition for a binary tree node. + * struct TreeNode { + * int val; + * struct TreeNode *left; + * struct TreeNode *right; + * }; + */ +struct TreeNode* lowestCommonAncestor(struct TreeNode* root, struct TreeNode* p, struct TreeNode* q) { + struct TreeNode* t; + if(p->val>q->val){ + t=p; + p=q; + q=t; + } + while(root){ + if((root->val)>(p->val) && (root->val)<(q->val)) + return root; + if((root->val)==(p->val) || (root->val)==(q->val)) + return root; + if(root->left && root->val>q->val) + root=root->left; + if(root->right && root->valval) + root=root->right; + } + return NULL; +} diff --git "a/2224020150/chapter9/\344\273\245\345\270\270\346\225\260\346\227\266\351\227\264\346\217\222\345\205\245,\345\210\240\351\231\244\345\222\214\350\216\267\345\217\226\351\232\217\346\234\272\345\205\203\347\264\240.c" "b/2224020150/chapter9/\344\273\245\345\270\270\346\225\260\346\227\266\351\227\264\346\217\222\345\205\245,\345\210\240\351\231\244\345\222\214\350\216\267\345\217\226\351\232\217\346\234\272\345\205\203\347\264\240.c" new file mode 100644 index 0000000000000000000000000000000000000000..128619848b1ea0818853a07684e50fb54dbdae12 --- /dev/null +++ "b/2224020150/chapter9/\344\273\245\345\270\270\346\225\260\346\227\266\351\227\264\346\217\222\345\205\245,\345\210\240\351\231\244\345\222\214\350\216\267\345\217\226\351\232\217\346\234\272\345\205\203\347\264\240.c" @@ -0,0 +1,57 @@ +typedef struct{ + int val; + UT_hash_handle hh; +}RandomizedSet; +RandomizedSet *myrandomizedSet = NULL; +RandomizedSet* randomizedSetCreate() { + return (RandomizedSet *)calloc(1,sizeof(RandomizedSet)); +} + +bool randomizedSetInsert(RandomizedSet* obj, int val) { + RandomizedSet *temp; + HASH_FIND_INT(myrandomizedSet, &val, temp); + if(temp == NULL){ + temp = (RandomizedSet *)calloc(1,sizeof(RandomizedSet)); + temp->val = val; + HASH_ADD_INT(myrandomizedSet, val, temp); + return true; + } + return false; +} + +bool randomizedSetRemove(RandomizedSet* obj, int val) { + RandomizedSet *temp; + HASH_FIND_INT(myrandomizedSet,&val,temp); + if(temp != NULL){ + HASH_DEL(myrandomizedSet,temp); + free(temp); + return true; + } + return false; +} +int randomizedSetGetRandom(RandomizedSet* obj) { + //计算哈希表元素的个数 + int cnt = HASH_COUNT(myrandomizedSet); + //随机生成一个数字val,val的范围为 0 ~ (HASH_COUNT(mySet)-1). + //表示哈希表第val个元素 + int val = rand() % cnt; + RandomizedSet *current, *temp; + cnt = 0; + //循环移动指针current,当指针指向第val个元素时跳出循环 + HASH_ITER(hh, myrandomizedSet, current, temp) { + if (cnt++ >= val) { + break; + } + } + //返回指针指向结构中的val + return current->val; +} + + +void randomizedSetFree(RandomizedSet* obj) { + RandomizedSet *current, *temp; + HASH_ITER(hh, myrandomizedSet, current, temp) { + HASH_DEL(myrandomizedSet, current); + free(current); + } +} \ No newline at end of file diff --git "a/2224020150/chapter9/\345\257\273\346\211\276\344\270\244\344\270\252\346\255\243\345\272\217\346\225\260\347\273\204\347\232\204\344\270\255\344\275\215\346\225\260.c" "b/2224020150/chapter9/\345\257\273\346\211\276\344\270\244\344\270\252\346\255\243\345\272\217\346\225\260\347\273\204\347\232\204\344\270\255\344\275\215\346\225\260.c" new file mode 100644 index 0000000000000000000000000000000000000000..db24a014a20d919e0696ffd0346e9194fb8f5c38 --- /dev/null +++ "b/2224020150/chapter9/\345\257\273\346\211\276\344\270\244\344\270\252\346\255\243\345\272\217\346\225\260\347\273\204\347\232\204\344\270\255\344\275\215\346\225\260.c" @@ -0,0 +1,38 @@ +int erfen(int *nums1,int *nums2, int nums1Size,int nums2Size, int k){ + int i=0,j=0; + while(true){ + + if(nums1Size==i){ + return nums2[j+k-1]; + } + if(nums2Size==j){ + return nums1[i+k-1]; + } + if(k==1){ + return nums1[i]>nums2[j]?nums2[j]:nums1[i]; + } + //中间不断判断更新k值的过程 + int newi =(i + k / 2 - 1)>(nums1Size - 1)?nums1Size - 1:(i + k / 2 - 1); + int newj =(j + k / 2 - 1)>(nums2Size - 1)?nums2Size - 1:(j + k / 2 - 1); + int pivot1 = nums1[newi]; + int pivot2 = nums2[newj]; + //比较这两个数 + if (pivot1 <= pivot2) { + k -= newi - i + 1; + i = newi + 1; + } + else { + k -= newj - j + 1; + j = newj + 1; + } + } +} +double findMedianSortedArrays(int* nums1, int nums1Size, int* nums2, int nums2Size){ + int numsSize=nums1Size+nums2Size; + if(numsSize%2==1){ + return(erfen(nums1,nums2,nums1Size,nums2Size,(numsSize+1)/2)); + } + else{ + return (erfen(nums1, nums2,nums1Size, nums2Size,numsSize / 2) + erfen(nums1,nums2,nums1Size,nums2Size, numsSize / 2 + 1)) / 2.0; + } +} \ No newline at end of file diff --git "a/2224020150/chapter9/\345\271\263\350\241\241\344\272\214\345\217\211\346\240\221.c" "b/2224020150/chapter9/\345\271\263\350\241\241\344\272\214\345\217\211\346\240\221.c" new file mode 100644 index 0000000000000000000000000000000000000000..e2113fe73cbcb0d6306775ec155d60b2a6635ea6 --- /dev/null +++ "b/2224020150/chapter9/\345\271\263\350\241\241\344\272\214\345\217\211\346\240\221.c" @@ -0,0 +1,51 @@ +#include +#include + +// 定义二叉树节点结构体 +typedef struct Node { + int data; // 数据元素 + struct Node* left; // 左子树指针 + struct Node* right; // 右子树指针 +} Node; + +// 创建新节点函数 +Node* createNewNode(int value) { + Node* newNode = (Node*)malloc(sizeof(Node)); // 分配内存空间 + if (!newNode) { + printf("Memory allocation failed.\n"); + exit(-1); + } + + newNode->data = value; // 设置节点值 + newNode->left = NULL; // 初始化左子树为NULL + newNode->right = NULL; // 初始化右子树为NULL + + return newNode; // 返回新节点指针 +} + +// 获取二叉树的最大深度函数 +int getMaxDepth(Node* root) { + if (root == NULL) { // 如果根节点为空则返回0 + return 0; + } else { + int leftHeight = getMaxDepth(root->left); // 计算左子树的最大深度 + int rightHeight = getMaxDepth(root->right); // 计算右子树的最大深度 + + return ((leftHeight > rightHeight) ? (leftHeight + 1):(rightHeight + 1)); // 返回左右子树最大深度加上当前节点所在层次 + } +} + +int main() { + Node* root = createNewNode(5); // 创建根节点 + root->left = createNewNode(3); // 创建左子树节点 + root->right = createNewNode(7); // 创建右子树节点 + root->left->left = createNewNode(2); // 创建左子树的左子树节点 + root->left->right = createNewNode(4); // 创建左子树的右子树节点 + root->right->left = createNewNode(6); // 创建右子树的左子树节点 + root->right->right = createNewNode(8); // 创建右子树的右子树节点 + + int maxDepth = getMaxDepth(root); // 调用获取最大深度函数 + printf("The maximum depth of the binary tree is %d\n", maxDepth); + + return 0; +} \ No newline at end of file diff --git "a/2224020150/chapter9/\346\225\260\347\273\204\344\270\255\347\232\204\347\254\254k\344\270\252\346\234\200\345\244\247\345\205\203\347\264\240.c" "b/2224020150/chapter9/\346\225\260\347\273\204\344\270\255\347\232\204\347\254\254k\344\270\252\346\234\200\345\244\247\345\205\203\347\264\240.c" new file mode 100644 index 0000000000000000000000000000000000000000..bb3713d3a1586c8f69415e4070eca1f564766013 --- /dev/null +++ "b/2224020150/chapter9/\346\225\260\347\273\204\344\270\255\347\232\204\347\254\254k\344\270\252\346\234\200\345\244\247\345\205\203\347\264\240.c" @@ -0,0 +1,37 @@ +void swap(int *a,int *b){//交换函数 + int temp = *a; + *a = *b; + *b = temp; +} +int partition(int *nums,int left,int right){//分治 + int j = left;//j指向待交换的位置 + for(int i = left;inums[i]){ + swap(nums+i,nums+j); + j++;//每次交换,待交换的位置++ + } + } + swap(nums+j,nums+right);//分治结束时,j是分治点的最终位置 + return j; +} +int randomPartition(int *nums,int left, int right){//随机分治 + int i = rand()%(right - left + 1)+left;//取left-right区间内随机一个数 + int temp = nums[right]; + nums[right] = nums[i]; + nums[i] = temp;//把nums[i]放在区间最右边 + return partition(nums,left,right);//分治 +} +int quickSelect(int *nums,int left,int right,int index){//快速选择 + int q = randomPartition(nums,left,right);//随机分治,获取被选择元素的下标 + if(index == q){//分治获取的下标等于索引下标//分治最终目的。 + return nums[q]; + }else if(q max[0]) { + max[2] = max[1]; + max[1] = max[0]; + max[0] = nums[i]; + } + else if (nums[i] < max[0] && nums[i] > max[1]) { + max[2] = max[1]; + max[1] = nums[i]; + } + else if (nums[i] < max[1] && nums[i] > max[2]) { + max[2] = nums[i]; + } + } + if (max[2] != LONG_MIN) { + return max[2]; + } + return max[0]; +} + diff --git "a/2224020150/chapter9/\350\256\276\350\256\241\345\223\210\345\270\214\351\233\206\345\220\210.c" "b/2224020150/chapter9/\350\256\276\350\256\241\345\223\210\345\270\214\351\233\206\345\220\210.c" new file mode 100644 index 0000000000000000000000000000000000000000..5fa999c02093cf643f8e3b41acf9de8c0715130c --- /dev/null +++ "b/2224020150/chapter9/\350\256\276\350\256\241\345\223\210\345\270\214\351\233\206\345\220\210.c" @@ -0,0 +1,106 @@ +#define maxsize 769 + +typedef struct note{ + int value; + struct note * next; +}List; + + +typedef struct { + List * hash[maxsize]; +} MyHashSet; + +/** Initialize your data structure here. */ + +MyHashSet* myHashSetCreate() { + MyHashSet* myhashset =(MyHashSet *)malloc(sizeof(MyHashSet)); + for(int i =0;ihash[i]=(List *)malloc(sizeof(List)); + myhashset->hash[i]->value=i; + myhashset->hash[i]->next =NULL; + } + return myhashset; + +} +//插入元素到hash数组中得每一个元素得链表中,只需传入链表指针即可 +void addList(List * a,int value) +{ + List * new = (List *)malloc(sizeof(List)); + List * tem = a->next; + new ->value = value; + new->next=tem; + a->next= new; +} + + + + +/* 链表元素释放 */ +void freeList(List *list) { + List *cur = list; + List *tmp = NULL; + while (cur != NULL) { + tmp = cur; + cur = cur->next; + free(tmp); + } +} + +void deletList(List *a ,int value) +{ + List * cur = a->next; + List * past = a; + while(cur) + { + if(cur->value==value) + { + past->next = cur->next; + free(cur); + return; + } + past=cur; + cur=cur->next; + } +} + + bool IsContain(List* a,int key) + { + List * temp = a->next; + while(temp) + { + if(temp->value==key) + { + return true; + } + temp=temp->next; + } + return false; + + } +void myHashSetAdd(MyHashSet* obj, int key) { + int a = key%maxsize; + if(IsContain(obj->hash[a],key)) + { + return ; + } + addList(obj->hash[a], key); +} + +void myHashSetRemove(MyHashSet* obj, int key) { + int a = key%maxsize; + deletList(obj->hash[a],key); +} + +/** Returns true if this set contains the specified element */ +bool myHashSetContains(MyHashSet* obj, int key) { + return IsContain(obj->hash[key%maxsize],key); +} + +void myHashSetFree(MyHashSet* obj) { + for(int i=0;ihash[i]); + } + free(obj); +}