alexander437 Δημοσ. 31 Ιανουαρίου 2012 Δημοσ. 31 Ιανουαρίου 2012 > #include <stdio.h> #include <stdlib.h> struct fibonacci { int key; long value; struct fibonacci *next; }; //prosthetei ena stoixeio stin arxi tis listas kai epistrefei ti nea arxi tis listas struct fibonacci *insert(struct fibonacci *head, int key, long value) { struct fibonacci *new = malloc (sizeof(struct fibonacci)); new->key = key; new->value = value; new->next = head; //to epomeno stoixeio na einai i arxi tis listas head = new; //i arxi tis listas na einai twra to stoixeio pou molis eisagame return head; //epistrefei tin arxi tis neas listas (i palia + to neo stoixeio) } //epistrefei -1 an to key den yparxei sti lista, i to fibonacci tou key, an to key yparxei sti lista long get(struct fibonacci *head, int key) { struct fibonacci *tmp = head; if (head == NULL) // i lista einai keni return -1; else { while (tmp != NULL) { if (tmp->key == key) //o arithmos key yparxei sti lista return tmp->value; tmp = tmp->next; //to tmp na einai to epomeno stoixeio } } return -1; //diavasa oli ti lista kai den vrika to key } //ektypwnei oli ti lista void toString(struct fibonacci *head) { struct fibonacci *tmp = head; if (!head) printf("The list is empty!\n"); else { printf("\nHere is the list [key-->fib(key)]:\n"); while (tmp) { printf("%3d-->%lu\n",tmp->key, tmp->value); tmp = tmp->next; } } } //anadromiki synartisi, termatizei otan input einai 2 `h < 2. int fib(int input, struct fibonacci *head) { long cached; if (input == 2) { return 1; } else if (input < 2) { return 0; } else if ((cached = get(head, input)) > 0){ //an cached > 0 tote exoume ksanaypologisei to fib tou input return cached; } else { return (fib(input-1, head) + fib (input-2, head)); //kalese anadromika ti fib me input-1 kai input-2 } } int main (void) { struct fibonacci *head = NULL; int i; int r; long cached; long result; for (i = 0; i < 1000; i++) { if (i < 20) r = (rand()% 32)+10; else r = rand()%83; if ((cached = get(head, r)) > 0) { //an cached > 0 tote exoume ksanaypologisei to fib tou r printf("fib(%3d):%lu (cached)\n", r, cached); } else { result = fib(r, head); //einai pio grigoro na ypologisw to fib(2) apo to na to psaksw se mia megali lista if (r > 2) head = insert(head, r, result); //apothikeyse sti lista to r kai to fib(r) printf("fib(%3d):%lu\n", r, result); } } toString(head); } Γεια σας εχω αυτο το προγραμμα και τρεχει μια χαρα, αλλα η κονσολα οταν το τρεχω δεν μου εμφανιζει ολα τα αποτελεσματα. Δηλαδη σαν να εχει καποιο οριο πχ 1000 γραμμες και οταν παει παραπανω σβηνει τις πρωτες γραμμες. Υπαρχει καποιος τροπος να το ρυθμισεις αυτο ; Ευχαριστω.
Προτεινόμενες αναρτήσεις
Δημιουργήστε ένα λογαριασμό ή συνδεθείτε για να σχολιάσετε
Πρέπει να είστε μέλος για να αφήσετε σχόλιο
Δημιουργία λογαριασμού
Εγγραφείτε με νέο λογαριασμό στην κοινότητα μας. Είναι πανεύκολο!
Δημιουργία νέου λογαριασμούΣύνδεση
Έχετε ήδη λογαριασμό; Συνδεθείτε εδώ.
Συνδεθείτε τώρα