队列:
queue<int> que;
que.push(xx); //将数据入队,放入队列末尾
que.pop(); //删除队首元素
que.front(); //访问队首元素
que.back(); //访问队尾元素
que.size(); //获取队列长度
que.empty(); //判断队列是否为空
👀️ 优先队列:
特点:队列中的元素根据优先级自动排列,优先级高的放在队首
头文件:queue
定义一个优先队列:
priority_queue<int> que;
函数:
que.push(xx); //将数据入队,放入队列末尾
que.pop(); //删除队首元素
que.top(); //访问优先级最高的元素
que.size(); //获取队列长度
que.empty(); //判断队列是否为空
//创建栈:
/*
stack <数据类型> 栈的名字;
获取栈顶栈顶元素:栈名.top();
出栈:删除栈顶元素:栈名.pop();
求栈的长度:栈名.size();
栈空:empty() 或者 size()
//清空栈:
while(s.size()){//栈不为空
cout<<s.top()<<" ";
s.pop();
}
*/
stack <int> s;
int main(){
//入栈:
s.push(2);
s.push(5);
s.push(0);
//获取栈顶栈顶元素:栈名.top();
cout<<s.top()<<endl;
//出栈:删除栈顶元素:栈名.pop();
s.pop();
cout<<s.top()<<endl;
//求栈的长度:栈名.size();
cout<<"栈的长度:" <<s.size()<<endl;
//栈空:empty() 或者 size()
cout<<s.empty()<<endl;
//清空栈:
while(s.size()){//栈不为空
cout<<s.top()<<" ";
s.pop();
}
BFS:
#include<bits/stdc++.h>
using namespace std;
const int N=110;
int v[N][N],n,m[N][N],qx,qy,zx,zy;
int dx[]={-1,0,1,0};
int dy[]={0,-1,0,1};
struct node{
int x,y;
};
bool V(){
queue<node> q;
q.push({qx,qy});
v[qx][qy]=1;
while(q.size()){
node a=q.front();
if(a.x==zx && a.y==zy) return 1;
q.pop();
for(int i=0;i<=3;i++){
int xx=a.x+dx[i];
int yy=a.y+dy[i];
if(xx>=1 && xx<=n && yy>=1 && yy<=n && m[xx][yy]!=1 && v[xx][yy]==0){
q.push({xx,yy});
v[xx][yy]=1;
}
}
}
return 0;
}
int main(){
cin>>n;
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
cin>>m[i][j];
}
}
cin>>qx>>qy>>zx>>zy;
if(V()) cout<<"YES";
else cout<<"NO";
}
结构体:用户自己定义一个数据结构,它是由不同的数据类型组成的一个整体,一个属于自己创造的数据类型
定义结构体格式:
struct 结构体名字{
成员列表;
};
定义结构体类型变量的方法:
1.先声明结构体类型再定义变量
例如:
struct stu{
string name;
int age;
};
单个变量初始化:stu s={"RH",10};
结构体数组: stu s[100];
2.在声明结构体的同时定义结构体变量
struct stu{
string name;
int age;
}s[100];
如何访问结构体成员:
格式:
❤️ 结构体变量名.成员变量名
例如:s[1].name s[1].age
概念:sort就是用来排序的函数,它根据具体情形使用不同的排序方法,效率较高。
头文件:algorithm(算法)
使用技巧:sort(首元素地址 , 尾元素地址的下一个地址 );
#include<iostream>
using namespace std;
//自定义函数的定义:
//返回值数据类型 函数名字(数据类型 变量名,数据类型 变量名,...){
// 函数体;//实现具体功能的代码
// return 值;
//}
//函数的声明:
int max2(int a,int b){
if(a>b) return a;
else return b;
}
int main(){//主函数
//调用函数:函数名字(参数1,参数2...);
int x=max2(5,6);
cout<<x;
return 0;
}
#include <iostream>
using namespace std;
int findFourthRoot(int a) {
int low = 1, high = 100; // 四次方根最大为100
while (low <= high) {
int mid = (low + high) / 2;
long long pow4 = 1LL * mid * mid * mid * mid; // 计算mid⁴
if (pow4 == a) {
return mid;
} else if (pow4 < a) {
low = mid + 1;
} else {
high = mid - 1;
}
}
return -1;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int t;
cin >> t;
while (t--) {
int a;
cin >> a;
int res = findFourthRoot(a);
cout << res << "\n";
}
return 0;
}
hydro.ac