This commit is contained in:
2025-09-15 21:12:04 +08:00
commit 3f58f483ff
144 changed files with 5298 additions and 0 deletions

16
we/23-1.h Normal file
View File

@@ -0,0 +1,16 @@
#include <string.h>
int countSubstring(char *A,char *B){
int count = 0;
char *p = A;
while(*p != '\0'){
p = strstr(p,B);
if(p != NULL){
count++;
p += strlen(B);
}else{
break;
}
}
return count;
}

60
we/23-2.h Normal file
View File

@@ -0,0 +1,60 @@
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
struct ListNode {
char *val;
int count;
struct ListNode *next;
};
struct ListNode *initNode(char *val) {
struct ListNode *node = (struct ListNode *)malloc(sizeof(struct ListNode));
node->val = val;
node->next = NULL;
return node;
}
// 返回值带头节点
struct ListNode *process(const char *fileName){
struct ListNode *head = initNode("");
FILE *fp = fopen(fileName, "r");
if(fp == NULL) {
printf("open file failed\n");
return head;
}
char buffer[1024];
char *words[1000];
int wordCount = 0;
while (fscanf(fp, "%s", buffer) != EOF && wordCount < 1000) {
words[wordCount] = strdup(buffer); // 使用 strdup 复制字符串
wordCount++;
}
int tag = 0; // 0 表示还没有处理1 表示已经处理
for (int i = 0; i < wordCount; i++) {
struct ListNode *p = head;
tag = 0;
while(p->next != NULL) {
if(strcmp(p->next->val, words[i]) == 0){
p->next->count++;
tag = 1;
break;
}
p = p->next;
}
if(tag == 0) {
struct ListNode *node = initNode(words[i]);
node->count = 1;
p->next = node;
}
}
fclose(fp);
return head;
}

86
we/23-3.c Normal file
View File

@@ -0,0 +1,86 @@
#include <stdlib.h>
#include <stdio.h>
struct student{
int stuId;
char *name;
int score;
};
void inputData(struct student *s, int size){
for(int i = 0; i < size; i++){
printf("请输入第%d个学生的学号、姓名、成绩", (i + 1));
printf("学生学号:");
scanf("%d", &s[i].stuId);
s[i].name = (char *)malloc(50 * sizeof(char));
if (s[i].name == NULL)
{
printf("内存分配失败\n");
}
printf("学生姓名:");
scanf("%s", s[i].name);
printf("学生成绩:");
scanf("%d", &s[i].score);
}
}
void bubbleSort(struct student *s, int size){
for(int i = 0;i<size;i++){
for(int j = 0;j<size-i-1;j++){
if(s[j].score > s[j+1].score){
struct student temp = s[j];
s[j] = s[j+1];
s[j+1] = temp;
}
}
}
}
struct student *findScore(struct student *s, int size, int score){
int left = 0;
int right = size - 1;
int mid;
while(left <= right){
mid = left + (right - left) / 2;
if(s[mid].score == score){
return &s[mid];
}else if (s[mid].score > score){
right = mid - 1;
}else
{
left = mid + 1;
}
}
return NULL;
}
int main(){
int size;
printf("请输入学生人数:");
scanf("%d",&size);
struct student *s = (struct student *)malloc(sizeof(struct student)*size);
inputData(s, size);
bubbleSort(s, size);
for(int i = 0;i<size;i++){
printf("学号:%d姓名%s成绩%d\n", s[i].stuId, s[i].name, s[i].score);
}
printf("请输入要查找学生的成绩:");
int score;
scanf("%d",&score);
struct student *p = findScore(s, size, score);
if(p == NULL){
printf("未找到此成绩的学生\n");
}else{
printf("学号:%d姓名%s成绩%d\n",p->stuId,p->name,p->score);
}
for (int i = 0; i < size; i++)
{
free[s[i].name];
}
free(s);
return 0;
}

73
we/23-4.c Normal file
View File

@@ -0,0 +1,73 @@
#include <stdlib.h>
#include <stdbool.h>
struct Toy
{
int val; // 玩具的编号
struct Toy *next;
};
void print(struct Toy *head){
struct Toy *temp = head->next;
while(temp != NULL){
printf("%d-",temp->val);
temp = temp->next;
}
printf("\n");
}
struct Toy *createToy(int val){
struct Toy *t = (struct Toy *)malloc(sizeof(struct Toy));
if(t == NULL){
return NULL;
}
t->val = val;
return t;
}
bool init(struct Toy *head){
struct Toy *temp = head;
for(int i = 1;i<=10;i++){
temp->next = createToy(i);
temp = temp->next;
if(temp == NULL){
return false;
}
}
return true;
}
void game(struct Toy *head,int val){
struct Toy *temp = head;
while (temp->next != NULL)
{
if(temp->next->val == val){
break;
}
temp = temp->next;
}
struct Toy *t = temp->next;
temp->next = t->next;
t->next = head->next;
head->next = t;
}
int main(){
struct Toy *head = createToy(-1); //头节点
if(head == NULL){
printf("malloc fail\n");
return -1;
}
bool in = init(head);
if(in == false){
printf("init fail\n");
return -1;
}
print(head);
game(head,3); // 骰子数为3
print(head);
return 0;
}

26
we/24-1.h Normal file
View File

@@ -0,0 +1,26 @@
#include <math.h>
int getN(long int a){
int ret = 0;
while (a)
{
ret++;
a/=10;
}
return ret;
}
long int findMO(long int a){
int ret = 0;
int i = 0;
int n = getN(a);
while(a!=0){
int div = pow(10,n--);
int temp = a/div;
a%=div;
if(temp%2 == 1){
ret += temp*(int)pow(10,i++);
}
}
return ret;
}

33
we/24-2.h Normal file
View File

@@ -0,0 +1,33 @@
#include <string.h>
#include <stdlib.h>
char* destar(char *str){
int length = strlen(str);
char *ret = (char *)malloc(sizeof(char)*length);
if(ret == NULL){
return "error";
}
int left=0,right=length-1;
while (str[left++]=='*');
while (str[right--]=='*');
strncpy(ret,str,left);
char temp;
int i = left;
while (left<=right)
{
temp = str[left++];
if((temp >= 'a' && temp <= 'z') ||
(temp >= 'A' && temp <= 'Z')) {
ret[i++] = temp;
}else if(temp=='*'){
continue;
}else{
free(ret);
return "error";
}
}
strncpy(ret+i,str+right+1,length - (right + 1));
ret[length - (left - i)] = '\0';
return ret;
}

25
we/24-3.h Normal file
View File

@@ -0,0 +1,25 @@
struct List{
int val;
struct List * next;
};
void bubbleListSort(List *head) {
if (head == nullptr) return; // 空链表检查
List *temp2;
int temp;
bool swapped;
do {
swapped = false;
temp2 = head;
while (temp2->next) {
if (temp2->next->val < temp2->val) {
temp = temp2->val;
temp2->val = temp2->next->val;
temp2->next->val = temp;
swapped = true;
}
temp2 = temp2->next;
}
} while (swapped); // 如果没有发生交换,提前结束
}

114
we/24-4.h Normal file
View File

@@ -0,0 +1,114 @@
#include <stdlib.h>
#include <string.h>
#include <limits.h>
int find(char **species,int size,char *str){
for(int i = 0; i<size;i++){
if (strcmp(species[i], str) == 0)
{
return i;
}
}
return -1;
}
// 传入咒语字符串数组二维数组eg. {{"猫""alal","鱼"},{x,x,x}} 代表猫变老鼠咒语为"alal"
char *find_best_animal(char ***curses, int cursesSize)
{
char **species = (char **)malloc(sizeof(char *) * 2 * cursesSize);
if(species == NULL){
return "error,fail to malloc memory";
}
int size = 0;
for(int i = 0;i<cursesSize;i++){ //统计个数并建立映射关系时间复杂度为o(n^2),使用哈希表(c语言哈希表篇幅过长)可以降到o(1)
for(int j=0;j<3;j++){
if(j==1){
continue;
}
if(find(species,size,curses[i][j])==-1){
species[size++]=curses[i][j];
}
}
}
int **matrix = (int**)malloc(sizeof(int*) * size); // 准备空间以便后续弗洛伊德操作
if(matrix == NULL){
return "error,fail to malloc memory";
}
for(int i = 0; i < size; i++){
matrix[i] = (int*)malloc(sizeof(int) * size);
if(matrix[i] == NULL){
// 释放之前分配的内存
for(int j = 0; j < i; j++){
free(matrix[j]);
}
free(matrix);
return "error,fail to malloc memory";
}
// 初始化数组元素为-1
memset(matrix[i], -1, sizeof(int) * size);
}
for(int i=0;i<cursesSize;i++){
int x = find(species,size,curses[i][0]);
int y = find(species,size,curses[i][2]);
int len = strlen(curses[i][1]);
// 保留最短路径
if(matrix[x][y] == -1 || len < matrix[x][y]) {
matrix[x][y] = len;
}
}
for(int i = 0;i<size;i++){ // 实现弗洛伊德操作
for(int j = 0;j<size;j++){
if(j==i || matrix[j][i] == -1){
continue;
}
for(int k = 0;k<size;k++){
if(k==i || matrix[i][k] == -1 || j==k){
continue;
}
// 更新最短路径
if(matrix[j][k] == -1 || matrix[j][i] + matrix[i][k] < matrix[j][k]) {
matrix[j][k] = matrix[j][i] + matrix[i][k];
}
}
}
}
int res = -1;
int min = INT_MAX;
for(int i = 0;i<size;i++){
int cost = 0;
int valid = 1;
for(int j = 0;j<size;j++){
if(i != j){
if(matrix[i][j] == -1){
valid = 0;
break;
}
cost += matrix[i][j];
}
}
if(valid && min > cost){
min = cost;
res = i;
}
}
// 释放内存
for(int i = 0; i < size; i++){
free(matrix[i]);
}
free(matrix);
char *animal = res != -1 ? species[res] : NULL;
free(species);
return animal;
}

14
we/24-sy-1.c Normal file
View File

@@ -0,0 +1,14 @@
#include <stdbool.h>
bool isPalindrome(char *s){
int len = strlen(s);
int left = 0, right = len - 1;
while (left < right) {
if (s[left] != s[right]) {
return false;
}
left++;
right--;
}
return true;
}

36
we/24-sy-2.c Normal file
View File

@@ -0,0 +1,36 @@
#include <stdlib.h>
struct ListNode
{
int val;
struct ListNode *next;
};
// 带头节点的单链表插入排序
void insertSort(struct ListNode *head){
struct ListNode *p = head->next;
struct ListNode *q;
if (p == NULL || p->next == NULL) return; // 空链表或单节点无需排序
while(p->next) {
q = head;
while (q->next != p->next) {
if(q->next->val < p->next->val) {
q = q->next;
} else {
// 插入操作
struct ListNode *temp = p->next;
p->next = temp->next;
temp->next = q->next;
q->next = temp;
break; // 插入完成后立即跳出
}
}
// 只有没发生插入时才移动p指针
if (q->next == p->next) {
p = p->next;
}
}
}

18
we/24-sy-3.c Normal file
View File

@@ -0,0 +1,18 @@
#include <stdlib.h>
#include <string.h>
char *strcpys(char *dest,char *src){
int len1 = strlen(dest);
int len2 = strlen(src);
char *res = (char *)malloc(sizeof(char)*(len1+len2+1));
if(!res){
printf("malloc failed\n");
return NULL;
}
memcpy(res,dest,len1);
memcpy(res+len1,src,len2);
res[len1+len2] = '\0';
free(src);
return res;
}