行业知识
王道C语言督学营oj练习(全部更新完成)
2026-12-02 22:20:00
来源:互联网转载
(���人是2022年3月跟着王道C语言训练营学习过的,代码只供学习参考,但都是ac成功了的,如果存在ac不了的情况就检查一下自己的代码,注意运行语言有时候是C有时候是C++)
目录
初级阶段
week1-day1作业
week1-day2作业
week1-day3作业
week2-day4作业1
week2-day5作业
week2-day6作业
week3-day7作业1
week3-day7作业2
week3-day8作业
week3-day9作业
week4-day10作业
week4-day11作业
week4-day12作业
中级阶段
中级-day1
中级-day2作业
中级day3作业
中级-day4作业
中级-day5作业
中级-day6作业
中级-day7作业
中级-day8作业
中级-day9作业
中级-day10作业
中级day11-作业
中级day12-作业
高级阶段
高级day1-作业
高级day2-作业
高级day3-作业
高级day4-作业
高级day5-作业
高级day6-作业
初级阶段
week1-day1作业
#include int main(){ printf("hello wangdao\n"); return 0;} week1-day2作业
#define _CRT_SECURE_NO_WARNINGS //解决scanf编译报错问题(考试不需要写)#includeint main(){ int a,b; scanf("%d%d", &a,&b);//一定要在变量前加取地址符& printf("%d\n", a+b);} week1-day3作业
#define _CRT_SECURE_NO_WARNINGS#includeint main(){ int a; scanf("%d", &a); if (65 week2-day4作业1
#define _CRT_SECURE_NO_WARNINGS#includeint main(){ int year; scanf("%d", &year); if (year % 400 == 0 || year % 4 == 0 && year % 100 != 0)//能被4整除但不能被100整除或者能被400整除 { printf("yes\n"); } else { printf("no\n"); }} week2-day5作业
#define _CRT_SECURE_NO_WARNINGS#include//一个scanf读多种类型的数据//混合输入时每次在%c之前需要加入一个空格int main(){ int i; char j; float k; scanf("%d %c%f", &i, &j, &k); //printf("%.2f",(float)i+(float)j+k); printf("%0.2f\n", i + j + k); return 0;} week2-day6作业
#define _CRT_SECURE_NO_WARNINGS#include//把最初输入的整型数a,反过来后,再存到另外一个整形数b,判断a和b是否相等,如果相等就输出yes,不等就输出noint main() { int a,b,c,tmp; while (scanf("%d", &a) != EOF) { b = 0; c = a; while (a) { tmp = a % 10; b = b * 10 + tmp; a = a / 10; } if (c == b) { printf("yes\n"); } else { printf("no\n"); } } return 0;} week3-day7作业1
//while循环 #define _CRT_SECURE_NO_WARNINGS#includeint main(){ int n; int i = 1; int total = 1;//存储最终的和 scanf("%d", &n); while (i data = x;//把读取到的值,给新空间中的data成员 s->next = L->next;//让新结点的next指针指向链表的第一个元素(第一个放我们数据的元素) L->next = s;//让s作为第一的元素 scanf("%d", &x);//读取标准输入 } return L;}//尾插法新建链表LinkList CreatList2(LinkList &L)//list_tail_insert{ int x; L = (LinkList)malloc(sizeof(LNode));//带头结点的链表 LNode* s, * r = L;//LinkList s,r=L;也可以,r代表链表表尾结点,指向链表尾部 //3 4 5 6 9999 scanf("%d\n", &x); while (x!=9999) { s = (LNode*)malloc(sizeof(LNode)); s->data = x; r->next = s;//让尾部节点指向新节点 r = s;//r指向新的表尾结点 scanf("%d\n", &x); } r->next = NULL;//尾结点的next指针赋值为null return L; }//打印链表中每个结点的值void PrintList(LinkList L){ L = L->next; while (L != NULL)//NULL是为了代表一张空的藏宝图 { printf("%3d", L->data);//打印当前节点数据 L = L->next;//指向下一个节点 if (L != NULL) { printf(" "); } } printf("\n");}int main(){ LinkList L; LinkList search; CreatList1(L);//输入数据可以为3 4 5 6 7 9999,头插法新建链表 PrintList(L);//链表打印 CreatList2(L);//输入数据可以为3 4 5 6 7 9999,尾插法新建链表 PrintList(L); return 0;} 中级-day5作业
#define _CRT_SECURE_NO_WARNINGS#include#includetypedef int ElemType;typedef struct LNode { ElemType data; struct LNode* next;//指向下一个节点}LNode,*LinkList;//尾插法建立链表LinkList CreatList(LinkList& L)//list_tail_insert{ int x; L = (LinkList)malloc(sizeof(LNode));//带头结点的链表 LNode* s, * r = L;//LinkList s,r=L;也可以,r代表链表表尾结点,指向链表尾部 //3 4 5 6 7 9999 scanf("%d", &x); while (x != 9999) { s = (LNode*)malloc(sizeof(LNode)); s->data = x; r->next = s;//让尾部节点指向新节点 r = s;//r指向新的尾部节点 scanf("%d", &x); } r->next = NULL; return L;}//按序号查找结点值LNode* GetElem(LinkList L, int i){ int j = 1; LNode* p = L->next; if (i == 0) return L; if (i data = e; s->next = p->next; p->next = s; return true;}//删除第i个节点bool ListDelete(LinkList L, int i){ LinkList p = GetElem(L, i - 1); if (NULL == p) { return false; } LinkList q; q = p->next; p->next = q->next;//断链 free(q);//释放对应节点的空间 return true;}//打印链表中每个节点的位置void PrintList(LinkList L){ L = L->next; while (L!=NULL) { printf("%3d", L->data); L = L->next;//指向下一个节点 } printf("\n");}int main(){ LinkList L; LinkList search; CreatList(L); //PrintList(L); search = GetElem(L, 2);//查找第二个位置的元素的值 if (search != NULL) { printf("%d\n", search->data); } LinstFrontInsert(L, 2, 99);//新结点插入第i个位置 PrintList(L); ListDelete(L, 4);//删除第4个节点 PrintList(L);} 中级-day6作业
#define _CRT_SECURE_NO_WARNINGS#include#include#define MaxSize 50typedef int ElemType;typedef struct { ElemType data[MaxSize];//数组 int top;}SqStack;//栈初始化void InitStack(SqStack& S){ S.top = -1;//代表栈为空}//入栈bool Push(SqStack& S, ElemType x){ if (S.top == MaxSize - 1)//数组的大小不能改变,避免访问越界 { return false; } S.data[++S.top] = x; return true;}//出栈bool Pop(SqStack& S, ElemType& x){ if (-1 == S.top) return false; x = S.data[S.top--]; printf(" %d", x); return true;}//队列typedef struct { ElemType data[5];//数组,存放MaxSize-1个元素 int front, rear;//队列头,队列尾}SqQueue;//队列初始化void InitQueue(SqQueue& Q)//初始化队列{ Q.rear = Q.front = 0;}//入队bool EnQueue(SqQueue& Q, ElemType x){ if ((Q.rear + 1) % MaxSize == Q.front)//判断是否队满 return false; Q.data[Q.rear] = x;//3 4 5 6 7 Q.rear = (Q.rear + 1) % MaxSize; return true;}//出队bool DeQueue(SqQueue& Q, ElemType& x){ if (Q.rear == Q.front) return false; x = Q.data[Q.front];//先进先出 Q.front = (Q.front + 1) % MaxSize; printf(" %d", x); return true;}int main(){ SqStack S;//后进先出LIFO ElemType m;//用来存放拿出的元素 InitStack(S);//初始化 ElemType a, b, c; scanf("%d %d %d\n", &a, &b, &c); Push(S, a); Push(S, b); Push(S, c); SqQueue Q; ElemType element;//存储出队元素 InitQueue(Q);//初始化队列 //入队 int n = 1; int n1; while (n 4)printf("false\n"); for(int i=1;inext = NULL;}bool IsEmpty(LinkQueue Q){ if (Q.front == Q.rear) return true; else return false;}void EnQueue(LinkQueue& Q, ElemType x){ LinkNode* s = (LinkNode*)malloc(sizeof(LinkNode)); s->data = x; s->next = NULL; Q.rear->next = s; Q.rear = s;}bool DeQueue(LinkQueue& Q, ElemType& x){ if (Q.front == Q.rear) return false; LinkNode* p = Q.front->next;//头结点什么都没存,所以头结点的下一个节点才有数据 x = p->data; Q.front->next = p->next; if (Q.rear == p) Q.rear = Q.front; free(p); return true;}//前序遍历void preOrder(BiTree p){ if (p != NULL) { putchar(p->c);//等价于visit函数 preOrder(p->lchild); preOrder(p->rchild); }}int main(){ BiTree pnew; int i, j, pos; char c; BiTree tree = NULL;//树根 ptag_t phead = NULL, ptail = NULL, listpnew = NULL, pcur = NULL;//phead是队列头,ptail是队列尾 while (scanf("%c", &c) != EOF) { if (c == '\n') { break; } pnew = (BiTree)calloc(1, sizeof(BiTNode));//calloc申请空间并对空间进行初始化,赋值为0 pnew->c = c;//数据放进去 listpnew = (ptag_t)calloc(1, sizeof(tag_t));//给队列结点申请空间 listpnew->p = pnew; if (NULL == tree) { tree = pnew;//树的根 phead = listpnew;//队列头 ptail = listpnew;//队列尾 pcur = listpnew; continue; } else { ptail->pnext = listpnew;//新节点放入链表,通过尾插法 ptail = listpnew;//ptail指向队列尾部 }//pcur始终指向要插入的位置 if (NULL == pcur->p->lchild)//如何把新节点放入树 { pcur->p->lchild = pnew;//把新节点放到要插入结点的左边 } else if (NULL == pcur->p->rchild) { pcur->p->rchild = pnew;//把新节点放到要插入结点的右边 pcur = pcur->pnext;//左右都放了结点后,pcur指向队列的下一个 } } preOrder(tree);} 中级-day8作业
#define _CRT_SECURE_NO_WARNINGS#include#includetypedef char BiElemType;typedef struct BiTNode { BiElemType c;//c就是书籍上的data struct BiTNode* lchild; struct BiTNode* rchild;}BiTNode, * BiTree;typedef struct tag { BiTree p;//树的某一个结点的地址值 struct tag* pnext;}tag_t, * ptag_t;#define MaxSize 50typedef BiTree ElemType;//队列的相关数据结构typedef struct LinkNode { ElemType data; struct LinkNode* next;}LinkNode;typedef struct { LinkNode* front, * rear;}LinkQueue;void InitQueue(LinkQueue& Q);bool IsEmpty(LinkQueue Q);void EnQueue(LinkQueue& Q, ElemType x);bool DeQueue(LinkQueue& Q, ElemType& x);//带头结点的队列void InitQueue(LinkQueue& Q){ Q.front = Q.rear = (LinkNode*)malloc(sizeof(LinkNode)); Q.front->next = NULL;}bool IsEmpty(LinkQueue Q){ if (Q.front == Q.rear) return true; else return false;}void EnQueue(LinkQueue& Q, ElemType x){ LinkNode* s = (LinkNode*)malloc(sizeof(LinkNode)); s->data = x; s->next = NULL; Q.rear->next = s; Q.rear = s;}bool DeQueue(LinkQueue& Q, ElemType& x){ if (Q.front == Q.rear) return false; LinkNode* p = Q.front->next;//头结点什么都没存,所以头结点的下一个节点才有数据 x = p->data; Q.front->next = p->next; if (Q.rear == p) Q.rear = Q.front; free(p); return true;}//中序遍历 hdibjeafcgvoid InOrder(BiTree p){ if (p != NULL) { InOrder(p->lchild); putchar(p->c); InOrder(p->rchild); }}//hidjebfgca 后序遍历void PostOrder(BiTree p){ if (p != NULL) { PostOrder(p->lchild); PostOrder(p->rchild); putchar(p->c); }}//层次遍历,层序遍历,广度优先遍历void LevelOrder(BiTree T){ LinkQueue Q;//辅助队列 InitQueue(Q);//初始化队列 BiTree p; EnQueue(Q, T);//树根入队 while (!IsEmpty(Q)) { DeQueue(Q, p);//出队当前结点并打印 putchar(p->c); if (p->lchild != NULL) //入队左孩子 EnQueue(Q, p->lchild); if (p->rchild != NULL) //入队右孩子 EnQueue(Q, p->rchild); }}int main(){ BiTree pnew; int i, j, pos; char c; BiTree tree = NULL;//树根 ptag_t phead = NULL, ptail = NULL, listpnew = NULL, pcur = NULL;//phead就是队列头,ptail就是队列尾 //abcdefghij while (scanf("%c", &c) != EOF) { if (c == '\n') { break; } pnew = (BiTree)calloc(1, sizeof(BiTNode));//calloc申请空间并对空间进行初始化,赋值为0 pnew->c = c;//数据放进去 listpnew = (ptag_t)calloc(1, sizeof(tag_t));//给队列结点申请空间 listpnew->p = pnew; if (NULL == tree) { tree = pnew;//树的根 phead = listpnew;//队列头 ptail = listpnew;//队列尾 pcur = listpnew; continue; } else { ptail->pnext = listpnew;//新结点放入链表,通过尾插法 ptail = listpnew;//ptail指向队列尾部 }//pcur始终指向要插入的结点的位置 if (NULL == pcur->p->lchild)//如何把新结点放入树 { pcur->p->lchild = pnew;//把新结点放到要插入结点的左边 } else if (NULL == pcur->p->rchild) { pcur->p->rchild = pnew;//把新结点放到要插入结点的右边 pcur = pcur->pnext;//左右都放了结点后,pcur指向队列的下一个 } } InOrder(tree); printf("\n"); PostOrder(tree); printf("\n"); LevelOrder(tree); printf("\n");} 中级-day9作业
#define _CRT_SECURE_NO_WARNINGS#include#includetypedef int KeyType;typedef struct BSTNode { KeyType key; struct BSTNode* lchild, * rchild;}BSTNode, * BiTree;//54 20 66 40 28 79 58int BST_Insert(BiTree& T, KeyType k){ if (NULL == T) { //为新节点申请空间 T = (BiTree)malloc(sizeof(BSTNode)); T->key = k; T->lchild = T->rchild = NULL; return 1;//代表插入成功 } else if (k == T->key) return 0;//发现相同元素,就不插入 else if (k key) return BST_Insert(T->lchild, k); else return BST_Insert(T->rchild, k);}//创建二叉排序树void Create_BST(BiTree& T, KeyType str[], int n){ T = NULL; int i = 0; while (i lchild, str, pos); printf("%3d", T->key); str[pos++] = T->key;//输出的同时存入数组 InOrder(T->rchild, str, pos); }}typedef int ElemType;typedef struct { ElemType* elem;//整形指针 int TableLen;}SSTable;int Binary_Search(SSTable L, ElemType key){ int low = 0, high = L.TableLen - 1, mid; while (low key) high = mid - 1; else low = mid + 1; } return -1;}int main(){ BiTree T = NULL; BiTree parent;//存储父亲节点的地址值 BiTree search; KeyType str[10];//将要进入二叉排序树的元素值 int i; for (i = 0; i 中级-day10作业
#define _CRT_SECURE_NO_WARNINGS#include#includeint main(){ char j; scanf("%c",&j); int i; int arr[9] = { 0,1,1,2,2,3,1,1,2 }; for (i = 0; i 中级day11-作业
#define _CRT_SECURE_NO_WARNINGS#include#include#include #includetypedef int ElemType;typedef struct { ElemType* elem;//存储元素的起始地址 int TableLen;}SSTable;void ST_Init(SSTable& ST, ElemType A[],int len){ ST.TableLen = len; ST.elem = (ElemType*)malloc(sizeof(ElemType) * ST.TableLen); int i; for (i = 0; i i; j--)//内层控制比较,交换 { if (A[j - 1] > A[j]) { swap(A[j - 1], A[j]); flag = 1; } } }}int Partition(int* arr, int left, int right){ int k, i; for (k = i = left; i 中级day12-作业
#define _CRT_SECURE_NO_WARNINGS#include #include #include #include typedef int ElemType;typedef struct {ElemType* elem;int TableLen;}SSTable;void ST_Init(SSTable& ST, ElemType A[], int len){ST.TableLen = len;ST.elem = (ElemType*)malloc(sizeof(ElemType) * ST.TableLen);int i;for (i = 0; i 0; i--){AdjustDown1(A, 0, i);//剩下元素调整为大根堆swap(A[0], A[i]);}}//选择排序与堆排序int main(){SSTable ST;ElemType A[10]; int i;for (i = 0; i 高级阶段
高级day1-作业
#define _CRT_SECURE_NO_WARNINGS#include #include #define N 10typedef int ElemType;void Merge(ElemType A[], int low, int mid, int high){ElemType B[N];//为了降低操作次数int i, j, k;for (k = low; k next_edge;//某个顶点的下一条边,例如B结点的下一条边 }}/* * 深度优先搜索遍历图 */void DFSTraverse(LGraph G){ int i; int visited[MAX]; // 顶点访问标记 // 初始化所有顶点都没有被访问 for (i = 0; i ivex; if (!visited[k]) { visited[k] = 1; printf(" %c", G.vexs[k].data); queue[rear++] = k;//类似于树的层次遍历,遍历到的同时入队 } node = node->next_edge; } } } printf("\n");}/* * 创建邻接表对应的图(有向图) */LGraph* create_example_lgraph_directed(){ char c1, c2; char vexs[] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G' }; char edges[][2] = { {'A', 'B'}, {'B', 'C'}, {'B', 'E'}, {'B', 'F'}, {'C', 'E'}, {'D', 'C'}, {'E', 'B'}, {'E', 'D'}, {'F', 'G'} }; int vlen = LENGTH(vexs); int elen = LENGTH(edges); int i, p1, p2; ENode* node1; LGraph* pG; if ((pG = (LGraph*)malloc(sizeof(LGraph))) == NULL) return NULL; memset(pG, 0, sizeof(LGraph)); // 初始化"顶点数"和"边数" pG->vexnum = vlen; pG->edgnum = elen; // 初始化"邻接表"的顶点 for (i = 0; i vexnum; i++) { pG->vexs[i].data = vexs[i]; pG->vexs[i].first_edge = NULL; } // 初始化"邻接表"的边 for (i = 0; i edgnum; i++) { // 读取边的起始顶点和结束顶点 c1 = edges[i][0]; c2 = edges[i][1]; p1 = get_position(*pG, c1); p2 = get_position(*pG, c2); // 初始化node1 node1 = (ENode*)calloc(1, sizeof(ENode)); node1->ivex = p2; // 将node1链接到"p1所在链表的末尾" if (pG->vexs[p1].first_edge == NULL) pG->vexs[p1].first_edge = node1; else link_last(pG->vexs[p1].first_edge, node1); } return pG;}int main(){ LGraph* pG; pG = create_example_lgraph_directed(); DFSTraverse(*pG);//深度优先遍历 BFS(*pG);//广度优先遍历 return 0;} 高级day3-作业
#includeint main(){printf("%3d%3d\n", 0, 13);return 0;} 高级day4-作业
#define _CRT_SECURE_NO_WARNINGS#includeint main(){int i;scanf("%d", &i);printf("%2d\n", i > 1);return 0;} 高级day5-作业
#define _CRT_SECURE_NO_WARNINGS#includeint main(){int i;int arr[5];for (i = 0; i 高级day6-作业
#includeint main(){printf("mov\npush\npop\n");return 0;}
下一篇:微软服务器烂了怎么修?