Exercise 3.10 is not complete.

This commit is contained in:
juan 2022-09-22 13:58:49 +08:00
parent 75f158d6be
commit 4e0691c286
No known key found for this signature in database
GPG key ID: 5C1E5093C74F1DC7
4 changed files with 188 additions and 8 deletions

View file

@ -1,16 +1,12 @@
#include <math.h>
#include <stdio.h>
#include <time.h>
void printTime(clock_t end, clock_t start) {
printf("Took %lf to calculate.\n", ((double)(end - start)) / CLOCKS_PER_SEC);
}
void calculate(long long N) {
clock_t end, start;
start = clock();
for (int i = 1, root = (int)floor(sqrt((double)N)); i < root; i++) {
if (N % i == 0) {
end = clock();
@ -19,19 +15,16 @@ void calculate(long long N) {
return;
}
}
end = clock();
printTime(end, start);
printf("%lld is prime\n", N);
return;
}
int main(void) {
long long test[] = {1048575, 1099511627775};
for (int i = 0; i < 2; i++) {
calculate(test[i]);
printf("\n");
}
return 0;
}

View file

@ -5,7 +5,7 @@ int counter = 0;
void getTime(int N, clock_t start_t, time_t end_t) {
double runningTime;
runningTime = ((double) (end_t - start_t)) / CLOCKS_PER_SEC;
runningTime = ((double)(end_t - start_t)) / CLOCKS_PER_SEC;
printf("%d: Took %lf sec\n", counter++, runningTime);
}

View file

@ -0,0 +1,88 @@
#include <stdio.h>
#include <stdlib.h>
typedef struct ListNode {
int val;
struct ListNode *next;
} ListNode;
ListNode *appendNode(ListNode *node, int val) {
ListNode *new = malloc(sizeof(ListNode));
node->next = new;
new->next = NULL;
new->val = val;
return new;
}
void freeNode(ListNode *head) {
if (head != NULL) {
ListNode * ptr = head->next;
free(head);
freeNode(ptr);
}
}
void deleteNode(ListNode *node) {
// deletes node after this one.
ListNode* tmp = node->next;
node->next = node->next->next;
// printf("Deleted: %d\n", tmp->val);
tmp->next = NULL;
free(tmp);
}
void printNode(ListNode *head) {
ListNode *ptr = head;
while (ptr != NULL) {
printf("%d->", ptr->val);
ptr = ptr->next;
}
printf("NULL\n");
}
int main(void) {
// N people, M passes
int N, M;
scanf("%d%d", &M, &N);
// construct ring
ListNode *head = malloc(sizeof(ListNode));
ListNode *ptr = head;
for (int i = 1; i <= N; i++) {
ptr = appendNode(ptr, i);
}
printNode(head->next);
// enclose the ring
ptr->next = head->next;
// start simulation
ListNode** cur = &head;
ListNode** tmp;
int pass = 0;
while ((*cur) && (*cur)->next && (*cur)->next->next) {
// printf("Potato: %d\n", (*cur)->next->val);
if (pass == M) {
// kick this player, and continue.
tmp = cur;
deleteNode(*cur);
pass = 0;
} else {
cur = &(*cur)->next;
pass++;
}
}
printf("Remaining: %d\n", (*cur)->next->val);
free(head);
free(cur);
return 0;
}

View file

@ -0,0 +1,99 @@
#include <stdio.h>
#include <stdlib.h>
typedef struct ListNode {
int val;
struct ListNode *next;
} ListNode;
ListNode *appendNode(ListNode *node, int val) {
ListNode *new = malloc(sizeof(ListNode));
node->next = new;
new->next = NULL;
new->val = val;
return new;
}
void freeNode(ListNode *head) {
ListNode *ptr = head;
do {
head = ptr;
ptr = ptr->next;
free(head);
} while (ptr != NULL);
}
void printNode(ListNode *head) {
ListNode *ptr = head;
while (ptr != NULL) {
printf("%d->", ptr->val);
ptr = ptr->next;
}
printf("NULL\n");
}
ListNode *addList(ListNode *l, ListNode *r) {
ListNode *head = malloc(sizeof(ListNode));
head->val = l->val + r->val;
ListNode *ptr = head;
ListNode *ptrL = l->next;
ListNode *ptrR = r->next;
while (ptrL && ptrR) {
ptr = appendNode(ptr, ptrL->val + ptrR->val);
ptrL = ptrL->next;
ptrR = ptrR->next;
}
while (ptrL) {
ptr = appendNode(ptr, ptrL->val);
ptrL = ptrL->next;
}
while (ptrR) {
ptr = appendNode(ptr, ptrR->val);
ptrR = ptrR->next;
}
return head;
}
int main(void) {
ListNode *l = malloc(sizeof(ListNode));
ListNode *r = malloc(sizeof(ListNode));
ListNode *ptrL = l;
ListNode *ptrR = r;
// input
int lLen, rLen;
scanf("%d", &lLen);
scanf("%d", &rLen);
int temp;
for (int i = 0; i < lLen; i++) {
scanf("%d", &temp);
ptrL = appendNode(ptrL, temp);
}
printNode(l->next);
for (int i = 0; i < rLen; i++) {
scanf("%d", &temp);
ptrR = appendNode(ptrR, temp);
}
printNode(r->next);
// add
ListNode *ans = addList(l->next, r->next);
printNode(ans);
freeNode(l);
freeNode(r);
freeNode(ans);
return 0;
}