总结:写了4题,其中符号配对没写全对,还没有找到问题所在,逻辑比较混乱,2天后再修改完成;今天熟悉了堆栈和队列的基本操作。
#include<iostream>
#include<stack>
using namespace std;
struct node {
int n;
char a;
char b;
char c;
};
int main() {
stack<node>stk;
int n;
cin >> n;
node p { n,'a','b','c' };
node to_push;
stk.push(p);
while (!stk.empty()) {
p = stk.top();
stk.pop();
if (p.n == 1) {
//cout << p.a << " -> " << p.c<<endl;
printf("%c -> %c\n", p.a, p.c);//注意cout太慢
}
else {
//3 situations
to_push.n = p.n - 1; to_push.a = p.b; to_push.b = p.a; to_push.c = p.c;
stk.push(to_push);
to_push.n = 1; to_push.a = p.a; to_push.b = p.b; to_push.c = p.c;
stk.push(to_push);
to_push.n = p.n - 1; to_push.a = p.a; to_push.b = p.c; to_push.c = p.b;
stk.push(to_push);
}
}
return 0;
}
#include<iostream>
#include<queue>
using namespace std;
int main() {
queue<int>q1;
queue<int>q2;
int flag = 1, ctn = 0;
int n, m;
cin >> n;
while (n--) {
cin >> m;
if (m % 2 == 0) {
q2.push(m);
}
else {
q1.push(m);
}
}
//末尾没有空格
if (!q1.empty()) {
cout<<q1.front();
q1.pop();
}
else {
cout << q2.front();
q2.pop();
}
while (!q1.empty() && !q2.empty()) {
cout << ' ' << q1.front(); q1.pop();
cout << ' ' << q2.front(); q2.pop();
if (!q1.empty()) {//a2,b1
cout << ' ' << q1.front(); q1.pop();
}
}
while (!q1.empty()) {
cout << ' ' << q1.front(); q1.pop();
}
while (!q2.empty()) {
cout << ' ' << q2.front(); q2.pop();
}
return 0;
}
#include<iostream>
#include<list>
using namespace std;
struct node {
int c;
int e;
};
int main() {
list<node>ls;
node p;
while (cin >> p.c && cin >> p.e) {
if (p.e == 0) {
p.c = 0;
ls.push_back(p);
//可以选择直接不加入,continue,然后print时候ls为空时打印0 0
}
else {
p.c = p.c * p.e;
p.e -= 1;
ls.push_back(p);
}
if (cin.get() == '\n') {//回车退出循环
break;
}
}
//print
for (list<node>::iterator i = ls.begin(); i != ls.end(); i++) {
if (i == ls.begin()) {
cout << i->c << ' ' << i->e ;
}
else {
if (i->c == 0 && i->e == 0) {//多个式子最后存在0 0
continue;
}
cout << ' ' << i->c << ' ' << i->e ;
}
}
return 0;
}
版权声明:本文为weixin_58073817原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。