自定义博客皮肤VIP专享

*博客头图:

格式为PNG、JPG,宽度*高度大于1920*100像素,不超过2MB,主视觉建议放在右侧,请参照线上博客头图

请上传大于1920*100像素的图片!

博客底图:

图片格式为PNG、JPG,不超过1MB,可上下左右平铺至整个背景

栏目图:

图片格式为PNG、JPG,图片宽度*高度为300*38像素,不超过0.5MB

主标题颜色:

RGB颜色,例如:#AFAFAF

Hover:

RGB颜色,例如:#AFAFAF

副标题颜色:

RGB颜色,例如:#AFAFAF

自定义博客皮肤

-+
  • 博客(78)
  • 收藏
  • 关注

原创 插入排序算法

#include<iostream>#define MAX 1000using 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 970

原创 冒泡排序算法

#include<iostream>#define MAX 1000using 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 870

原创 选择排序算法

#include<iostream>#define MAX 1000using 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 1312

原创 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 484

原创 判断是否是二叉搜索树

#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 294

原创 二叉树的高度

#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 334

原创 二叉搜索树-查找最小值和最大值

#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 1376

原创 二叉搜索树实现

#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 78

原创 队列:使用链表实现一个队列

#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 646

原创 中缀到后缀表达式的转换(运用栈实现)

/* 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 7708

原创 后缀表达式求值

/*c++中后缀表达式的求值 输入后缀表达式必须符合要求的格式。 操作数必须是整数,并且两个操作数之间应该有空格。 只能是'+'、'-'、'*'和'/'操作符。 */#include<iostream>#include<stack>#include<string>using namespace std;// Function to evaluate Postfix expression and return outputint Eval.

2022-03-06 14:16:33 368

原创 运用栈反转一个链表

#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 729

原创 利用栈反转字符串

#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 844

原创 栈:使用链表实现一个栈

#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 805

原创 栈:利用数组实现一个栈

#include<iostream>#define MAX_SIZE 101using 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 108

原创 链表(删除操作)

#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 399

原创 链表:在任意位置插入一个节点

#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 1225 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 434

原创 斐波那契数列(递归与记忆)

#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 466 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 370

原创 递归(使用阶乘)

#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<&lt.

2022-02-18 23:20:11 185

原创 链表:头部插入一个节点

将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 830

原创 函数返回指针

一般用于全局变量 或者 堆假如用于栈#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 486 2

原创 内存的概论

分配给一个程序执行的内存或者应用程序的内存 通常分为4个部分代码区 全局区 栈区 这三个区的大小是固定的,在编译期间就决定了。第四个区 被称为堆或者动态内存区不是固定的,堆可以动态按需生长。C:malloc申请内存 new释放内存C++: new申请内存 delete释放内存...

2022-02-11 02:58:54 325

原创 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 88

原创 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 276

原创 指针和字符数组

#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 158

原创 指针与字符数组

#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 173

原创 数组作为函数参数

#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 185

原创 指针地址传递

#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 308

原创 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 391

原创 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 783

原创 顺序查找——指针实现方式

#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 568

原创 顺序查找——数组实现方式

#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 6715

原创 全排列(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 215

原创 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 430

原创 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 341

原创 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 189

原创 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 49

原创 单调栈问题

单调栈单调递增栈:栈中元素从栈底到栈顶是递增的。单调递减栈:栈中元素从栈底到栈顶是递减的。应用:求解下一个大于x元素或者是小于x元素的位置。给一个数组,返回一个大小相同的数组,返回的数组的第i个位置的值应当是,对于原数组中的第i个元素,至少往右走多少步,才能遇到一个比自己大的元素。(如果之后没有比自己大的元素,或者已经是最后一个元素,则在返回数组的对应位置放上-1)#include<bits/stdc++.h>using namespace std;cons

2022-02-04 17:56:29 243

空空如也

空空如也

TA创建的收藏夹 TA关注的收藏夹

TA关注的人

提示
确定要删除当前文章?
取消 删除