108 lines
2.1 KiB
C
108 lines
2.1 KiB
C
#include "_.h"
|
|
#include <pthread.h>
|
|
|
|
typedef struct structure {
|
|
int nbr;
|
|
struct structure *next;
|
|
} struc;
|
|
|
|
typedef struct data_t {
|
|
long nbr;
|
|
int max;
|
|
} data;
|
|
|
|
long long lenght_struc(struc *head) {
|
|
long long count = 0;
|
|
while (head->next != NULL) {
|
|
head = head->next;
|
|
count++;
|
|
}
|
|
return count;
|
|
}
|
|
|
|
void print_struc(struc *head) {
|
|
while (head->next != NULL) {
|
|
head = head->next;
|
|
printf("%d ", head->nbr);
|
|
}
|
|
}
|
|
|
|
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) {
|
|
|
|
if (argc != 2)
|
|
return 1;
|
|
|
|
char *input = argv[1];
|
|
char **inputs = str_split(strdup(input), ' ');
|
|
struc *head = calloc(1, sizeof(struc));
|
|
struc *cur = head;
|
|
|
|
for (int i = 0; inputs[i] != NULL; i++) {
|
|
cur->nbr = atol(inputs[i]);
|
|
cur->next = calloc(1, sizeof(struc));
|
|
cur = cur->next;
|
|
}
|
|
|
|
|
|
//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;
|
|
}
|