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;
};
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);