SDUT PTA 22级数据结构与算法实验3——栈和队列

7-1 银行业务队列简单模拟

#include<bits/stdc++.h>
using namespace std;
int main()
{
	queue<int> q1, q2;
	int n;
	cin >> n;
	while(n --)
	{
		int m;
		cin >> m;
		if(m % 2) q1.push(m);
		else q2.push(m);
	}
	while(!q1.empty())
	{
		int cnt = 2, i = 0;
		while(cnt -- && !q1.empty()) 
        {
			if(i ++) cout << " ";
			cout << q1.front();
			q1.pop();
		}
		if(!q2.empty())
        {
			cout << " " << q2.front() << " ";
			q2.pop();
		}
	}
	int i = 0;
	while(!q2.empty())
	{
		if(i ++) cout << " ";
		cout << q2.front();
		q2.pop();
	}
}

7-2 表达式转换

#include<bits/stdc++.h>
using namespace std;
stack<char> st;
map<char, int> mp;
int main()
{
	mp['+'] = mp['-'] = 1;
	mp['*'] = mp['/'] = 2;
	string s;
	cin >> s;
	int flag = 1;
	for(int i = 0; i < s.length(); i ++)
	{
		if(isdigit(s[i]) || s[i] == '.' || ((i == 0 || s[i - 1] == '(') && (s[i] == '+' || s[i] == '-')))
		{
			if(!flag) cout << " ";
			else flag = 0;
			if(s[i] != '+') cout << s[i];
			while(s[i + 1] == '.' || isdigit(s[i + 1])) cout << s[++ i];
		}
		else
		{
			if(s[i] == '(') st.push(s[i]);
			else if(s[i] == ')')
			{
				while(!st.empty() && st.top() != '(')
				{
					cout << " " << st.top();
					st.pop();
				}
				st.pop();
			}
			else if(st.empty() || mp[s[i]] > mp[st.top()]) st.push(s[i]);
			else
			{
				while(!st.empty() && st.top() != '(')
				{
					cout << " " << st.top();
					st.pop();
				}
				st.push(s[i]);
			}
		}
	}
	while(!st.empty())
	{
		cout << " " << st.top();
		st.pop();
	}
}

7-3 堆栈模拟队列

#include <bits/stdc++.h>
using namespace std;
stack<int> a, b;
int main()
{
    int n, m;
    cin >> n >> m;
    if(n > m) swap(n, m);
    while(1)
    {
        char t;
        int k;
        cin >> t;
        if(t == 'T') break;
        else if(t == 'A')
        {
            cin >> k;
            if(a.size() < n) a.push(k);
            else if(b.empty() && a.size() == n)
            {
                while(!a.empty())
                {
                    b.push(a.top());
                    a.pop();
                }
                a.push(k);
            }
            else cout << "ERROR:Full" << endl;
        }
        else
        {
            if(!b.empty())
            {
                cout << b.top() << endl;
                b.pop();
            }
            else if(b.empty() && !a.empty())
            {
                while(!a.empty())
                {
                    b.push(a.top());
                    a.pop();
                }
                cout << b.top() << endl;
                b.pop();
            }
            else cout << "ERROR:Empty" << endl;
        }
    }
}

7-4 输出全排列

#include <bits/stdc++.h>
using namespace std;
int main()
{
	string str;
	int n;
	cin >> n;
	for(int i = 0; i < n; i ++)
		str += i + 1 + '0';
	sort(str.begin(),str.end());
	do {
		cout << str << endl;
	} while(next_permutation(str.begin(),str.end()));
}

7-5 括号匹配

#include <bits/stdc++.h>
using namespace std;
stack<int> stk;
int main()
{
    string s, x;
    getline(cin, x);
    for(int i = 0; i < x.size(); i ++)
        if(x[i] == '(' || x[i] == ')' || x[i] == '[' || x[i] == ']' || x[i] == '{' || x[i] == '}')
            s += x[i];
    if(s.size() < 2) 
    {
        cout << "no" << endl;
        return 0;
    }
    for(int i = 0; i < s.size(); i ++)
    {
        if(s[i] == '(' || s[i] == '[' || s[i] == '{')
            stk.push(s[i]);
        else
        {
            if(stk.empty())
            {
                stk.push(s[i]);
                break;
            }
            if(s[i] == ')' && stk.top() == '(') stk.pop();
            else if(s[i] == ']' && stk.top() == '[') stk.pop();
            else if(s[i] == '}' && stk.top() == '{') stk.pop();
        }
    }
    if(!stk.empty()) cout << "no" << endl;
    else cout << "yes" << endl;
}

7-6 出栈序列的合法性

#include <bits/stdc++.h>
using namespace std;
int main()
{
    int n, m, k;
    cin >> m >> n >> k;
    while(k --)
    {
        queue<int> q;
        stack<int> stk;
        int flag = 0;
        for(int i = 0; i < n; i ++)
        {
            int t;
            cin >> t;
            q.push(t);
        }
        for(int i = 1; i <= n; i ++)
        {
            stk.push(i);
            while(!stk.empty() && q.front() == stk.top())
            {
                stk.pop();
                q.pop();
            }
            if(stk.size() >= m) flag = 1;
        }
        if(!q.empty()) flag = 1;
        if(flag) cout << "NO" << endl;
        else cout << "YES" << endl;
    }
}

7-7 后缀式求值

#include <bits/stdc++.h>
using namespace std;
string s;
double n1, n2;
stack<double> stk;
int main()
{
    getline(cin, s);
    int n = s.size();
    for(int i = 0; i < n; i ++)
    {
        if(s[i] == ' ') continue;
        else if ((s[i] == '+' || s[i] == '-' || s[i] == '*' || s[i] == '/') && (i == n - 1 || s[i + 1] == ' '))
        {
            n1 = stk.top();
            stk.pop();
            n2 = stk.top();
            stk.pop();
            if(s[i] == '+') stk.push(n1 + n2);
            else if (s[i] == '-') stk.push(n2 - n1);
			else if (s[i] == '*') stk.push(n1 * n2);
			else stk.push(n2 / n1);
        }
        else 
        {
            string t = "";
            while(s[i] != ' ')
            {
                t += s[i];
                i ++;
            }
            n1 = stof(t);
            stk.push(n1);
        }
    }
    printf("%.1f", stk.top());
}

7-8 进制转换

#include <bits/stdc++.h>
using namespace std;
stack<int> stk;
int main()
{
    int n, m;
    cin >> n >> m;
    if(m == 2)
    {
        while(n)
        {
            stk.push(n % 2);
            n /= 2;
        }
        while(!stk.empty())
        {
            cout << stk.top();
            stk.pop();
        }
    }
    else if(m == 8) printf("%o", n);
    else printf("%X", n);
}

7-9 行编辑器

#include <bits/stdc++.h>
using namespace std;
int main()
{
    string s;
    while(getline(cin, s))
    {
        int k = 0;
        string ss;
        for(int i = 0; i < s.size(); i ++)
        {
            if(s[i] == '#' && i == 0) continue;
            else if(s[i] == '#') k --;
            else if(s[i] == '@') k = 0;
            else ss[k ++] = s[i];
        }
        for(int i = 0; i < k; i ++) cout << ss[i];
        cout << endl;
    }
}

7-10 选数

#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10, M = 25;
int a[M], b[M], c[N], p[N];
int n, m, d, ans;
int prime(int n)
{
    if(n == 0 || n == 1) return 0;
    for(int i = 2; i <= n / i; i ++)
        if(n % i == 0)
            return 0;
    return 1;
}
void dfs(int x, int y)
{
    if(x == m)
    {
        if(c[d] == 0)
        {
            c[d] == 1;
            p[d] = prime(d);
        }
        if(p[d]) ans ++;
        return;
    }
    for(int i = y; i <= n; i ++)
    {
        if(b[i] == 0)
        {
            b[i] = 1;
            d += a[i];
            dfs(x + 1, i + 1);
            b[i] = 0;
            d -= a[i];
        }
    }
}

int main()
{
    cin >> n >> m;
    for(int i = 1; i <= n; i ++)
        cin >> a[i];
    sort(a + 1, a + n  + 1);
    dfs(0, 1);
    cout << ans;
}

  • 14
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

村长二甫弘

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值