![](https://img-blog.csdnimg.cn/950a4bf69f934ac69f9fd07001834966.png?x-oss-process=image/resize,m_fixed,h_224,w_224)
c++数据结构
使用cpp实现的数据结构
吴天德少侠
这个作者很懒,什么都没留下…
展开
-
c++最小生成树实现
中的图结构,并计算它的最小生成树。原创 2024-07-22 16:11:22 · 101 阅读 · 0 评论 -
c++实现二叉树的前序遍历
首先实现一颗这样的树然后使用系统栈(递归)和自己定义的栈分别实现二叉树的前序遍历。原创 2024-06-25 11:03:47 · 145 阅读 · 0 评论 -
c++实现快速排序
【代码】c++实现快速排序。原创 2024-06-24 14:52:41 · 119 阅读 · 0 评论 -
c++实现2个有序数组的合并
【代码】c++实现2个有序数组的合并。原创 2024-03-07 14:56:30 · 800 阅读 · 0 评论 -
c++/c图的邻近矩阵表示
100。原创 2024-03-01 16:38:06 · 465 阅读 · 0 评论 -
c++使用递归实现汉诺塔
参考文献:数据结构 【严蔚敏】这个小游戏,以前玩过,当时玩的很吃力。对着书,然后实现出来的,感觉还挺有意思。原创 2023-12-22 10:51:56 · 87 阅读 · 0 评论 -
c++二叉树遍历
数据结构c语言版,严蔚敏_吴伟民著。原创 2023-10-30 17:04:21 · 103 阅读 · 0 评论 -
c++实现dijskstra算法
我一开始写成了最小生成树的代码,最小生成树一直在选最小的那条边,对每个节点来说,它到原点的距离不一定是最近的。原创 2023-10-27 16:42:53 · 104 阅读 · 0 评论 -
c++广度优先搜索
c++算法原创 2023-02-07 13:49:39 · 427 阅读 · 0 评论 -
c++实现堆排序
算法原创 2023-01-28 16:53:35 · 821 阅读 · 0 评论 -
c++使用bitset求质数
c++原创 2022-12-08 21:55:46 · 216 阅读 · 0 评论 -
c++数据结构之实现链表操作
c++数据结构原创 2022-12-05 14:04:59 · 666 阅读 · 0 评论 -
c++实现数的全排列
#include<iostream>using namespace std;#include<vector>const int maxn=11; // 最多排11位vector<int> ans(maxn), used(maxn,0);void permu(int index,int n,int start){ const int astart=start; if(index==n+start){ for (int .原创 2021-12-21 20:15:28 · 508 阅读 · 0 评论 -
c++牛顿迭代法实现sqrt
基于这个公式,假设要求的数为num,要求的数为res,,其中i的初始值可以任意取。如果,那么就可以认为res为要求的值,否则用res去更新i,继续迭代。#include<iostream>using namespace std;double mysqrt(int num, double i){ double res = (i + num*1.0/i)/(2*1.0); if (abs(res*res-(num*1.0))<1e-5){ retur原创 2021-12-21 19:06:41 · 337 阅读 · 0 评论 -
c++实现KMP算法
原理我相信大家已经了解的差不多了,现在看看实际代码吧cpp:// cpp实现kmp算法#include<iostream>#include<string>using namespace std;#include<vector>vector<int> getnextarr(string patten){ int length = patten.length(); // 初始化next数组 vector<int&原创 2021-12-21 18:17:06 · 262 阅读 · 0 评论 -
c++的快速排序
原理就是分治法,我猜大家应该都已经了解了,主要讲一下具体实现细节:以第1次排序为例,首先以第1位作为标准值std,你可以想象把它挖出来了,这个槽是空的,然后:1. 从右边开始,往左找,找到第1个小于等于std值的数p,把这个数放到空槽里(交换,现在j的位置是空槽)。左指针右移一步。2. 从左边开始,往右找,找到第1个大于std值的数q,把这个值放到空槽里(交换,现在i的位置是空槽)。然后j往左前进一步。之后就是重复1,2这2个过程,直到i和j相遇,...原创 2021-12-02 11:42:57 · 605 阅读 · 0 评论 -
318. 最大单词长度乘积
其实算是暴力计算了,踩着线过了,如果数据范围设大一点应该就过不了cpp/c++实现:#include<vector>#include<string>#include<iostream>#include<map>using namespace std;class Solution {public: int maxProduct(vector<string>& words) { int ans原创 2021-11-17 11:46:56 · 169 阅读 · 0 评论 -
判断message是不是在newspaper中
给定2个字符串,newspaper和message,要求判断message是否可以由newspaper中的内容得到。如果是返回ture,否则返回falsecpp/c++实现#include<string>#include<iostream>#include<unordered_map>using namespace std;bool canCompose(string newspaper, string message){ unordered_原创 2021-11-13 16:13:38 · 533 阅读 · 0 评论 -
520. 检测大写字母
c++/cpp实现:class Solution {public: bool detectCapitalUse(string word) { int length = word.length(); bool first = whetherUpper(word[0]); if (first == false){ // 首字母是小写字母 for (int i=1; i<length; i++)原创 2021-11-13 13:46:11 · 179 阅读 · 0 评论 -
cpp结构排序
cpp实现:#include<iostream>using namespace std;#include<string>#include<vector>#include<iomanip>struct Book{ string name; float price; int classification; }book[101]; int main(){ int n; scanf("%d",&n); ..原创 2021-10-26 14:32:48 · 98 阅读 · 0 评论 -
496. 下一个更大元素 I
这个题目挺简单的,按正常思路做就可以了。cpp实现:#include<iostream>#include<string>using namespace std;#include<vector>#include<algorithm>class Solution {public: vector<int> nextGreaterElement(vector<int>& nums1, vector<原创 2021-10-26 10:17:42 · 49 阅读 · 0 评论 -
240. 搜索二维矩阵 II
思路:从右上角[0,n-1]开始搜索,如果是target则返回true;如果大于target,则往左走(j-1);如果小于target,则往下走(j+1)。越出边界还没有找到,返回false。cpp实现:#include<iostream>using namespace std;#include<vector>class Solution {public: bool searchMatrix(vector<vector<int>>&原创 2021-10-25 08:12:31 · 71 阅读 · 0 评论 -
使用cpp的优先队列
// implementing maxheap and min heap using priority queue#include <iostream>#include "queue"#include "vector"using namespace std;class Mountain{public: string name; int height;public: Mountain(string arg_name, int arg_height):na.原创 2021-09-13 16:53:12 · 294 阅读 · 0 评论 -
cpp+Floyd算法
例如,求一张有向图各个顶点到其他顶点的距离#include<cstdio>#include<algorithm>using namespace std;const int INF = 10000000;const int MAXV = 200; // MAXV为最大顶点数int n, m; // n为顶点数,m为边 int dis[MAXV][MAXV]; // 顶点i和j的最短距离 void Floyd(){ for (int k=0; k<n; k原创 2021-09-01 09:53:00 · 102 阅读 · 0 评论 -
cpp优先队列
#include<iostream>#include<string>#include<queue>using namespace std;struct fruit{ string name; int price; friend bool operator < (fruit f1, fruit f2){ return f1.price > f2.price; // 价格高的优先级低 }}f1,f2,f3;int main(){ .原创 2021-08-09 17:29:50 · 470 阅读 · 0 评论 -
cpp的map.find(key)函数
#include<stdio.h>#include<map>using namespace std;int main() { /* map.find(key)返回键为key的映射的迭代器 */ map<char, int> mp; mp['m'] = 20; mp['r'] = 30; mp['a'] = 40; map<char, int>::iterator it = mp.find('a'); if (it !=mp.end(.原创 2021-07-20 17:25:35 · 1457 阅读 · 0 评论 -
cpp打印map的键和值
#include<stdio.h>#include<map>using namespace std;int main() { map<char, int> mp; mp['m'] = 20; mp['r'] = 30; mp['a'] = 40; for (map<char, int>::iterator it = mp.begin(); it != mp.end(); it++) { printf("%c %d\n", it-&g.原创 2021-07-20 17:16:29 · 4885 阅读 · 0 评论 -
cpp判断2个数用指定小数位的科学计数法表示之后,形式是否相同
#include<iostream>#include<string>using namespace std;int n;string deal(string s, int& e) { int k = 0; // s的下标 while (s.length() > 0 && s[0] == '0') { s.erase(s.begin()); // 去掉s的前导0 } if (s[0] == '.') { // 去掉前导0后是小数点.原创 2021-07-20 17:07:36 · 172 阅读 · 0 评论 -
c/c++贪心算法求区间不相交
题目描述:给出N个开区间(x,y),从中选择尽可能多的开区间,使得这些开区间两两没有交集。例如对于开区间(1, 3), (2, 4), (3, 5), (6, 7) 来说,可以选出最多3个区间 (1, 3), (3, 5), (6, 7) ,它们互相没有交集。输入:第一行输入N,表示有几个开区间接下来输入N行,每行2个数,代表开区间的左右端点。例如:41 32 43 56 7输出:不相交开区间的个数例如:3代码:#include<cstdio&g原创 2021-07-07 14:59:30 · 446 阅读 · 0 评论 -
c/c++贪心算法求0-9组成的最小数字
输入:输入一排数字,表示0-9的个数例如:2 2 0 0 0 3 0 0 1 0表示2个0,2个1,0个2,...输出:组成的最小数字例如:10015558代码:#include<cstdio>int main() { int count[11]; // 记录数字0-9的个数 for (int i = 0; i < 10; i++) { scanf_s("%d", &count[i]); } for (int i = 1; i &l原创 2021-07-07 14:34:10 · 365 阅读 · 0 评论 -
c/c++贪心算法求月饼利润
输入:第1行:输入几种月饼n,需求量D第2行:输入n种月饼各自的存量第3行:输入n种月饼的总售价例如:3 2018 15 1075 72 45输出:最大收益例如:94.50代码:#include<cstdio>#include<algorithm>using namespace std;struct mooncake { double store; double sell; double price;}cake[1010];原创 2021-07-07 14:22:59 · 390 阅读 · 0 评论 -
python和c/c++递归实现全排列
c++实现:#include<cstdio>const int maxn = 11;// p为当前排列,hashTable记录整数x是否已经在p中int n, P[maxn], hashTable[maxn] = { false };// 当前处理排列的第index号位void generateP(int index) { if (index == n + 1) { // 递归边界 for (int i = 1; i <= n; i++) { printf(.原创 2021-06-25 17:29:49 · 83 阅读 · 0 评论 -
c/c++判断m个字符串在长度为n的字符串数组中对应的出现次数
#include<cstdio>const int maxn = 100;char S[maxn][5], temp[5];int hashTable[26 * 26 * 26 + 10];int hashFunc(char S[], int len) { // 将字符串转换为整数 int id = 0; for (int i = 0; i < len; i++) { id = id * 26 + (S[i] - 'A'); } return id;}int .原创 2021-06-25 17:01:56 · 352 阅读 · 0 评论 -
c/c++查询M个数在N数组中出现的次数
#include<cstdio>const int maxn = 100001;int hashTable[maxn] = { 0 };int main() { int n, m, x; scanf_s("%d %d", &n, &m); for (int i = 0; i < n; i++) { scanf_s("%d", &x); hashTable[x]++; } for (int i = 0; i < m; i++) {.原创 2021-06-25 16:31:15 · 488 阅读 · 0 评论 -
c/c++考场分排序
#include<cstdio>#include<cstring>#include<algorithm>using namespace std;struct Student { char id[15]; int score; int location_number; int local_rank;}stu[30010];bool cmp(Student a, Student b) { if (a.score != b.score) return.原创 2021-06-25 16:08:25 · 196 阅读 · 0 评论 -
c/c++实现进制转换
输入 A B D,要求将A+B的和转换为D进制数#include<cstdio>int main(){ int a,b,d; scanf("%d%d%d",&a,&b,&d); int sum = a+b; int ans[31],num=0; // ans 存放D进制的每一位 do{ ans[num++] = sum%d; sum /=d; }while(sum!=0); for(int i=num-1; i>=0; i-原创 2021-06-24 16:53:02 · 89 阅读 · 0 评论 -
c/c++浮点数的比较
#include<stdio.h>#include<math.h>const double eps=1e-8;#define Equ(a,b) ((fabs((a)-(b)))<(eps))int main(){ double db=1.23; if (Equ(db,1.23)){ printf("true"); } else{ printf("false"); } return 0;}输出:True#include<.原创 2021-06-22 16:23:34 · 98 阅读 · 0 评论 -
c/c++的sscanf和sprintf
sscanf可以认为 string + scanf,sprintf 可以认为 string + printf#include<stdio.h>int main(){ int n; char str[100] = "123"; sscanf(str,"%d",&n); printf("%d\n",n); return 0;}输出:123#include<stdio.h>int main(){ int n = 123; ch原创 2021-06-22 15:30:34 · 216 阅读 · 0 评论 -
leetcode198抢劫问题
题目地址# 解答原创 2021-06-11 14:59:48 · 97 阅读 · 0 评论 -
c++ 调整数组顺序使奇数位于偶数前面
描述:输入一个整数数组,实现一个函数来调整该数组中数字的顺序,使得所有的奇数位于数组的前半部分,所有的偶数位于数组的后半部分,并保证奇数和奇数,偶数和偶数之间的相对位置不变。...转载 2021-06-10 15:01:46 · 367 阅读 · 0 评论