c++个人笔记
头文件
万能头文件(比较建议用这个)
系统头文件
普通头文件
基本框架
#include<bits/stdc++.h>
using namespace std;
int main(){
return 0;
}
变量
整形
浮点
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; 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};int dy[4] = {0,1,0,-1};void dfs(int x,int y){ if(cnt==9){
cout<<x<<' '<<y;
return;
}
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; cnt++;
dfs(nx,ny); }
}
}
int main(){
vis[1][2] = 1;
cnt++;
dfs(1,2);
return 0;
}
深搜模板2
#include<bits/stdc++.h>
using namespace std;
int vis[4][4];
int dx[4] = {1,0,-1,0};int dy[4] = {0,1,0,-1};
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(){
vis[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(){
node a={1,1};
vis[1][1]=1;
q.push(a);
while(q.empty()!=1){
node f=q.front();
if(mp[f.x][f.y]==2){
cout<<"YES"<<endl;
return;
}
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;
}