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;
}