Day 41 算法笔记之提高篇(1) 栈的应用,队列的应用

目录

1.栈的实现

2.队列的实现

3.简单计算器

4.Pop Sequence

5.Mice and Rice


1.栈的实现

void clear(){
	top = -1;
}
int size(){
	return top+1;
}
bool empty(){
	if(top==-1) return true;
	else return false;
}
void push(int x){
	st[++top] = x;
}
void pop(){
	top--;
}
int top(){
	return st[top];
}
while(!st.empty()){
	st.pop();
}

2.队列的实现

void clear(){
	front=rear=-1;
}
int size(){
	return rear-front;
}
bool empty(){
	if(front==rear) return true;
	else return false;
}
void push(int x){
	q[++rear] = x;
}
void pop(){
	front++;
}
int getfront(){
	return q[front+1];
}
int get_rear(){
	return q[rear];
}

3.简单计算器

#include<iostream>
#include<cstdio>
#include<string>
#include<stack>
#include<queue>
#include<map>
using namespace std;

struct node{
	double num;
	char op;
	bool flag;
};

string str;
stack<node> s;
queue<node> q;
map<char,int> op;

void change(){
	double num;
	node temp;
	for(int i=0;i<str.length();){
		if(str[i]>='0'&&str[i]<='9'){
			temp.flag=true;
			temp.num=str[i++] - '0';
			while(i<str.length()&&str[i]>='0'&&str[i]<='9'){
				temp.num=temp.num*10+(str[i]-'0');
				i++;
			}
			q.push(temp);
		}else{
			temp.flag=false;
			while(!s.empty()&&op[str[i]]<=op[s.top().op]){
				q.push(s.top());
				s.pop();
			}
			temp.op=str[i];
			s.push(temp);
			i++;
		}
	}
	while(!s.empty()){
		q.push(s.top());
		s.pop();
	}
}

double cal(){
	double temp1,temp2;
	node cur,temp;
	while(!q.empty()){
		cur = q.front();
		q.pop();
		if(cur.flag==true) s.push(cur);
		else{
			temp2 = s.top().num;
			s.pop();
			temp1 = s.top().num;
			s.pop();
			temp.flag=true;
			if(cur.op=='+') temp.num=temp1+temp2;
			else if(cur.op=='-') temp.num=temp1-temp2;
			else if(cur.op=='*') temp.num=temp1*temp2;
			else if(cur.op=='/') temp.num=temp1/temp2;
			s.push(temp);
		}
	}
	return s.top().num;
}

int main(){
	
	op['+'] = op['-'] = 1;
	op['*'] = op['/'] = 2;
	while(getline(cin,str),str!="0"){
		for(string::iterator it=str.end();it!=str.begin();it--){
			if(*it==' ') str.erase(it);
		}
		while(!s.empty()) s.pop();
		change();
		printf("%.2f\n",cal());
	}

	return 0;
}

4.Pop Sequence

#include<cstdio>
#include<stack>
using namespace std;

const int maxn = 1010;
int arr[maxn];
stack<int> st;

int main(){
	int m,n,t;
	scanf("%d%d%d",&m,&n,&t);
	while(t--){
		while(!st.empty()){
			st.pop();
		}
		for(int i=1;i<=n;i++){
			scanf("%d",&arr[i]);
		}
		int current = 1;
		bool flag=true;
		for(int i=1;i<=n;i++){
			st.push(i);
			if(st.size()>m){
				flag=false;
				break;
			}
			while(!st.empty()&&st.top()==arr[current]){
				st.pop();
				current++;
			}
		}
		if(st.empty()==true&&flag==true){
			printf("YES\n");
		}else{
			printf("NO\n");
		}
	}
	return 0 ;
}

5.Mice and Rice

#include<cstdio>
#include<queue>
using namespace std;

const int maxn = 1010;
struct mouse{
	int weight;
	int r;
}mouse[maxn];

int main(){
	
	int np,ng;
	scanf("%d%d",&np,&ng);
	
	for(int i=0;i<np;i++){
		scanf("%d",&mouse[i].weight);
	}
	
	queue<int> q;
	int order;
	for(int i=0;i<np;i++){
		scanf("%d",&order);
		q.push(order);
	}
	
	int temp=np,group;
	while(q.size()!=1){
		if(temp%ng==0) group = temp/ng;
		else group = temp/ng+1;
		for(int i=0;i<group;i++){
			int k = q.front();
			for(int j=0;j<ng;j++){
				if(i*ng+j>=temp) break;
				int front=q.front();
				if(mouse[front].weight>mouse[k].weight){
					k = front;
				}
				mouse[front].r = group+1;
				q.pop();
			}
			q.push(k);
		}
		temp=group;
	}
	
	mouse[q.front()].r=1;
	
	for(int i=0;i<np;i++){
		printf("%d",mouse[i].r);
		if(i<np-1) printf(" ");
	}

	return 0 ;
}

おすすめ

転載: blog.csdn.net/aixiaoxiao13/article/details/121372763