2021算法笔记Codeup、pat刷题记录
《算法笔记》7.1小节——数据结构专题(1)->栈的应用
Codeup
简单计算器
#include<iostream>
#include<cstdio>
#include<string>
#include<map>
#include<stack>
#include<queue>
using namespace std;
struct node {
double num;//操作数
char op;//操作符
bool flag;//ture 表示操作数,false表示操作符
};
string str;
stack<node> s;
queue<node> q;
map<char, int> mp;
void change() {//将中缀表达式转换成后缀
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 = 10 * temp.num + str[i] - '0';
++i;
}
q.push(temp);
}
else {
temp.flag = false;
while (!s.empty() && mp[str[i]] <= mp[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 calculate() {
node cur;
while (!q.empty()) {
cur = q.front();
q.pop();
if (cur.flag == true) s.push(cur);//如果是数,则压入栈中
else {//为操作符则拿出前两个数进行计算
node temp;
temp.flag = true;
double temp1, temp2;
temp2 = s.top().num;
s.pop();
temp1 = s.top().num;
s.pop();
if (cur.op == '+') temp.num = temp1 + temp2;
else if (cur.op == '-') temp.num = temp1 - temp2;
else if (cur.op == '*') temp.num = temp1 * temp2;
else temp.num = temp1 / temp2;
s.push(temp);
}
}
return s.top().num;
}
int main() {
mp['+'] = mp['-'] = 1;
mp['*'] = mp['/'] = 2;
while (getline(cin, str), str != "0") {
for (string::iterator it = str.begin();it != str.end();++it) {
if (*it == ' ') str.erase(it);
}
while (!s.empty()) s.pop();//这里不写的话会导致后续结果出错
change();
// while(!q.empty()){//中间段的测试代码
// if(q.front().flag==1) printf("%f",q.front().num);
// else printf("%c",q.front().op);
// q.pop();
// }
printf("%.2f\n", calculate());
}
return 0;
}
Problem E
一会儿要while(EOF)一会儿又不用,搞的输出超限- -。
#include<iostream>
#include<cstdio>
#include<stack>
using namespace std;
int main() {
int n;
scanf("%d", &n);
getchar();
while (n--) {
string str;
getline(cin, str);
stack<char> st;
bool flag;
for (int i = 0;i < str.length();++i) {
flag = true;
if (str[i] == '(' || str[i] == '[' || str[i] == '{') st.push(str[i]);
else if (str[i] == ')') {
if (!st.empty() && st.top() == '(') st.pop();
else {
flag = false;
break;
}
}
else if (str[i] == ']') {
if (!st.empty() && st.top() == '[') st.pop();
else {
flag = false;
break;
}
}
else if (str[i] == '}') {
if (!st.empty() && st.top() == '{') st.pop();
else {
flag = false;
break;
}
}
}
if (st.empty() && flag) printf("yes\n");
else printf("no\n");
}
return 0;
}
配套实战指南
PAT A1051 Pop Sequence
#include<cstdio>
#include<stack>
#include<queue>
using namespace std;
int main() {
int m, n, k;
scanf("%d %d %d", &m, &n, &k);
while (k--) {
stack<int> st;
queue<int> q;
bool flag = true;
for (int j = 0;j < n;++j) {
int temp;
scanf("%d", &temp);
q.push(temp);
}
for (int i = 1;i <= n;++i) {
st.push(i);
if (st.size() > m) {
flag = false;
break;
}
while (!st.empty() && st.top() == q.front()) {
st.pop();
q.pop();
}
}
if (st.empty() && flag == true) printf("YES\n");
else printf("NO\n");
}
return 0;
}
《算法笔记》7.2小节——数据结构专题(1)->队列的应用
Codeup
C语言-数字交换
#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;
void scan(vector<int>& vi) {
int n;
for (int i = 0;i < 10;++i) {
scanf("%d", &n);
vi.push_back(n);
}
}
void change(vector<int>& vi) {
int min = 100000, max = -1, minn = 0, maxn = 0;
for (int i = 0;i < 10;++i) {
if (min > vi[i]) {
min = vi[i];
minn = i;
}
}
swap(vi[minn], vi[0]);
for (int i = 0;i < 10;++i) {
if (max < vi[i]) {
max = vi[i];
maxn = i;
}
}
swap(vi[maxn], vi[9]);
}
void print(vector<int> vi) {
for (int i = 0;i < 10;++i) printf("%d ", vi[i]);
}
int main() {
vector<int> vi;
scan(vi);
change(vi);
print(vi);
return 0;
}
配套实战指南
PAT A1056 Mice and Rice
#include<cstdio>
#include<queue>
using namespace std;
const int maxn = 1010;
struct mouse {
int weight;
int rank;
}m[maxn];
int main() {
int np, ng;
scanf("%d %d", &np, &ng);
for (int i = 0;i < np;++i) scanf("%d", &m[i].weight);
queue<int> q;
for (int i = 0;i < np;++i) {
int order;
scanf("%d", &order);
q.push(order);
}
int group, temp = np;
while (q.size() != 1) {
if (temp % ng == 0) group = temp / ng;
else group = temp / ng + 1;
//计算组数
for (int i = 0;i < group;++i) {//枚举每一组
int heaviest = q.front();
for (int j = 0;j < ng;++j) {//找到同组中质量最大的老鼠
if (i * ng + j >= temp) break;
int front = q.front();
if (m[heaviest].weight < m[front].weight) heaviest = front;
m[front].rank = group + 1;//晋级的老鼠为group只,因此未晋级的老鼠的排名均为group+1
q.pop();//出队以找到同组中质量最大的老鼠
}
q.push(heaviest);
}
temp = group;//group只老鼠晋级,因此下一轮总老鼠数为group
}
m[q.front()].rank = 1;//队列中剩余一只老鼠时,令其排名为1
for (int i = 0;i < np;++i) {
if (i > 0) printf(" ");
printf("%d", m[i].rank);
}
return 0;
}
《算法笔记》7.3小节——数据结构专题(1)->链表处理
Codeup
算法2-8~2-11:链表的基本操作
写了好久,得有快俩小时,除了许多的BUG,不敢相信,练习练习了链表。
#include<cstdio>
#include<cstring>
typedef struct LNode {
int data;
struct LNode* next;
}LNode;
void Createlist(LNode*& head, int n) {
head = new LNode;
head->next = NULL;
for (int i = 0;i < n;++i) {
LNode* p = new LNode;
scanf("%d", &p->data);
p->next = head->next;
head->next = p;
}
}
void Show_list(LNode* p) {
int cnt = 0;
if (p == NULL) printf("Link list is empty\n");
else {
while (p) {
if (cnt != 0) printf(" ");
printf("%d", p->data);
p = p->next;
++cnt;
}
printf("\n");
}
}
void Del_elem(LNode* p, int n) {
for (int i = 1;i < n && p->next != NULL;++i) p = p->next;//此时p指向删除元素的前一个
if (p->next == NULL) printf("delete fail\n");
else {
LNode* q = p->next;//q指向待删除元素
p->next = p->next->next;
delete(q);
printf("delete OK\n");
}
}
void Insert_elem(LNode* p, int n, int m) {
for (int i = 1;i < n && p != NULL;++i) p = p->next;//此时p指向待插入元素的前一个
if (p == NULL) printf("insert fail\n");
else {
LNode* q = new LNode;
q->next = p->next;
q->data = m;
p->next = q;
printf("insert OK\n");
}
}
void Get_elem(LNode* p, int n) {
for (int i = 0;i < n && p != NULL;++i) p = p->next;
if (p == NULL) printf("get fail\n");
else printf("%d\n", p->data);
}
int main() {
int n;
scanf("%d", &n);
LNode* head;
Createlist(head, n);
scanf("%d", &n);
for (int i = 0;i < n;++i) {
char str[10];
scanf("%s", str);
if (strcmp(str, "show") == 0) {
Show_list(head->next);//head->next
}
else if (strcmp(str, "delete") == 0) {
int m;
scanf("%d", &m);
Del_elem(head, m);
}
else if (strcmp(str, "insert") == 0) {
int m, k;
scanf("%d %d", &m, &k);
Insert_elem(head, m, k);
}
else if (strcmp(str, "get") == 0) {
int m;
scanf("%d", &m);
Get_elem(head, m);
}
}
return 0;
}
C语言-链表排序
这题不是欺负老实人吗,用数列啪啪两下就写完了,用链表愣是写了60多行,大家不要随意模仿。(好像可以最开始就创建成有序链表,这样排序的时候会方便很多)
简单题还是会粗心导致bug
#include<cstdio>
typedef struct LNode {
int ID;
int grade;
struct LNode* next;
}LNode;
void Create_list(LNode*& L, int n) {
L = new LNode;
L->next = NULL;
LNode* p;
for (int i = 0;i < n;++i) {
p = new LNode;
scanf("%d %d", &p->ID, &p->grade);
p->next = L->next;//头插法
L->next = p;
}
}
LNode* Merge_list(LNode* h1, int n, LNode* h2, int m) {
LNode* L = new LNode, * q, * p, * maxq, * maxp;
L->next = NULL;
int max = -1;
while (h1->next != NULL || h2->next != NULL) {
max = -1, q = h1, p = h2;
while (q->next) {
if (q->next->ID > max) {
max = q->next->ID;
maxq = q;
maxp = q->next;
}
q = q->next;
}
while (p->next) {
if (p->next->ID > max) {
max = p->next->ID;
maxq = p;
maxp = p->next;
}
p = p->next;
}
maxq->next = maxp->next;
maxp->next = L->next;
L->next = maxp;
}
return L;
}
void print(LNode* L) {
L = L->next;
while (L != NULL) {
printf("%d %d\n", L->ID, L->grade);
L = L->next;
}
}
int main() {
LNode* h1, * h2;
int n, m;
scanf("%d %d", &n, &m);
Create_list(h1, n);
Create_list(h2, m);
LNode* res = Merge_list(h1, n, h2, m);
print(res);
return 0;
}
最快合并链表(线性表)
#include<cstdio>
typedef struct LNode {
int data;
struct LNode* next;
}LNode;
void Create_list(LNode*& L, int n) {//生成循环单链表
L = new LNode;
L->next = L;
LNode* p, * q = L;
for (int i = 0;i < n;++i) {
p = new LNode;
scanf("%d", &p->data);
p->next = q->next;
q->next = p;
q = p;//尾插法
}
}
LNode* Merge(LNode* L1, LNode* L2) {
LNode* q = L1, * p = L2->next, * s;
while (q->next != L1) q = q->next;//将q指到L1的最后一位
while (p != L2) {
s = p;
p = p->next;
s->next = q->next;
q->next = s;
q = s;
}
return L1;
}
void printList(LNode* L) {
LNode* p = L->next;
int cnt = 0;
while (p != L) {
if (cnt) printf(" ");
printf("%d", p->data);
p = p->next;
++cnt;
}
}
int main() {
int m, n;
LNode* L1, * L2;
scanf("%d", &m);
Create_list(L1, m);
scanf("%d", &n);
Create_list(L2, n);
LNode* res = Merge(L1, L2);
printList(res);
return 0;
}
链表查找(线性表)
#include<cstdio>
typedef struct LNode {
int data;
struct LNode* next;
}LNode;
void Create_list(LNode*& L, int n) {//生成链表
L = new LNode;
L->next = NULL;
LNode* p, * q = L;
for (int i = 0;i < n;++i) {
p = new LNode;
scanf("%d", &p->data);
p->next = q->next;
q->next = p;
q = p;//尾插法
}
}
bool Find_x(LNode*& L, int x) {
LNode* p = L->next, * q = L;
while (p->data <= x) {
if (p->data == x) {
int temp = p->data;
p->data = p->next->data;
p->next->data = temp;
return true;
}
q = p;
p = p->next;
}
LNode* s = new LNode;
s->data = x;
s->next = q->next;
q->next = s;
return false;
}
void printList(LNode* L) {
LNode* p = L->next;
int cnt = 0;
while (p != NULL) {
if (cnt) printf(" ");
printf("%d", p->data);
p = p->next;
++cnt;
}
}
int main() {
int n, x;
LNode* L;
scanf("%d%d", &x, &n);
Create_list(L, n);
if (Find_x(L, x)) {
printf("yes\n");
printList(L);
}
else {
printf("no\n");
printList(L);
}
return 0;
}
算法2-24 单链表反转
最开始写了个尾插法插入然后倒置后输出的,超时了,于是就写了这种直接头插法输出的- -
#include<cstdio>
typedef struct LNode {
int data;
struct LNode* next;
}LNode;
void Create_list(LNode*& L, int n) {//生成链表
L = new LNode;
L->next = NULL;
for (int i = 0;i < n;++i) {
LNode* p = new LNode;
scanf("%d", &p->data);
printf("%d ", p->data);
p->next = L->next;
L->next = p;//头插法
}
}
void printList(LNode* L) {
LNode* p = L->next;
int cnt = 0;
while (p != NULL) {
if (cnt) printf(" ");
printf("%d", p->data);
p = p->next;
++cnt;
}
printf("\n");
}
int main() {
int n;
LNode* L;
while (scanf("%d", &n) != EOF) {
if (n == 0) {
printf("list is empty");
break;
}
Create_list(L, n);
printf("\n");
printList(L);
}
return 0;
}
错误代码也放出来吧
#include<cstdio>
typedef struct LNode {
int data;
struct LNode* next;
}LNode;
void Create_list(LNode*& L, int n) {//生成链表
L = new LNode;
L->next = NULL;
LNode* q = L;
for (int i = 0;i < n;++i) {
LNode* p = new LNode;
scanf("%d", &p->data);
p->next = q->next;
q->next = p;//尾插法
q = p;
}
}
void reverse(LNode*& L, int n) {
if(n==1) return;
LNode* s = L;
for (int i = 0;i < n;++i) {
LNode* p = L->next, * q;
while (p->next != NULL) {
q = p;
p = p->next;
}
q->next = p->next;
p->next = s->next;
s->next = p;
s = p;
}
}
void printList(LNode* L) {
LNode* p = L->next;
int cnt = 0;
while (p != NULL) {
if (cnt) printf(" ");
printf("%d", p->data);
p = p->next;
++cnt;
}
printf("\n");
}
int main() {
int n;
LNode* L;
while (scanf("%d", &n) != EOF) {
if (n == 0) {
printf("list is empty");
break;
}
Create_list(L, n);
printList(L);
reverse(L, n);
printList(L);
}
return 0;
}
算法2-25 有序单链表删除重复元素
代码耗时19062ms,跑了那么久还以为自己写错了,结果是正确的;
#include<cstdio>
typedef struct LNode {
int data;
struct LNode* next;
}LNode;
void Create_list(LNode*& L, int n) {//生成链表
L = new LNode;
L->next = NULL;
for (int i = 0;i < n;++i) {
LNode* p = new LNode, * q = L;
scanf("%d", &p->data);
while (q->next && q->next->data < p->data) q = q->next;
if (q->data != p->data) {
p->next = q->next;
q->next = p;
}
}
}
void Delete_same(LNode*& L) {
LNode* p = L->next;
while (p->next) {
while (p->next && p->next->data == p->data) {
LNode* s = p->next;
p->next = p->next->next;
delete(s);//养成好习惯
}
if (p->next) p = p->next;//不然会导致p变成NULL
}
}
void printList(LNode* L) {
LNode* p = L->next;
int cnt = 0;
while (p != NULL) {
if (cnt) printf(" ");
printf("%d", p->data);
p = p->next;
++cnt;
}
printf("\n");
}
int main() {
int n;
LNode* L;
while (scanf("%d", &n) != EOF) {
if (n == 0) {
printf("list is empty");
break;
}
Create_list(L, n);
printList(L);
Delete_same(L);
printList(L);
}
return 0;
}
可以把Delete_same函数改成这样一次性删除。虽然没释放内存
void Delete_same(LNode*& L) {
LNode* p = L->next;
while (p->next) {
if (p->next->data == p->data) {
LNode* s = p->next;
while (s->next && p->data == s->next->data) s = s->next;
p->next = s->next;
}
if (p->next) p = p->next;//不然会导致p变成NULL
}
}
配套实战指南
PAT A1032 Sharing
简单题,但还是犯了一些小错误
#include<cstdio>
const int maxn = 100010;
struct Node {
char c;
int next;
}node[maxn];
int main() {
int s1, s2, n;
bool hashTable[maxn] = { false };
scanf("%d %d %d", &s1, &s2, &n);
while (n--) {
int p, next;
char c;
scanf("%d %c %d", &p, &c, &next);
node[p].c = c;
node[p].next = next;
}
int res;
for (int i = s1;i != -1;i = node[i].next) hashTable[i] = true;//这里要写i!=-1不能写node[i].next!=-1 不然会导致最后一个结点没读入
for (res = s2;res != -1;res = node[res].next) {
if (hashTable[res] == true) break;
}
if (res != -1) printf("%05d\n", res);
else printf("-1\n");
return 0;
}
PAT A1052 Linked List Sorting
题目有挺多坑的,查错花了挺久。
1、题目可能会有无效结点,因此需要从题目里给的起始结点开始走一遍标记flag
2、题目中可能会给出均为无效的情况,即起始结点为-1,这是需要特判并输出“0 -1”
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn = 100010;
struct Node {
int adr;
int key;
int next;
int flag;
Node() {
flag = -1;
}
}node[maxn];
bool cmp(Node a, Node b) {
if (a.flag != b.flag) return a.flag > b.flag;
else return a.key < b.key;
}
int main() {
int n, s;
scanf("%d %d", &n, &s);
for (int i = 0;i < n;++i) {
int adress, key, next;
scanf("%d %d %d", &adress, &key, &next);
node[adress].adr = adress;
node[adress].key = key;
node[adress].next = next;
}
int cnt = 0;
for (int i = s;i != -1;i = node[i].next) {
node[i].flag = 1;
++cnt;
}
if (cnt == 0) printf("0 -1");
else {
sort(node, node + maxn, cmp);
printf("%d %05d\n", cnt, node[0].adr);
for (int i = 0;i < cnt;++i) {
if (node[i + 1].flag == -1) {
printf("%05d %d -1\n", node[i].adr, node[i].key);
}
else printf("%05d %d %05d\n", node[i].adr, node[i].key, node[i + 1].adr);
}
}
return 0;
}
PAT A1074 Reversing Linked List
最开始没看清,以为只要调转前面的元素, 书上的输出操作稍显繁琐,我使用了reverse函数调转块,这样调转后每个块的后一块即为next,如果某一块的后一块的rank=maxn
时则说明已经遍历完,则输出-1;
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn = 100010;
struct Node {
int adr, data, next, rank;
Node() {
rank = 100010;
}
}node[maxn];
bool cmp(Node a, Node b) {
return a.rank < b.rank;
}
int main() {
int s, n, t;
scanf("%d %d %d", &s, &n, &t);
for (int i = 0;i < n;++i) {
int adress;
scanf("%d", &adress);
scanf("%d %d", &node[adress].data, &node[adress].next);
node[adress].adr = adress;
}
int cnt = 0;
for (int i = s;i != -1;i = node[i].next) {
node[i].rank = cnt++;
}
sort(node, node + maxn, cmp);
int time = cnt / t;//计算反转次数
for (int i = 0;i < time;++i) {
reverse(node + i * t, node + (i + 1) * t);
}
for (int i = 0;i < cnt;++i) {
if (node[i + 1].rank == 100010) {
printf("%05d %d -1\n", node[i].adr, node[i].data);
}
else {
printf("%05d %d %05d\n", node[i].adr, node[i].data, node[i + 1].adr);
}
}
return 0;
}
PAT A1097 Deduplication on a Linked List
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn = 100010;
struct Node {
int adr, data, next, rank;
bool flag;
Node() {
flag = false;//flag表示在链表上的是否是否保留;
rank = maxn;//rank表示保留和不保留的数的输出顺序;
}
}node[maxn];
bool cmp(Node a, Node b) {
if (a.flag != b.flag) return a.flag > b.flag;
else return a.rank < b.rank;
}
bool hashTable[maxn] = { false };
int main() {
int s, n;
scanf("%d %d", &s, &n);
for (int i = 0;i < n;++i) {
int adress;
scanf("%d", &adress);
node[adress].adr = adress;
scanf("%d %d", &node[adress].data, &node[adress].next);
}
int countsave = 0, countall = 0;//要用两个count来计数,因为存在在静态链表外的结点
for (int i = s;i != -1;i = node[i].next) {
node[i].rank = countall++;
int x = abs(node[i].data);
if (hashTable[x] == false) {
hashTable[x] = true;
node[i].flag = true;
++countsave;
}
}
sort(node, node + maxn, cmp);
for (int i = 0;i < countall;++i) {
if (i == countsave - 1) printf("%05d %d -1\n", node[i].adr, node[i].data);
else if (i == countall - 1) printf("%05d %d -1\n", node[i].adr, node[i].data);
else printf("%05d %d %05d\n", node[i].adr, node[i].data, node[i + 1].adr);
}
return 0;
}