- 博客(78)
- 收藏
- 关注
原创 插入排序算法
#include<iostream> #define MAX 1000 using namespace std; void Insertionsort(int* A, int n); int main() { int Array[MAX], n; cin >> n; for(int i = 0; i < n; i++) cin >> Array[i]; Insertionsort(Array, n); for(int i = 0; i .
2022-03-13 08:37:25
1064
原创 冒泡排序算法
#include<iostream> #define MAX 1000 using namespace std; void Bubblesort(int* A, int n); int main() { int Array[MAX], n; cin >> n; for(int i = 0; i < n; i++) cin >> Array[i]; Bubblesort(Array, n); for(int i = 0; i < n.
2022-03-12 23:37:29
982
原创 选择排序算法
#include<iostream> #define MAX 1000 using namespace std; void SelectionSort(int* A,int n); int main() { int userArray[MAX], n; cin >> n; for(int i = 0; i < n; i++) cin >> userArray[i]; SelectionSort(userArray, n); for(int i = .
2022-03-12 22:44:43
1418
原创 Object Oriented Programming (OOP) in C++
#include<iostream> using namespace std; class AbstractEmployee { virtual void AskForPromotion() = 0; }; class Employee:AbstractEmployee { private: string Company; int Age; protected: string Name; public: void setName(string name) { .
2022-03-10 16:18:54
594
原创 判断是否是二叉搜索树
#include<iostream> using namespace std; struct Node { int data; Node* left; Node* right; }; bool IsSubtreeLesser(Node* root, int value) { if(root == NULL) return true; if(root->data <= value && IsSubtreeLesser(root->left, v.
2022-03-08 12:36:56
354
原创 二叉树的高度
#include<iostream> using namespace std; struct BstNode { int data; BstNode* left; BstNode* right; }; BstNode* GetNewNode(int data) { BstNode* newNode = new BstNode; newNode->data = data; newNode->left = newNode->right = NULL; retu.
2022-03-07 13:35:29
380
原创 二叉搜索树-查找最小值和最大值
#include<iostream> using namespace std; struct BstNode { int data; BstNode* left; BstNode* right; }; BstNode* GetNewNode(int data) { BstNode* newNode = new BstNode; newNode->data = data; newNode->left = newNode->right = NULL; retu.
2022-03-07 13:21:59
1620
原创 二叉搜索树实现
#include<iostream> using namespace std; struct BstNode { int data; BstNode* left; BstNode* right; }; BstNode* GetNewNode(int data) { BstNode* newNode = new BstNode; newNode->data = data; newNode->left = newNode->right = NULL; retu.
2022-03-07 12:44:10
124
原创 队列:使用链表实现一个队列
#include<iostream> using namespace std; struct Node { int data; Node* next; }; Node* front = NULL; Node* rear = NULL; void Enqueue(int x) { Node* temp = new Node; temp->data = x; temp->next = NULL; if(front == NULL && rear =.
2022-03-06 22:50:12
736
原创 中缀到后缀表达式的转换(运用栈实现)
/* Infix to postfix conversion in C++ Input Postfix expression must be in a desired format. Operands and operator, both must be single character. Only '+' , '-' , '*', '/' and '$' (for exponentiation) operators are expected. */ #include<.
2022-03-06 14:41:52
7865
原创 后缀表达式求值
/* c++中后缀表达式的求值 输入后缀表达式必须符合要求的格式。 操作数必须是整数,并且两个操作数之间应该有空格。 只能是'+'、'-'、'*'和'/'操作符。 */ #include<iostream> #include<stack> #include<string> using namespace std; // Function to evaluate Postfix expression and return output int Eval.
2022-03-06 14:16:33
438
原创 运用栈反转一个链表
#include<iostream> #include<stack> using namespace std; struct Node { int data; Node* next; }; Node* head = NULL; void Reverse() { stack<Node*> S; Node* temp = head; while(temp != NULL) { S.push(temp); temp = temp->next;.
2022-03-04 21:46:29
796
原创 利用栈反转字符串
#include<iostream> #include<cstring> #include<stack> // C++标准库的stack using namespace std; void Reverse(char *C,int n) { stack<int> S; //loop for push for(int i = 0; i < n; i++) { S.push(C[i]); } //loop for pop .
2022-03-03 20:48:24
938
原创 栈:使用链表实现一个栈
#include<iostream> using namespace std; struct Node { int data; Node* link; }; Node* top = NULL; void Push(int x) { Node* temp = new Node; temp->data = x; temp->link = top; top = temp; } void Pop() { Node* temp; if(top == NULL) re.
2022-03-03 20:10:54
896
原创 栈:利用数组实现一个栈
#include<iostream> #define MAX_SIZE 101 using namespace std; int A[MAX_SIZE]; int top = -1; void Push(int x) { if(top == MAX_SIZE - 1) { printf("Error: stack overflow\n"); return; } top++; A[top] = x;//A[++top] = x } void Pop() { if(.
2022-03-03 18:51:48
149
原创 链表(删除操作)
#include<iostream> using namespace std; struct Node { int data; Node* next; }; Node* head; void Insert(int data)//在链表尾部插入节点 { Node *temp = new Node; temp->data = data; temp->next = NULL; if(head == NULL) { head = temp; } el.
2022-03-01 11:49:06
492
原创 链表:在任意位置插入一个节点
#include<iostream> using namespace std; struct Node { int data; Node* next; }; Node* head ; //接收两个参数:插入的数据和想要插入的位置 void Insert(int data,int n) { Node* temp1 = new Node; temp1->data = data; temp1->next = NULL; if(n == 1) { temp.
2022-02-25 00:04:52
1326
1
原创 幂POW(递归实现)
第一种:时间复杂度O(logn) #include<iostream> using namespace std; int POW(int a,int b) { if(b==0) return 1; if(b&1) return a * POW(a, b - 1); else { int y = POW(a, b/2); return y * y; } } int main() { int x, n; cin >> x >> n;
2022-02-19 23:04:01
524
原创 斐波那契数列(递归与记忆)
#include<iostream> using namespace std; int F[51]; int Fib(int n) { if(F[n] != -1) return F[n]; F[n] = Fib(n - 1) + Fib(n - 2); return F[n]; } int main() { for(int i = 0; i < 51; i++) F[i] = -1; F[0] = 0; F[1] = 1; int n; cout.
2022-02-19 15:23:50
539
1
原创 斐波那契数列(递归与递推)
#include<iostream> using namespace std; int Fib(int n) { if(n <= 1) return n; return Fib(n - 1) + Fib(n - 2); } int main() { int n; cout << "Give me an n: "; cin >> n; int result = Fib(n); cout << result; return 0;.
2022-02-19 13:41:13
450
原创 递归(使用阶乘)
#include<iostream> using namespace std; int Factorial(int n) { cout << "I am calculating F("<< n <<")\n"; if(n == 0) return 1; int F = n * Factorial(n - 1); cout << "Done ! F("<< n <<") = "<<F<<.
2022-02-18 23:20:11
229
原创 链表:头部插入一个节点
将head声明为全局变量 #include<iostream> using namespace std; struct Node { int data; Node* next; }; Node* head;//将head声明为全局变量 void Insert(int x) { Node* temp = new Node; temp -> data = x; temp -> next = head; head = temp; } void Prin
2022-02-16 01:34:05
920
原创 函数返回指针
一般用于全局变量 或者 堆 假如用于栈 #include<iostream> using namespace std; void PrintHelloWorld() { printf("Hello World\n"); } int *Add(int *a,int *b) { int c = *a + *b; return &c; } int main() { int a = 2, b = 4; int* ptr = Add(&a, &b);
2022-02-11 04:08:04
554
2
原创 内存的概论
分配给一个程序执行的内存或者应用程序的内存 通常分为4个部分 代码区 全局区 栈区 这三个区的大小是固定的,在编译期间就决定了。 第四个区 被称为堆或者动态内存区不是固定的,堆可以动态按需生长。 C:malloc申请内存 new释放内存 C++: new申请内存 delete释放内存 ...
2022-02-11 02:58:54
361
原创 malloc calloc realloc free
malloc #include<bits/stdc++.h> using namespace std; int main() { int n; printf("Enter size of array\n"); scanf("%d",&n); int* A = (int*)malloc(n*sizeof(int)); for(int i = 0; i < n; i++) A[i] = i + 1; for(int i = 0; i < n;
2022-02-10 20:48:57
146
原创 new 和 delete
#include<iostream> using namespace std; int main() { int a; //在栈中 int *p; p = new int;//p指针 指向堆中的一块内存 *p = 10; delete p; //撤销内存 p = new int[20];//p指针 指向堆中的一块连续内存 p[0] = 1; *(p + 1) = 2;//p[i]等价于*(p + i) cout << p[0] << e.
2022-02-10 17:54:08
307
原创 指针和字符数组
#include<iostream> using namespace std; void printf(char *C) { int i = 0; while(*(C + i) != '\0') { printf("%c",C[i]); i ++; } printf("\n"); } int main() { char C[20] = "Hello" ; printf(C); return 0; } #include<iostream> u.
2022-02-10 02:36:50
192
原创 指针与字符数组
#include<iostream> #include<cstring> using namespace std; int main() { char c[ ] = "Hello"; printf("Size in bytes = %d\n",sizeof(c)); int len = strlen(c); printf("Length = %d\n",len); return 0; }
2022-02-10 02:32:19
215
原创 数组作为函数参数
#include<iostream> using namespace std; void Double(int a[], int size) { for(int i = 0; i < size; i++) a[i] *= 2; } int main() { int a[ ] = {1, 2, 3, 4, 5}; int size = sizeof(a) / sizeof(a[0]); Double(a, size); for(int i = 0; i < size.
2022-02-10 01:33:51
220
原创 指针地址传递
#include<iostream> using namespace std; void add(int* p) { *p = 88; } int main() { int a = 5; add(&a); cout << a << endl; return 0; }
2022-02-10 00:53:43
385
原创 P1458 [USACO2.1]顺序的分数 Ordered Fractions
P1458 [USACO2.1]顺序的分数 Ordered Fractions - 洛谷 | 计算机科学教育新生态 (luogu.com.cn) #include<bits/stdc++.h> using namespace std; struct node { int fz,fm; //分子 分母 }a[100010]; int n; int gcd(int a, int b) //辗转相除法 { if(b == 0) return a; return gcd(b, a
2022-02-08 00:49:34
498
原创 P1376 [USACO05MAR]Yogurt factory 机器工厂
P1376 [USACO05MAR]Yogurt factory 机器工厂 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn) #include<bits/stdc++.h> using namespace std; long long ans; int n, s, c, y; int main() { cin >> n >> s; cin >> c >> y; ans = c * y; int minn = c;
2022-02-06 21:27:43
899
原创 顺序查找——指针实现方式
#include<iostream> using namespace std; const int N = 6; //假定数组有N个元素 int main() { int a[N] = {2, 4, 8, 6, 5, 3},i,key,index = -1; int* p = a; //指向数组首地址 key = 8; //假设待查找的元素值为8 for( ; p < a + N; p++) if( *p == key) { index = p - .
2022-02-06 14:28:14
631
原创 顺序查找——数组实现方式
#include<iostream> using namespace std; const int N = 6; //假定数组有N个元素 int main() { int a[N] = {2, 4, 8, 6, 5, 3},i,key,index = -1; key = 8; //假设待查找的元素值为9 for(i = 0; i < N; i++) if(a[i] == key) { index = i; //找到,提前退出循环 break; }.
2022-02-06 13:16:23
6829
原创 全排列(dfs)
#include<bits/stdc++.h> using namespace std; int n; int a[1000]; //a[i]表示第i个盒子放了哪一个球 int v[1000]; //v[i]表示第i个球是否在手里面 void dfs(int cur) //当面临第cur个盒子时,放置球的情况 { if(cur == n + 1) // 递归出口 { //输出这组解 for(int i = 1; i <= n; i++) cout <&.
2022-02-05 02:19:52
271
原创 P1209 [USACO1.3]修理牛棚 Barn Repair
P1209 [USACO1.3]修理牛棚 Barn Repair - 洛谷 | 计算机科学教育新生态 (luogu.com.cn) #include<bits/stdc++.h> using namespace std; int m, s, c; int a[210], b[210];//牛的位置, 空隙 bool cmp(int a, int b) { return a > b; } int main() { cin >> m >> s >
2022-02-05 01:24:47
508
原创 P1211 [USACO1.3]牛式 Prime Cryptarithm
P1211 [USACO1.3]牛式 Prime Cryptarithm - 洛谷 | 计算机科学教育新生态 (luogu.com.cn) #include<bits/stdc++.h> using namespace std; int a[10], n; //a[10]为桶数组 bool check(int x, int y) //检查x是否是符合条件的y位数 { if(x >= pow(10,y)) return false;//判断位数 while(x >
2022-02-05 01:02:30
431
原创 P1217 [USACO1.5]回文质数 Prime Palindromes
P1217 [USACO1.5]回文质数 Prime Palindromes - 洛谷 | 计算机科学教育新生态 (luogu.com.cn) #include<bits/stdc++.h> using namespace std; int a, b; bool isHuiwen(int x) { int t = x, n = 0; while(x > 0) { n = n * 10 + x % 10; x /= 10; } return (t == n); }
2022-02-05 00:43:54
247
原创 POJ3250
Bad Hair Day - POJ 3250 - Virtual Judge #include<bits/stdc++.h> using namespace std; stack<int>s; int main() { int n; while(scanf("%d",&n)==1) { int num; long long sum = 0; scanf("%d",&num); stack<int>s; while(!s.
2022-02-04 22:16:34
79
原创 单调栈问题
单调栈 单调递增栈:栈中元素从栈底到栈顶是递增的。 单调递减栈:栈中元素从栈底到栈顶是递减的。 应用:求解下一个大于x元素或者是小于x元素的位置。 给一个数组,返回一个大小相同的数组,返回的数组的第i个位置的值应当是,对于原数组中的第i个元素,至少往右走多少步,才能遇到一个比自己大的元素。 (如果之后没有比自己大的元素,或者已经是最后一个元素,则在返回数组的对应位置放上-1) #include<bits/stdc++.h> using namespace std; cons
2022-02-04 17:56:29
303
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人