Code a2 fertig

This commit is contained in:
2021-10-12 17:25:00 +02:00
parent bd4e76b246
commit 4782708650

View File

@ -15,16 +15,11 @@ struct hotel {
uint_least8_t rating; 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[]) { int main(int argc, char *argv[]) {
for(char **filepath = argv + 1; filepath - argv < argc; ++filepath) { for(char **filepath = argv + 1; filepath - argv < argc; ++filepath) {
printf("%s:\n", *filepath); printf("%s:\n", *filepath);
uint_least16_t numberHotels, totalMinutes; uint_least16_t numberHotels, totalMinutes;
struct hotel *hotels; struct hotel *hotels;
uint_least8_t *ratings;
{ {
char *inputText; char *inputText;
{ {
@ -37,44 +32,42 @@ int main(int argc, char *argv[]) {
} }
{ {
char *nextnumber; char *nextnumber;
numberHotels = strtol(inputText, &nextnumber, 10); numberHotels = strtol(inputText, &nextnumber, 10) + 1;
totalMinutes = getNumber; totalMinutes = getNumber;
hotels = malloc(numberHotels * sizeof *hotels + numberHotels * sizeof *ratings); hotels = malloc(numberHotels * sizeof *hotels);
ratings = (uint_least8_t*)(hotels + numberHotels);
for(uint_least16_t i = 0; i < numberHotels; ++i) { for(uint_least16_t i = 0; i < numberHotels; ++i) {
hotels[i].minutes = getNumber; hotels[i].minutes = getNumber;
ratings[i] = 10 * getNumber; hotels[i].rating = 10 * getNumber;
ratings[i] += getNumber; hotels[i].rating += getNumber;
hotels[i].rating = ratings[i];
} }
hotels[numberHotels - 1] = (struct hotel){ .minutes = totalMinutes, .rating = 50};
} }
free(inputText); free(inputText);
} }
qsort(ratings, numberHotels, sizeof *ratings, compareReverse);
{ {
struct hotel hotelRoute[4]; struct hotel hotelRoute[4];
uint_least8_t stoppedHotels; uint_least8_t stoppedHotels;
{ {
uint_least8_t *rating = ratings; uint_least8_t rating = 50;
do { do {
uint_least16_t lastHotelMinutes = 0; uint_least16_t lastHotelMinutes = 0;
struct hotel *possibleStop = NULL; struct hotel *possibleStop = NULL;
stoppedHotels = 0; stoppedHotels = 0;
for(struct hotel *currentHotel = hotels; currentHotel - hotels < numberHotels; ++currentHotel) { for(struct hotel *currentHotel = hotels; currentHotel - hotels < numberHotels; ++currentHotel) {
if(currentHotel->minutes - lastHotelMinutes > 360 && possibleStop != NULL) { if(currentHotel->minutes - lastHotelMinutes > 360) {
if(stoppedHotels >= 4) break; if(possibleStop == NULL || stoppedHotels >= 4) break;
hotelRoute[stoppedHotels] = *possibleStop; hotelRoute[stoppedHotels] = *possibleStop;
lastHotelMinutes = possibleStop->minutes; lastHotelMinutes = possibleStop->minutes;
currentHotel = possibleStop; currentHotel = possibleStop;
++stoppedHotels; ++stoppedHotels;
} }
if(currentHotel->rating >= *rating) possibleStop = currentHotel; if(currentHotel->rating >= rating) possibleStop = currentHotel;
} }
++rating; --rating;
} while(totalMinutes - hotelRoute[stoppedHotels - 1].minutes > 360); } while(rating && (!stoppedHotels || totalMinutes - hotelRoute[stoppedHotels - 1].minutes > 360));
} }
for(struct hotel *hotelPrint = hotelRoute; hotelPrint - hotelRoute < stoppedHotels; ++hotelPrint) { 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); free(hotels);