c++笔记

wyh15 2024-04-20 16:44:36 2024-05-06 12:09:31 11

c++个人笔记


头文件

万能头文件(比较建议用这个)

#include<bits/stdc++.h>

系统头文件

#include<windows.h>

普通头文件

#include <iostream>

基本框架

#include<bits/stdc++.h>
using namespace std;
int main(){

    return 0;
}

变量

整形

int,long long,stort

浮点

double

oj提交代码注意事项:

Liusers oj 不是用的windows,所以不要用wihdows.h

hello word:

#include<bits/stdc++.h>
using namespace std;
int main(){
    cout<<"hello word!";
    return 0;
}

c++算法:

二分模板

#include<bits/stdc++.h>
using namespace std;
int main(){
    int L = 1,R = 100,mid,num = 45,cnt = 0;//L表示左边,R表示右边的最大值
    while(L<=R){
        mid = (L+R)/2;
        cnt++;//二分找中间值
        if(mid==num){
            break;
        }
        else if(mid>num){
            R = mid - 1;
        }
        else L = mid + 1;
    }
    cout<<cnt;
    return 0;
}

深搜模板

#include<bits/stdc++.h>
using namespace std;
int vis[4][4],cnt = 0;
int dx[4] = {1,0,-1,0};//x的下右上左的变化值 
int dy[4] = {0,1,0,-1};//y的下右上左的变化值 
void dfs(int x,int y){ //(2,2) 
	//搜索出口
	if(cnt==9){
		cout<<x<<' '<<y;
		return;
	} 
	for(int i=0;i<4;i++){//开始四个方向的搜索
		int nx = x+dx[i]; //3 
		int ny = y+dy[i]; //2  nx,ny (3,2) 
		//判断当前点是否在搜索范围内,并且没有被搜索过
		if(nx>0 &&nx<=3 &&ny>0 &&ny<=3 && vis[nx][ny]==0){
			vis[nx][ny] = 1;//马上标记该点 
			cnt++;
			dfs(nx,ny); //下面进行深度优先搜索 
		} 
	}
} 
int main(){
	vis[1][2] = 1;
	cnt++;
	dfs(1,2);
	return 0;
}

深搜模板2

#include<bits/stdc++.h>
using namespace std;
//1.创建标记数组vis(4行4列) 
int vis[4][4]; 
//2.定义x方向数组(4个方向)
int dx[4] = {1,0,-1,0};//下右上左 
int dy[4] = {0,1,0,-1};
//3.定义y方向数组
//4.定义dfs搜索函数
void dfs(int x,int y){
	
	for(int i=0;i<4;i++){
		int nx = x+dx[i];
		int ny = y+dy[i];
		if(nx>0&&nx<=3 &&ny>0&&ny<=3 && vis[nx][ny]==0){
			vis[nx][ny]=1;
			dfs(nx,ny);
		}
	}
}

int main(){
	//对起点(1,1)进行标记
	vis[1][1] = 1;
	//从起点(1,1)开始搜索 
	dfs(1,1);
	return 0;
}

字典树模板

(Hello word作为模板来写的)

#include <iostream>
using namespace std;

struct TrieNode {
    TrieNode* children[26];
    bool isEndOfWord;
};

TrieNode* getNode() {
    TrieNode* node = new TrieNode;
    for (int i = 0; i < 26; i++) {
        node->children[i] = nullptr;
    }
    node->isEndOfWord = false;
    return node;
}

void insertWord(TrieNode* root, string word) {
    TrieNode* current = root;
    for (char c : word) {
        int index = c - 'a';
        if (current->children[index] == nullptr) {
            current->children[index] = getNode();
        }
        current = current->children[index];
    }
    current->isEndOfWord = true;
}

void printTrie(TrieNode* root) {
    if (root->isEndOfWord) {
        cout << "Hello World" << endl;
        return;
    }
    for (int i = 0; i < 26; i++) {
        if (root->children[i] != nullptr) {
            printTrie(root->children[i]);
        }
    }
}

int main() {
    TrieNode* root = getNode();
    insertWord(root, "helloworld");
    printTrie(root);
    return 0;
}

贪心算法(难点)

#include<bits/stdc++.h>
using namespace std;
struct goods{
	int p,n1,d;
};
bool cmp(goods x,goods y){
	return x.d>y.d;
}
int main(){
	int n,l;
    cin>>n>>l;
	goods a[101];
	for(int i=0;i<n;i++){
        cin>>a[i].p>>a[i].n1;
		a[i].d =a[i].p/a[i].n1;
	}
	sort(a,a+n,cmp);
	int sum=0;
	for(int i=0;i<n;i++){
		if(a[i].n1<=l){
			sum = sum + a[i].p;
			l = l - a[i].n1;
		}
		else{
			sum = sum + a[i].d*l;
            break;
		}
	}
	cout<<sum;
	return 0;
}

广搜模板

#include<bits/stdc++.h>
using namespace std;
struct node{
	int x,y;
};
queue<node> q;
int mp[1100][1100];
int vis[1100][1100];
int dx[4]={-1,0,1,0};
int dy[4]={0,1,0,-1};
int n,m;
void bfs(){
	//1.把起点入队
	node a={1,1};
	vis[1][1]=1;
	q.push(a);
	while(q.empty()!=1){
		//2.判断队首元素是不是目标
		node f=q.front();
		if(mp[f.x][f.y]==2){
			cout<<"YES"<<endl;
			return;
		}
		//3.将队首相邻的符合要求的元素入队
		for(int i=0;i<4;i++){
			int nx=f.x+dx[i];
			int ny=f.y+dy[i];
			if(nx>=1 && nx<=n && ny>=1 && ny<=m && mp[nx][ny]==0 && vis[nx][ny]==0){
				vis[nx][ny]=1;
				node r={nx,ny};
				q.push(r);
			}
		}
		q.pop();
	}
	cout<<"NO"<<endl;
}
int main(){
	cin>>n>>m;
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			cin>>mp[i][j];
		}
	}
	bfs();
	
	return 0;
}
{{ vote && vote.total.up }}