basic a2
This commit is contained in:
82
a2-Vollgeladen/src/main.c
Normal file
82
a2-Vollgeladen/src/main.c
Normal file
@ -0,0 +1,82 @@
|
||||
#include <stdio.h>
|
||||
#include <acl/file.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <inttypes.h>
|
||||
|
||||
#define panicio {\
|
||||
perror(*filepath);\
|
||||
return 1;\
|
||||
}
|
||||
#define getNumber strtol(nextnumber + 1, &nextnumber, 10)
|
||||
|
||||
struct hotel {
|
||||
uint_least16_t minutes;
|
||||
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;
|
||||
{
|
||||
FILE *inputFile = fopen(*filepath, "rb");
|
||||
if(inputFile == NULL) panicio;
|
||||
bool success;
|
||||
inputText = acl_ReadTextFile(inputFile, &success);
|
||||
if(!success) panicio;
|
||||
fclose(inputFile);
|
||||
}
|
||||
{
|
||||
char *nextnumber;
|
||||
numberHotels = strtol(inputText, &nextnumber, 10);
|
||||
totalMinutes = getNumber;
|
||||
hotels = malloc(numberHotels * sizeof *hotels + numberHotels * sizeof *ratings);
|
||||
ratings = (uint_least8_t*)(hotels + numberHotels);
|
||||
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];
|
||||
}
|
||||
}
|
||||
free(inputText);
|
||||
}
|
||||
qsort(ratings, numberHotels, sizeof *ratings, compareReverse);
|
||||
{
|
||||
struct hotel hotelRoute[4];
|
||||
uint_least8_t stoppedHotels;
|
||||
{
|
||||
uint_least8_t *rating = ratings;
|
||||
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;
|
||||
hotelRoute[stoppedHotels] = *possibleStop;
|
||||
lastHotelMinutes = possibleStop->minutes;
|
||||
currentHotel = possibleStop;
|
||||
++stoppedHotels;
|
||||
}
|
||||
if(currentHotel->rating >= *rating) possibleStop = currentHotel;
|
||||
}
|
||||
++rating;
|
||||
} while(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);
|
||||
}
|
||||
}
|
||||
free(hotels);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user