news 2026/7/9 3:03:18

408代码题汇总

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
408代码题汇总
#include<stdio.h> //数组算法题 //10年 void fun1(int r[], int l, int r) { int a = l, j = r; while(a < b) { int temp = r[a]; r[a] = r[b]; r[b] = temp; a++;b--; } } void fun2(int r[], int n, int p) { if(p > 0 && p < n) { fun1(r,0,n-1); fun1(r,0,n-p-1); fun1(r,n-p,n-1); } } //11年 int fun(int A[],int B[],int n){ int i = j = count = 0; while(1){ count++; if(A[i] > B[j]) j++; else i++; if(count == (n - 1) / 2) break; } if(A[i] > B[j]) return B[j]; else return A[i]; } //13年 找主元素 int fun(int A[], int n){ int i, count = 1, temp = A[0]; for(i = 1; i < n; i ++ ) { if(count == 0) temp = A[i]; else { if(A[i] == temp) count ++; else count --; } } count = 0; for(i = 0; i <= n - 1; i ++ ) if(A[i] == temp)count ++; if(count > n / 2)return temp; else return -1; } //16年 void quickSort(int arr[],int l,int r)//left和right的首字母 { if(l>=r) return; int base,temp;int i=l,j=r; base = arr[l]; //取最左边的数为基准数 while(i<j){ while(arr[j]>=base&&i<j)j--;//顺序很重要 while(arr[i]<=base&&i<j) i++; if(i < j) {temp = arr[i];arr[i] = arr[j];arr[j] = temp;} }//基准数归位 arr[l]=arr[i];arr[i]=base; quickSort(arr,l,i-1);//递归左边 quickSort(arr,i+1,r);//递归右边 } int fun(int A[], int n){ quickSort(A, 0, n - 1); int i, s1 = s2 = 0, t = n / 2; for(i = 0; i < n; i ++ ) { if(i < t) s1 += A[i]; else s2 += A[i]; } return s2 - s1; } //18年 int fun(int A[], int n){ int i, *B; B = (int *)malloc(sizeof(int)*(n+1)); //申请辅助空间; memset(B,0,sizeof(int)*(n+1)); //初始化数组 for(i = 0; i < n; i ++ ){ if(A[i] > 0 && A[i] <= n) B[A[i]] = 1; } for(i = 1; i <= n; i ++ ) if(B[i] == 0) break; return i; } //20年---三元组 #define INT_MAX 0x7fffffff int abs(int a){ if(a < 0)return -a; else return a; } int fun(int A[], int a, int B[], int b, int C[], int c) { int i = j = k = 0, D, Dmin = INT_MAX; while(i < a && j < b && k < c && Dmin > 0) { D = abs(A[i] - B[j]) + abs(A[i] - C[k]) + abs(B[j] - C[k]); if(D < Dmin)Dmin = D; if(A[i] <= B[j] && A[i] <= C[k]) i ++; else if(B[j] <= A[i] && B[j] <= C[k]) j ++; else k++; } return Dmin; } //25年 void fun(int A[], int res[],int n){ int i, max, min; max = min = A[n - 1]; for(i = n - 1; i >= 0; i -- ) { if(A[i] > max) max = A[i]; else if(A[i] < min) min = A[i]; if(A[i] > 0) res[i] = A[i] * max; else res[i] = A[i] * min; } } //链表题 //09---倒数k个 typedef struct Node{ int data; struct Node *link; }Node; int fun(Node *head, int k){ Node *temp = head -> link; int length = 0, i; while(temp != Null) { //求表长。 length ++; temp = temp -> link; } if(length < k) return 0; Node *ans = head -> link; for(i = 0; i <= length - k; i ++){ ans = ans -> link; } printf("%d", ans -> data); return 1; } //12年---找公共后缀 typedef struct Node{ int data; struct Node *next; }Node; int len(LNode *head) { int length = 0; while (head -> next != NULL) { length ++ ; head = head -> next; } return length; } Node* fun(Node *str1, Node *str2){ //返回类型为节点。 int m,n; Node *p, *q; m = len(str1); n = len(str2); for(p = str1; m > n; m--) p = p -> next; for(q = str2; m < n; n--) q = q -> next; while(p -> next != NULL && q -> != NULL) { p = p -> next; q = q -> next; } return p -> next; } //15年---删除绝对值相同的点 typedef struct Node{ int data; struct Node *link; }Node; void fun(Node *head, int n){ int *q, m; q = (int *)malloc(sizeof(int)*(n+1)); memset(q,0,sizeof(int)*(n+1)); Node *p = head, *r; while(p -> link != NULL){ int temp = abs(p -> link -> data); if(q[temp] == 0) { q[temp] = 1; p = p -> link; } else { r = p -> link; p -> link = r -> link; free(r); } } free(q); } //19年---链表逆置 + 快慢指针 typedef struct Node{ int data; struct Node *next; }Node; void fun(Node *head){ Node *l, *r, *temp; l = r = h; while(r -> next != NULL) { l = l -> next; r = r -> next; if(r -> next != NULL) r = r -> next; } r = l -> next; //r为后半段的首节点。 l -> next = NULL; while(r -> next != NULL){ //链表后半段逆置 temp = r -> next; r -> next = l -> next; l -> next = r; r = temp; }//最后r指针指向尾巴节点 Node *first = head -> next; while(r != NULL){ Node *temp1 = first -> next; Node *temp2 = r -> next; first -> next = r; r -> next = temp1; first -> next = temp1; r -> next = temp2; } } //树 //14年---求WPL typedef struct node{ int weight; struct *left, *right; }Tree; int func(Tree *tree, int h){ if(tree -> left == NULL && tree -> right == NULL) return (tree -> weight * h); else return (func(tree -> left, h + 1) + func(tree -> right, h + 1)); } int wpl(Tree *tree){ return func(tree, 0); } //17年中缀表达式 typedef struct node{ char data[10]; struct node *left, *right; }Tree; void func(Tree *root, int h){ if(root == NULL) return; if(root -> left == NULL && root -> right == NULL){ printf("%s", root -> data); return; } if(h > 1) printf("("); func(root -> left, h + 1); printf("%s", root -> data); func(root -> right, h + 1); if(h > 1) printf(")"); } //22年---二叉搜索树的判定---太难了。 //图 //21年---EL路径 int func(MGraph G){ int i, j, degree, count; for(i = 0; i < G.numVertices; i ++ ){ degree = 0; for(j = 0; j < G.numVertices; j ++ ){ if(G.Edge[i][j] == 1) degree ++; } if(degree % 2 == 1) count ++; } if(count == 0 || count == 2) return 1; else return 0; } //23年 int func(MGraph G){ int i, j, in, out, count; for(i = 0; i < G.numVertices; i ++){ in = out = 0; for(j = 0; j < G.numVertices; j ++){ if(G.Edge[i][j] == 1) out ++; if(G.Edge[j][i] == 1) in ++; } if(out > in) { printf("%s", G.VerticesLit[i]); count ++; } } return count; } //24年---拓扑排序---太难了。 int func(MGraph G){ int *degree, i, j, n = G.numVertices; degree = (int*)malloc(sizeof(int)*n); for(i = 0; i < n; i ++) for(j = 0; j < n; j ++) degree[i] += G.Edge[i][j]; }
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/7/4 16:59:20

腾讯混元开源四款轻量级模型:端侧AI落地的全新突破

腾讯混元开源四款轻量级模型&#xff1a;端侧AI落地的全新突破 【免费下载链接】Hunyuan-0.5B-Instruct-AWQ-Int4 腾讯开源混元0.5B指令微调模型&#xff0c;专为高效部署设计&#xff0c;支持4位整数量化&#xff0c;显著降低计算资源需求。模型具备双思维推理模式&#xff0c…

作者头像 李华
网站建设 2026/7/8 16:07:09

22、Linux系统进程管理与文本文件编辑全解析

Linux系统进程管理与文本文件编辑全解析 1. 识别运行进程 在Linux系统中,理解和管理运行中的进程是系统管理的重要部分。负载平均值是衡量系统负载的一个关键指标,例如,在一个四核CPU的系统上,负载平均值为4.0意味着进程对CPU时间的需求恰好等于计算机的可用CPU时间。 1…

作者头像 李华
网站建设 2026/7/8 1:15:49

深度剖析GLM-Edge-V-2B:20亿参数如何引爆边缘智能革命

在人工智能技术迅猛迭代的浪潮中&#xff0c;大语言模型&#xff08;LLM&#xff09;正以前所未有的速度渗透到各行各业。然而&#xff0c;这类模型普遍存在的"重量级"特性——动辄数十亿甚至千亿的参数规模、对高端计算资源的依赖&#xff0c;使其在边缘设备这一关键…

作者头像 李华
网站建设 2026/7/9 17:41:12

37、进程间与网络通信技术全解析

进程间与网络通信技术全解析 1. 进程间通信基础 在网络通信中,构建服务器套接字地址结构是关键的一步。以下是相关代码示例: receiver.sin_port=htons(atoi(argv[2])); /* (3) */ struct hostent *hp = gethostbyname(argv[1]); if ( hp == NULL ) {sprintf(buf, "%s…

作者头像 李华
网站建设 2026/7/9 22:28:24

JAVA微服务与分布式(概念版)

分布式系统 简单理解 分布式系统 多台机器一起干活&#xff0c;对外看起来像一台 想象一下&#xff1a;你开了一家奶茶店。最开始你一个人搞定所有事——接单、做奶茶、收钱。但生意太好了&#xff0c;一个人忙不过来&#xff0c;于是你雇了3个员工&#xff1a;一个专门接单…

作者头像 李华