Add pset0

This commit is contained in:
juan 2022-10-08 09:33:48 +08:00
parent 7adbfbf396
commit d2f2268cf2
No known key found for this signature in database
GPG key ID: 5C1E5093C74F1DC7
4 changed files with 310 additions and 0 deletions

View file

@ -0,0 +1,71 @@
#include <cstdio>
#include <iostream>
#include <pthread.h>
#include <stack>
#include <string>
#include <vector>
char getParen(const char &ch) {
if (ch == ']') {
return '[';
} else if (ch == ')') {
return '(';
} else {
return '{';
}
}
int main(void) {
std::string input;
std::cin >> input;
std::stack<char> st;
bool ans[4] = {false, false, false, false};
for (char ch : input) {
// scan for errors
// () [] {}
if (ch == '(' || ch == '[' || ch == '{') {
st.push(ch);
} else if (ch == ')' || ch == ']' || ch == '}') {
// check
if (!st.empty() && st.top() == getParen(ch)) {
// pop, nothing wrong
st.pop();
} else {
if (ch == ')') {
ans[1] = true;
} else if (ch == ']') {
ans[2] = true;
} else {
ans[3] = true;
}
ans[0] = true;
}
}
}
while (!st.empty()) {
if (st.top() == '(') {
ans[1] = true;
} else if (st.top() == '[') {
ans[2] = true;
} else {
ans[3] = true;
}
ans[0] = true;
st.pop();
}
if (ans[0] == false) {
printf("0");
} else {
for (int i = 1; i < 4; i++) {
if (ans[i] == true) {
printf("%d,", i);
}
}
}
return 0;
}

View file

@ -0,0 +1,92 @@
// Convert the infix expression to postfix expression.
#include <cctype>
#include <cstdio>
#include <iostream>
#include <stack>
#include <string>
using namespace std;
int prec(char ch) {
if (ch == '+' || ch == '-') {
return 0;
} else if (ch == '*' || ch == '/') {
return 1;
} else {
return -1;
}
}
void printEval(string &postfix) {
stack<double> st;
double l, r;
double ans;
for (char ch : postfix) {
if (isdigit(ch)) {
st.push(ch - '0');
} else {
r = st.top();
st.pop();
l = st.top();
st.pop();
if (ch == '+') {
ans = l + r;
} else if (ch == '-') {
ans = l - r;
} else if (ch == '*') {
ans = l * r;
} else if (ch == '/') {
ans = l / r;
}
st.push(ans);
}
}
printf("%.2lf\n", st.top());
}
void printFix(string &postfix) {
for (char ch : postfix) {
printf("%c ", ch);
}
}
int main(void) {
string input;
cin >> input;
stack<char> st;
string postfix;
for (char ch : input) {
if (isdigit(ch)) {
postfix.push_back(ch);
} else {
// if is not a digit
if (ch == '(') {
st.push(ch);
} else if (ch == ')') {
while (st.top() != '(') {
postfix.push_back(st.top());
st.pop();
}
st.pop();
} else {
// a operand
while (!st.empty() && prec(ch) <= prec(st.top())) {
postfix.push_back(st.top());
st.pop();
}
st.push(ch);
}
}
}
while (!st.empty()) {
postfix.push_back(st.top());
st.pop();
}
printEval(postfix);
printFix(postfix);
return 0;
}

View file

@ -0,0 +1,63 @@
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
typedef struct stack {
int val;
struct stack *next;
} stack;
stack *push(stack *st, int val) {
stack *new = malloc(sizeof(stack));
new->next = st;
new->val = val;
return new;
}
int isEmpty(stack *st) { return st == NULL; }
int peek(stack *st) { return st->val; }
stack *pop(stack *st) {
stack *tmp = st->next;
free(st);
return tmp;
}
int main(void) {
stack *st = NULL;
int size = 0;
int in;
int last;
int lastUsed = 0;
int centFound = 0;
while (scanf("%d,", &in) != EOF) {
size++;
if (!isEmpty(st) && peek(st) == in) {
st = pop(st);
} else if (lastUsed == 1 && last == in) {
st = pop(st);
st = pop(st);
centFound = 1;
} else {
if (!isEmpty(st)) {
last = peek(st);
lastUsed = 1;
}
// printf("PUSHING: %d\n", in);
st = push(st, in);
}
}
if (size > 0 && isEmpty(st) && centFound == 1) {
printf("Yes");
} else if (size == 1) {
printf("Yes");
} else {
printf("No");
}
return 0;
}

View file

@ -0,0 +1,84 @@
#include <stdio.h>
#include <stdlib.h>
typedef struct listNode {
int id;
struct listNode *next;
int out;
} listNode;
listNode *appendNode(int id, listNode *head) {
listNode *new = malloc(sizeof(listNode));
new->id = id;
new->next = NULL;
new->out = 0;
head->next = new;
return new;
}
listNode *makecircle(const int N) {
listNode *head = malloc(sizeof(listNode));
head->id = 1;
head->next = NULL;
head->out = 0;
listNode *ptr = head;
for (int i = 2; i <= N; i++) {
ptr = appendNode(i, ptr);
// printf("appending: %d\n", i);
}
ptr->next = head;
return head;
}
void printCircle(listNode *head) {
if (head->out == 0) {
printf("%d->", head->id);
}
head = head->next;
while (head->id != 1) {
if (head->out == 0) {
printf("%d->", head->id);
}
head = head->next;
}
printf("|");
}
int main(void) {
int N, M;
scanf("%d,%d", &N, &M);
listNode *head = makecircle(N);
// printCircle(head);
int count = N;
int step = 0;
listNode *ptr = head;
while (count != 0) {
while (step != M) {
while (ptr->out == 1) {
ptr = ptr->next;
}
ptr = ptr->next;
step++;
}
step = 0;
while (ptr->out == 1) {
ptr = ptr->next;
}
ptr->out = 1;
count--;
if (count != 0) {
printf("%d,", ptr->id);
}
}
printf("%d", ptr->id);
return 0;
}