Add lazy versions of pset1

This commit is contained in:
juan 2022-11-20 21:22:03 +08:00
parent cda2959974
commit cd77744a06
No known key found for this signature in database
GPG key ID: 5C1E5093C74F1DC7
3 changed files with 172 additions and 12 deletions

View file

@ -1,26 +1,122 @@
// 20:05 - 20:50
#include <stdio.h>
#define SIZE 100
#include <stdlib.h>
#define MAX 256
int getPriority(char ch) {
if (ch == '+' || ch == '-') {
return 0;
} else if (ch == '*' || ch == '/') {
return 1;
} else {
return -1;
}
}
typedef struct stack {
double *arr;
int top;
int capacity;
int* array;
} stack;
void push(stack* st, int val) {
st->array[++st->top] = val;
stack *newStack() {
stack *st = malloc(sizeof(stack));
st->top = 0;
st->arr = malloc(sizeof(double) * MAX);
return st;
}
void pop(stack* st) {
st->top--;
void push(stack *st, double val) {
// printf("Pushing: %.2lf\n", val);
st->arr[st->top++] = val;
}
int peek(stack* st) {
return st->array[st->top];
double top(stack *st) { return st->arr[st->top - 1]; }
void pop(stack *st) { st->top--; }
int isEmpty(stack *st) { return (st->top == 0); }
void toPostfix(char *buf, char *out) {
int count = 0;
stack *st = newStack();
for (int i = 0; buf[i] != '\0'; i++) {
if (buf[i] >= '0' && buf[i] <= '9') {
// is digit, push to out array
out[count++] = buf[i];
} else {
if (buf[i] == '(') {
// left paren, don't pop elements in stack.
push(st, buf[i]);
} else if (buf[i] == ')') {
// push everything after the (
while (top(st) != '(') {
out[count++] = top(st);
pop(st);
}
pop(st);
} else {
// the equal sign is important
while (!isEmpty(st) && getPriority(top(st)) >= getPriority(buf[i])) {
out[count++] = top(st);
pop(st);
}
push(st, buf[i]);
}
}
}
while (!isEmpty(st)) {
out[count++] = top(st);
pop(st);
}
out[count] = '\0';
}
void calculate(char *postfix) {
double l, r;
stack *st = newStack();
for (int i = 0; postfix[i] != '\0'; i++) {
if (postfix[i] >= '0' && postfix[i] <= '9') {
// remember convert from char to int
push(st, postfix[i] - '0');
} else {
r = top(st);
pop(st);
l = top(st);
pop(st);
if (postfix[i] == '+') {
push(st, l + r);
} else if (postfix[i] == '-') {
push(st, l - r);
} else if (postfix[i] == '*') {
push(st, l * r);
} else if (postfix[i] == '/') {
push(st, l / r);
} else {
printf("ERROR: %c is not recognized\n", postfix[i]);
}
}
}
printf("%.2lf\n", top(st));
}
void printPostfix(char *postfix) {
for (int i = 0; postfix[i] != '\0'; i++) {
printf("%c ", postfix[i]);
}
}
int main(void) {
char input[SIZE];
fscanf(input, SIZE, stdin);
char buf[MAX];
char out[MAX];
scanf("%s", buf);
toPostfix(buf, out);
calculate(out);
printPostfix(out);
return 0;
}

View file

@ -0,0 +1,22 @@
// 20:50 - 21:00
#include <stdio.h>
#define MAX 100
int main(void) {
int arr[MAX];
int counter = 0;
while (scanf("%d,", &arr[counter]) != EOF) {
counter++;
}
for (int i = 0, end = counter / 2; i < end; i++) {
if (arr[i] != arr[counter - i - 1]) {
printf("No");
return 0;
}
}
printf("Yes");
return 0;
}

View file

@ -0,0 +1,42 @@
// 21:01 - 21:21
#include <stdio.h>
int step(int cur, int *dead, int N, int M) {
for (int i = 0; i < M; i++) {
cur = (cur + 1) % N;
while (dead[cur] == 1) {
cur = (cur + 1) % N;
}
}
return cur;
}
int main(void) {
int N, M;
scanf("%d,%d", &N, &M);
int deadCount = 0;
int dead[N];
for (int i = 0; i < N; i++) {
dead[i] = 0;
}
int cur = 0;
while (1) {
cur = step(cur, dead, N, M);
dead[cur] = 1;
deadCount++;
if (deadCount == N) {
printf("%d", cur + 1);
break;
} else {
printf("%d,", cur + 1);
}
cur = step(cur, dead, N, 1);
}
return 0;
}