栈(stack)
特点:先进后出
参考薯片理解
栈的结构实现建立在结构体上
模拟进制的转换
十进制转换成非十进制:除基取余,逆序输出;
代码实现:
#include <bits/stdc++.h>
using namespace std;
struct stack
{
int data[100];
int top;
};
struct stack s;
int main()
{
int n,d;
cin>>n>>d;
while(n)
{
s.data[s.top++]=n%d;
n=n/d;
}
for(int i=s.top;i>=0;i++)
{
cout<<s.data[i]<<" ";
}
return 0;
}
十进制转化为十六进制:
#include <bits/stdc++.h>
using namespace std;
char a[16]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
struct stack
{
char data[100];
int top;
};
struct stack s;
int main()
{
int n,d;
cin>>n>>d;
while(n)
{
s.data[s.top++]=a[n%d];
n=n/d;
}
for(int i=s.top;i>=0;i++)
{
cout<<s.data[i]<<" ";
}
return 0;
}
括号的匹配:
主要就是看左括号和右括号是否数量相等;
代码实现:
优秀的拆分:
实质考察进制转换:
通过分析可得若n为奇数,则一定不是优秀的拆分;
代码实现:
#include <bits/stdc++.h>
using namespace std;
int n;
int a[50];
int main()
{
int x=0;
cin>>n;
if(n%2)
{
cout<<-1;
}
else
{
while(n!=0)
{
a[x++]=n%2;
n=n/2;
}
for(int i=x-1;i>=0;i--)
{
if(a[i])
{
cout<<pow(2,i)<<" ";
}
}
}
return 0;
}
主要是利用二进制得出拆分,最后用pow函数直接输出结果
队列
特点:先进先出,在表一端插入,在另一端删除;
只能在表的一端插入,在另一端进行删除的操作;
递归:函数中自己使用自己;当多个函数构成嵌套调用时,遵循先调用后返回;
队列的操作和表示:
区别空队和满队
初始化