trying
This commit is contained in:
parent
83beca8152
commit
dcbface9ba
109
11/part2.c
109
11/part2.c
|
@ -1,10 +1,16 @@
|
||||||
#include "_.h"
|
#include "_.h"
|
||||||
|
#include <pthread.h>
|
||||||
|
|
||||||
typedef struct structure {
|
typedef struct structure {
|
||||||
int nbr;
|
int nbr;
|
||||||
struct structure *next;
|
struct structure *next;
|
||||||
} struc;
|
} struc;
|
||||||
|
|
||||||
|
typedef struct data_t {
|
||||||
|
long nbr;
|
||||||
|
int max;
|
||||||
|
} data;
|
||||||
|
|
||||||
long long lenght_struc(struc *head) {
|
long long lenght_struc(struc *head) {
|
||||||
long long count = 0;
|
long long count = 0;
|
||||||
while (head->next != NULL) {
|
while (head->next != NULL) {
|
||||||
|
@ -21,6 +27,47 @@ void print_struc(struc *head) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void* thread(void *data_void);
|
||||||
|
|
||||||
|
pthread_t create_thread(long nbr, int max) {
|
||||||
|
pthread_t thread_id;
|
||||||
|
data dt = {nbr, max};
|
||||||
|
pthread_create(&thread_id, NULL, thread, &dt);
|
||||||
|
pthread_join(thread_id, NULL);
|
||||||
|
|
||||||
|
return thread_id;
|
||||||
|
}
|
||||||
|
|
||||||
|
long count = 0;
|
||||||
|
pthread_mutex_t mutex;
|
||||||
|
|
||||||
|
void blink(long nbr, int max) {
|
||||||
|
for (; max > 0; max--) {
|
||||||
|
if (nbr == 0) {
|
||||||
|
nbr = 1;
|
||||||
|
}
|
||||||
|
else if (((int)floor(log10(ABS(nbr))) + 1) % 2 == 0) {
|
||||||
|
double len = floor(log10(ABS(nbr))) + 1;
|
||||||
|
int left = nbr / (pow(10, len / 2));
|
||||||
|
nbr = nbr % (long)(pow(10, len / 2));
|
||||||
|
|
||||||
|
create_thread(left, max - 1);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
nbr *= 2024;
|
||||||
|
}
|
||||||
|
pthread_mutex_lock(&mutex);
|
||||||
|
count++;
|
||||||
|
pthread_mutex_unlock(&mutex);
|
||||||
|
}
|
||||||
|
|
||||||
|
void* thread(void *data_void) {
|
||||||
|
data *dt = (data*)data_void;
|
||||||
|
blink(dt->nbr, dt->max);
|
||||||
|
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
int main(int argc, char **argv) {
|
int main(int argc, char **argv) {
|
||||||
|
|
||||||
if (argc != 2)
|
if (argc != 2)
|
||||||
|
@ -37,52 +84,24 @@ int main(int argc, char **argv) {
|
||||||
cur = cur->next;
|
cur = cur->next;
|
||||||
}
|
}
|
||||||
|
|
||||||
long long count = lenght_struc(head);
|
|
||||||
long long pre_count = count;
|
|
||||||
long long estimate = count;
|
|
||||||
double average = 0;
|
|
||||||
int max = 40;
|
|
||||||
for (int i = 0; i < max; i++) {
|
|
||||||
cur = head;
|
|
||||||
for (int j = 0; cur->next != NULL; j++) {
|
|
||||||
if (cur->nbr == 0) {
|
|
||||||
cur->nbr = 1;
|
|
||||||
cur = cur->next;
|
|
||||||
}
|
|
||||||
else if (((int)floor(log10(ABS(cur->nbr))) + 1) % 2 == 0) {
|
|
||||||
double len = floor(log10(ABS(cur->nbr))) + 1;
|
|
||||||
int left = cur->nbr / (pow(10, len / 2));
|
|
||||||
int right = cur->nbr % (int)(pow(10, len / 2));
|
|
||||||
|
|
||||||
struc *new = calloc(1, sizeof(struc));
|
|
||||||
new->nbr = right;
|
|
||||||
new->next = cur->next;
|
|
||||||
|
|
||||||
cur->nbr = left;
|
|
||||||
cur->next = new;
|
|
||||||
cur = new->next;
|
|
||||||
|
|
||||||
count++;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
cur->nbr *= 2024;
|
|
||||||
cur = cur->next;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
//printf("count - pre_count: %f\n", (double)count / pre_count);
|
|
||||||
average += (double)count / pre_count;
|
|
||||||
pre_count = count;
|
|
||||||
}
|
|
||||||
average /= (double)max;
|
|
||||||
printf("average: %f\n", average);
|
|
||||||
|
|
||||||
for (int i = 0; i < 75; i++)
|
|
||||||
estimate *= average;
|
|
||||||
|
|
||||||
//print_struc(head);
|
|
||||||
printf("count: %lld\n", count);
|
|
||||||
printf("estimate: %lld\n", estimate);
|
|
||||||
|
|
||||||
|
//long len = lenght_struc(head);
|
||||||
|
//pthread_t thread_id[len + 1];
|
||||||
|
|
||||||
|
pthread_mutex_init(&mutex, NULL);
|
||||||
|
|
||||||
|
cur = head;
|
||||||
|
for (int i = 0; cur->next != NULL; i++) {
|
||||||
|
create_thread(cur->nbr, 75);
|
||||||
|
cur = cur->next;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//for (int i = 0; i < len; i++) {
|
||||||
|
//pthread_join(thread_id[i], NULL);
|
||||||
|
//}
|
||||||
|
|
||||||
|
printf("count: %ld\n", count);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue