From 4782708650d8a71e5fc082891d72877ad5e0807a Mon Sep 17 00:00:00 2001 From: MrGeorgen Date: Tue, 12 Oct 2021 17:25:00 +0200 Subject: [PATCH] Code a2 fertig --- a2-Vollgeladen/src/main.c | 31 ++++++++++++------------------- 1 file changed, 12 insertions(+), 19 deletions(-) diff --git a/a2-Vollgeladen/src/main.c b/a2-Vollgeladen/src/main.c index ab22201..3becd91 100644 --- a/a2-Vollgeladen/src/main.c +++ b/a2-Vollgeladen/src/main.c @@ -15,16 +15,11 @@ struct hotel { uint_least8_t rating; }; -int compareReverse(const void *a, const void *b) { - return (uint_least8_t*)a - (uint_least8_t*)b; -} - int main(int argc, char *argv[]) { for(char **filepath = argv + 1; filepath - argv < argc; ++filepath) { printf("%s:\n", *filepath); uint_least16_t numberHotels, totalMinutes; struct hotel *hotels; - uint_least8_t *ratings; { char *inputText; { @@ -37,44 +32,42 @@ int main(int argc, char *argv[]) { } { char *nextnumber; - numberHotels = strtol(inputText, &nextnumber, 10); + numberHotels = strtol(inputText, &nextnumber, 10) + 1; totalMinutes = getNumber; - hotels = malloc(numberHotels * sizeof *hotels + numberHotels * sizeof *ratings); - ratings = (uint_least8_t*)(hotels + numberHotels); + hotels = malloc(numberHotels * sizeof *hotels); for(uint_least16_t i = 0; i < numberHotels; ++i) { hotels[i].minutes = getNumber; - ratings[i] = 10 * getNumber; - ratings[i] += getNumber; - hotels[i].rating = ratings[i]; + hotels[i].rating = 10 * getNumber; + hotels[i].rating += getNumber; } + hotels[numberHotels - 1] = (struct hotel){ .minutes = totalMinutes, .rating = 50}; } free(inputText); } - qsort(ratings, numberHotels, sizeof *ratings, compareReverse); { struct hotel hotelRoute[4]; uint_least8_t stoppedHotels; { - uint_least8_t *rating = ratings; + uint_least8_t rating = 50; do { uint_least16_t lastHotelMinutes = 0; struct hotel *possibleStop = NULL; stoppedHotels = 0; for(struct hotel *currentHotel = hotels; currentHotel - hotels < numberHotels; ++currentHotel) { - if(currentHotel->minutes - lastHotelMinutes > 360 && possibleStop != NULL) { - if(stoppedHotels >= 4) break; + if(currentHotel->minutes - lastHotelMinutes > 360) { + if(possibleStop == NULL || stoppedHotels >= 4) break; hotelRoute[stoppedHotels] = *possibleStop; lastHotelMinutes = possibleStop->minutes; currentHotel = possibleStop; ++stoppedHotels; } - if(currentHotel->rating >= *rating) possibleStop = currentHotel; + if(currentHotel->rating >= rating) possibleStop = currentHotel; } - ++rating; - } while(totalMinutes - hotelRoute[stoppedHotels - 1].minutes > 360); + --rating; + } while(rating && (!stoppedHotels || totalMinutes - hotelRoute[stoppedHotels - 1].minutes > 360)); } for(struct hotel *hotelPrint = hotelRoute; hotelPrint - hotelRoute < stoppedHotels; ++hotelPrint) { - printf("Hotel: %" PRIuLEAST16 " m, rating: %" PRIuLEAST8 "\n", hotelPrint->minutes, hotelPrint->rating); + printf("Entfernung vom Start: %" PRIuLEAST16 " Minuten, Bewertung: %" PRIuLEAST8 "\n", hotelPrint->minutes, hotelPrint->rating); } } free(hotels);