
c语言练手
Python ml
这个作者很懒,什么都没留下…
展开
-
acwing 二分模板
如果mid不符合条件,我们要找符合性质的左端点则l = mid + 1(已经确定mid是不符合的就从mid+1开始算)而mid = (r - 1 + r)/2 = r - 0.5 = r - 1 = l(向下取整),则更新完仍然是l = r- 1没变化会死循环。对mid进行判断来获得结果:如果mid符合条件,则mid就在左半区,那么我们要找的右边界点就可以更新为(mid,r)l = mid。如果mid不满足条件,则mid必然在右半区间,而我们要找的符合要求的左半区间性质的右端点就是(l,mid-1)。原创 2024-03-11 11:38:13 · 445 阅读 · 0 评论 -
贪吃蛇(C语言小游戏)
c语言完成小游戏原创 2022-11-14 17:13:35 · 726 阅读 · 0 评论 -
数组顺序间隔分组
把数组中的位序为奇数的放到前面class Solution {public: vector<int> exchange(vector<int>& nums) { int mid=(nums.size()-1)/2,left,right; //mid数组中间位置的下标 if(nums.size()==1) return nums; left=1; //left数组第一个偶数的下标 right=原创 2022-05-13 10:34:15 · 202 阅读 · 0 评论 -
B: LOGO
B: LOGO#include <cmath>#include <iostream>using namespace std;void painting(int order,int len){ if(order==0) { if(len!=0) printf("FD 1/%d\n",(int)pow(3,len)); else printf("FD 1\n"); return; } painting(or原创 2022-03-10 20:35:20 · 163 阅读 · 0 评论 -
大数加减法
#include <iostream>using namespace std;int main(){ string s1,s2,s; cin>>s; for(int i=0;i<s.length();++i){ if(!isdigit(s[i])) { s1=s.substr(0,i); s2=s.substr(i+1); } } int len1=s1原创 2022-03-10 20:02:09 · 171 阅读 · 0 评论 -
Square DFS
#include <iostream>#include <vector>using namespace std;const int MAXN=25;int side; //边长int m;int sticks[MAXN];bool visit[MAXN];bool DFS(int sum,int number,int position){ //sum当前拼凑的木棍长度,number已拼凑的木棍数量,position当前木棍编号原创 2022-03-08 14:53:46 · 139 阅读 · 0 评论 -
谁是你的潜在朋友
北大复试上机题#include <iostream>#include <vector>using namespace std;int main() { int n,m; cin>>n>>m; vector<int> stu(n); vector<int> book(m+1); for(int i=0;i<n;++i){ scanf("%d",&stu[i])原创 2022-03-08 10:32:14 · 163 阅读 · 0 评论 -
POJ 3278 抓住那头牛 (宽度优先搜索、队列实现)
POJ 3278 抓住那头牛#include <iostream>#include <algorithm>#include <queue>using namespace std;const int MAXN=1e5+10;bool visit[MAXN];struct Status{ int position; int time; Status(int p,int t):position(p),time(t){}};int B原创 2022-03-01 20:01:29 · 683 阅读 · 0 评论 -
最小生成树——Kruskal算法
#include <iostream>#include <algorithm>using namespace std;const int MAXN=100; //顶点数int father[MAXN];int height[MAXN];struct Edge{ //边的结构体 int from; int to; int length; bool operator < (const Edge &e原创 2022-02-28 16:06:07 · 219 阅读 · 0 评论 -
HDU 4508 湫湫系列故事——减肥记(完全背包)
拿了第i号物品后依然可以再拿第i号物品,考虑第i个物品的状态转移方程改一点#include <iostream>using namespace std;const int MAXN=100+10;const int MAXM=1e5+10;int dp[MAXN][MAXM];int weight[MAXN];int value[MAXN];int main() { int n,m; while (scanf("%d",&n)!=EOF){ .原创 2022-02-27 20:02:05 · 223 阅读 · 0 评论 -
01背包(动态规划)详解
最优子结构性质:大问题的最优解可以由小问题的最优解推出无后效性:一旦f(n)确定,如何凑出f(n)就再也用不着了如何判断一个问题能否使用DP解决:能将大问题拆成小问题,且满足无后效性、最优子结构性质DP为什么这么快:暴力解法是枚举所有的可能解,这是最大的可能解空间DP枚举有希望成为答案的解,这个空间比暴力小得多,即DP自带剪枝其核心思想就是尽量缩小解空间DP操作过程:(有记忆的递归)将一个大问题转化成几个小问题求解小问题推出大问题的解dp[i][j]表示考虑前i个物品在容重为j的情况原创 2022-02-27 17:47:23 · 420 阅读 · 0 评论 -
最长公共子序列
#include <iostream>using namespace std;const int MAXN=1000+10;string s1,s2;int fun1(int n,int m){ //直接递归,时间复杂度O(2^(n+m)) int answer; if(n==0||m==0) answer=0; else{ if(s1[n]=s2[m]) answer=fun1(n-1,m-1)+1; else ans..原创 2022-02-27 10:58:21 · 157 阅读 · 0 评论 -
最短带权路径长度(哈夫曼树的优先队列实现)
#include <iostream>#include <queue>using namespace std;int main() { int n; while (scanf("%d",&n)!=EOF){ priority_queue<int,vector<int>,greater<int>> MypriorityQueue; //每次取出最小的两个值,小顶堆,优先级低的先输出原创 2022-02-26 20:26:29 · 417 阅读 · 0 评论 -
复数集合(优先队列、二叉堆)
#include <iostream>#include <queue>using namespace std;struct Complex{ int real,imag; Complex(int a,int b):real(a),imag(b){} //构造函数 bool operator <(Complex c) const{ //重载小于号 if(real*real+imag*imag==c.real*c.rea原创 2022-02-26 11:54:35 · 121 阅读 · 0 评论 -
第k个素数
#include <iostream>#include <vector>using namespace std;vector<int>prime; //存放找到的所有的质数const int MAXN=1e5+10;bool Isprime[MAXN];void Inital(){ fill(Isprime,Isprime+MAXN,true); Isprime[0]=Isprime[1]=false; //0和1不是素数,原创 2022-02-25 17:13:22 · 739 阅读 · 0 评论 -
最大公因数和最小公倍数
#include <iostream>using namespace std;int GCD(int a,int b){ if(b==0) return a; else return GCD(b,a%b);}int main() { int a,b; while(scanf("%d%d",&a,&b)!=EOF){ printf("最大公因数:%d\n",GCD(a,b)); printf("最小公倍数:%原创 2022-02-25 16:12:02 · 398 阅读 · 0 评论 -
最大公约数gcd 和最小公倍数lcm
#include <iostream>using namespace std;int GCD(int a,int b){ if(b==0) return a; else return GCD(b,a%b);}int main() { int a,b; while(scanf("%d%d",&a,&b)!=EOF){ printf("最大公因数:%d\n",GCD(a,b)); printf("最小公倍数:.原创 2022-02-25 16:02:33 · 227 阅读 · 0 评论 -
M进制数转换为N进制数(清华大学复试上机题)
#include <iostream>#include <vector>using namespace std;char In2Char(int target){ //int转换为char if(target<=9&&target>=0) return target+'0'; else return target-10+'A';}int Char2In(char c){原创 2022-02-25 12:27:57 · 216 阅读 · 0 评论 -
简单计算器(表达式求值)
input:1+24+2*5-7/110output:3.0013.36#include <iostream>#include <cctype>#include <stack>using namespace std;int Priority(char c){ if(c=='#') return 0; else if(c=='$') return 1; else if(c=='+'||c=='-')return 2; e原创 2022-02-24 21:15:15 · 376 阅读 · 0 评论 -
括号匹配问题
#include <iostream>#include <stack>using namespace std;int main() { string s; while(cin>>s){ string ans(s.size(),' '); stack<int> brackets; for(int i=0;i<s.size();i++){ if(s[i]=='('原创 2022-02-24 20:22:54 · 261 阅读 · 0 评论 -
约瑟夫问题(队列实现)
n个小孩按1-n次序围成一圈,从编号为p的小孩开始报数,由1报到m,报到m时这名小孩从圈中出去,然后下一名小孩再从1开始报,报到m时再出去。直到所有小孩出去,输出先后出去的小孩的编号#include <iostream>#include <queue>using namespace std;int main() { int n,p,m; while (scanf("%d%d%d",&n,&p,&m)){ //n个小孩从编号p原创 2022-02-24 19:35:50 · 478 阅读 · 0 评论 -
打印日期(华中科技大学上机题)
#include <iostream>using namespace std;int daytab[2][13]={ {0,31,28,31,30,31,30,31,31,30,31,30,31}, {0,31,29,31,30,31,30,31,31,30,31,30,31}, //daytab[1] 闰年};bool IsLeapYear(int year){ return ((year%4==0&&year%100!=0)||(year%原创 2022-02-24 11:29:31 · 208 阅读 · 0 评论 -
Stack
//栈的相关数据结构#define MaxSize 50typedef BiTree ElemType;typedef struct{ ElemType data[MaxSize]; int top;}SqStack;void InitStack(SqStack &S){ S.top=-1;}bool StackEmpty(SqStack &S){ if(S.top==-1) return true; else return false;}//入栈原创 2021-05-11 17:09:45 · 76 阅读 · 0 评论 -
数据结构定义
#include <stdio.h> #include <stdlib.h>typedef int ElemType;#define Maxsize 50#define Initsize 100//线性表的顺序存储--顺序表(静态)typedef struct { ElemType data[Maxsize]; int length;}SqList;//动态顺序表typedef struct { Elem原创 2021-05-06 12:45:10 · 125 阅读 · 0 评论 -
快速排序
#include <stdio.h> #include <stdlib.h>void QuickSort(int a[], int begin, int end) { if (begin < end) { int i = begin, j = end, temp = a[end]; while (i < j) { while (i < j && a[i] <= temp) { i++; } //从左往右找第一个大于temp原创 2021-04-27 23:45:02 · 61 阅读 · 0 评论 -
归并排序
#include<vector>#include<iostream>using namespace std;void Merge(int a[], int front, int mid, int end) { /*int* aux = new int[end - front + 1];*/ vector<int>aux( end - front + 1 ); int start1 = front, end1 = mid; int start2 = mid原创 2021-04-23 16:40:01 · 70 阅读 · 0 评论 -
408 数据结构 2010 算法题
#include <stdio.h>#include <stdlib.h>void converse(int a[],int len, int& n) { int* p = new int[n](); for (int i = 0; i < n; i++) { p[i] = a[i]; } for (int i = 0; i < len - n; i++) { a[i] = a[n + i]; } for (int i = 0; i <原创 2021-04-18 16:07:04 · 516 阅读 · 0 评论 -
计算数组中各数出现次数
#include <stdio.h>#include <stdlib.h>#include<iostream>//using name space std;void vote(int a[], int n) { int t = 0, i, j, re; int* b = new int[n], * c = new int[n]; //b[j]为计数数组,a[]中共有t个不同数值 for (i = 0; i < n; i++) { //遍历数组 fo原创 2021-02-02 20:13:24 · 614 阅读 · 0 评论 -
c++容器的一般使用
使用的容器有:list , stark, vector#include <stdio.h>#include<stdlib.h>#include <iostream>#include <stack> #include <list>#include <queue>#include <vector>#include <string>#include<algorithm>using nam原创 2021-02-02 14:47:00 · 127 阅读 · 1 评论 -
层次建立二叉树
#include <stdio.h>#include<stdlib.h>#include <string>typedef char ElemType;typedef struct node{ //建立树节点结构体 ElemType c; struct node* pleft; struct node* pright;}Node,*pNode;typedef struct queue //建立辅助队列结构体{ pNode inse原创 2021-01-31 18:37:42 · 636 阅读 · 0 评论 -
循环队列
#include <stdio.h>#include<stdlib.h>#include <string>#define maxsize 5typedef int Elemtype;typedef struct { Elemtype data[maxsize]; //结构体数组 int front, rear; }SqQueue;void InitQueue(SqQueue*queue){ queue->front = queue-原创 2021-01-30 19:33:40 · 74 阅读 · 0 评论 -
栈
stack.h#pragma once#include <stdio.h>#include<stdlib.h>typedef struct tag //定义栈的节点结构体{ int val; struct tag* pnext;}node, * pnode;typedef struct { //定义栈结构体 int size; pnode phead;}Stack, * pStack;void initStack原创 2021-01-30 10:52:07 · 70 阅读 · 0 评论 -
大数加法
#include <stdio.h>#include<stdlib.h>#include <string.h> #include <iostream> using namespace std;int main(){ int a1[1004], b1[1004], c[1004], t1, t2, t, i, k, n; char a2[1004], b2[1004];// 这两个数组用来输入两个数 cin >&原创 2021-01-30 00:15:08 · 117 阅读 · 0 评论 -
删除有序链表的重复元素
若链表的元素无序的话先有序插入新建链表,再删除重复元素#include <stdio.h>#include<stdlib.h>typedef struct student { int num; struct student* pnext;}stu, * pstu;void list_print(pstu phead){ while (phead) { printf("%d ", phead->num);原创 2021-01-29 20:53:19 · 259 阅读 · 0 评论 -
链表相交及求其交点
题目:判断两个链表是否相交, 如果相交, 计算交点 void find_two_list_intersection(pstu head1, pstu head2) { int len1, len2; pstu pcur1=NULL, pcur2=NULL; pstu tail1 = head1, tail2 = head2; while (tail1) { len1++; pcur1 = t原创 2021-01-29 09:53:37 · 220 阅读 · 0 评论 -
逆转链表
#include <stdio.h>#include<stdlib.h>typedef struct student { int num; struct student* pnext;}stu, * pstu;void list_print(pstu phead){ while (phead) { printf("%d %5.1f\n", phead->num); phead = phead->原创 2021-01-28 17:57:10 · 306 阅读 · 0 评论 -
合并两个有序链表
#include <stdio.h>#include<stdlib.h>typedef struct student { int num; float score; struct student* pnext;}stu, * pstu;void list_print(pstu phead){ while (phead) { printf("%d %5.1f\n", phead->num,phead->原创 2021-01-28 15:08:39 · 99 阅读 · 0 评论 -
链表的增删查改
#include <stdio.h>#include<stdlib.h>typedef struct student { int num; float score; struct student* pnext;}stu, * pstu;void list_head_insert(pstu* pphead, stu** pptail, int i) { //头插法 pstu pnew = (pstu)mal原创 2021-01-28 10:46:17 · 133 阅读 · 0 评论 -
链表的插入
头插法、尾插法、有序插入#include <stdio.h>#include<stdlib.h>typedef struct student { int num; float score; struct student* pnext;}stu, * pstu;void list_head_insert(pstu* pphead, stu** pptail, int i) { //头插法 pstu pne原创 2021-01-26 14:52:04 · 123 阅读 · 0 评论 -
结构体定义和相关处理
题目有一个学生结构体,其数据成员有: 学号, 姓名, 3 门课程。从键盘上输入 5 个学生 的信息。要求输出:(1) 按照学号递增输出全部学生信息,每个学生的信息一行。(格式: 学号 姓名 分数 1 分数 2 分数 3 总分)(2) 输出每门课程最高分的学生的信息(3) 输出每门课程的平均分(4) 按照总分输出学生排名#include<stdio.h>#include<stdlib.h>typedef struct student //定义结构体{ int n原创 2021-01-26 12:19:28 · 211 阅读 · 0 评论