feat: Initial commit
This commit is contained in:
56
Laboratorio 3/Esercizio 1/main.c
Normal file
56
Laboratorio 3/Esercizio 1/main.c
Normal file
@@ -0,0 +1,56 @@
|
||||
// Laboratorio 3 - Esercizio 1
|
||||
// Matteo Schiff - s295565
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
int majority(int * a, int N) {
|
||||
if (N == 1) {
|
||||
return a[0];
|
||||
}
|
||||
|
||||
int m = N/2;
|
||||
|
||||
int x = majority(a, m);
|
||||
int y = majority(a+m, m);
|
||||
|
||||
if (x == y)
|
||||
return x;
|
||||
|
||||
if (x != -1) {
|
||||
int c = 0;
|
||||
for (int i = 0; i < N; i++) {
|
||||
if (a[i] == x)
|
||||
c++;
|
||||
}
|
||||
if (c > (N/2))
|
||||
return x;
|
||||
}
|
||||
|
||||
if (y != -1) {
|
||||
int c = 0;
|
||||
for (int i = 0; i < N; i++) {
|
||||
if (a[i] == y)
|
||||
c++;
|
||||
}
|
||||
|
||||
if (c > (N/2))
|
||||
return y;
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
int main(int argc, char ** argv) {
|
||||
int N = argc - 1;
|
||||
|
||||
if (argc < 2)
|
||||
return 1;
|
||||
|
||||
int * vec = malloc(N * sizeof(int));
|
||||
for (int i = 0; i < N; i++) {
|
||||
vec[i] = atoi(argv[i+1]);
|
||||
}
|
||||
|
||||
printf("major: %d\n", majority(vec, N));
|
||||
}
|
||||
26
Laboratorio 3/Esercizio 2/brani.txt
Normal file
26
Laboratorio 3/Esercizio 2/brani.txt
Normal file
@@ -0,0 +1,26 @@
|
||||
5
|
||||
5
|
||||
DivideAndConquer
|
||||
MementoMori
|
||||
LegacyOfHate_Pt.III
|
||||
GhostLoveScore
|
||||
TheHowling
|
||||
3
|
||||
BirdSetFree
|
||||
ThePrayer
|
||||
SomeoneLikeYou
|
||||
4
|
||||
Yellow
|
||||
Wonderwall
|
||||
MessageInABottle
|
||||
FreeBird
|
||||
5
|
||||
GliAngeli
|
||||
HoMessoVia
|
||||
GliAnni
|
||||
ReginaDiCuori
|
||||
Pescatore
|
||||
3
|
||||
ISeeFire
|
||||
ChasingCars
|
||||
HowToSaveALife
|
||||
87
Laboratorio 3/Esercizio 2/main.c
Normal file
87
Laboratorio 3/Esercizio 2/main.c
Normal file
@@ -0,0 +1,87 @@
|
||||
// Laboratorio 3 - Esercizio 2
|
||||
// Matteo Schiff - s295565
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#define MAX_LEN 255
|
||||
|
||||
typedef struct Lista
|
||||
{
|
||||
unsigned int N;
|
||||
char *canzoni;
|
||||
} lista;
|
||||
|
||||
lista *leggi_brani(int *N, int **playlist)
|
||||
{
|
||||
FILE *fp_read;
|
||||
|
||||
if ((fp_read = fopen("brani.txt", "r")) == NULL)
|
||||
{
|
||||
puts("Impossibile aprire il file");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
fscanf(fp_read, "%u \n", N);
|
||||
|
||||
lista *raccolta = malloc(*N * sizeof(lista));
|
||||
*playlist = malloc(*N * sizeof(int));
|
||||
|
||||
for (int i = 0; i < *N; i++)
|
||||
{
|
||||
fscanf(fp_read, "%u \n", &(raccolta[i].N));
|
||||
raccolta[i].canzoni = malloc(raccolta[i].N * MAX_LEN * sizeof(char));
|
||||
for (int j = 0; j < raccolta[i].N; j++)
|
||||
{
|
||||
fscanf(fp_read, "%s ", raccolta[i].canzoni + j * MAX_LEN);
|
||||
}
|
||||
}
|
||||
|
||||
fclose(fp_read);
|
||||
return raccolta;
|
||||
}
|
||||
|
||||
void free_brani(lista *mat, int N, int *playlist)
|
||||
{
|
||||
for (int i = 0; i < N; i++)
|
||||
{
|
||||
free(mat[i].canzoni);
|
||||
}
|
||||
|
||||
free(mat);
|
||||
free(playlist);
|
||||
}
|
||||
|
||||
void stampa_playlist(lista *mat, int *playlist, int N, int depth)
|
||||
{
|
||||
if (N == depth)
|
||||
{
|
||||
puts("\nPlaylist:");
|
||||
for (int i = 0; i < N; i++)
|
||||
{
|
||||
printf(" - %s\n", (mat[i].canzoni + playlist[i] * MAX_LEN));
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
for (int i = 0; i < mat[depth].N; i++)
|
||||
{
|
||||
playlist[depth] = i;
|
||||
stampa_playlist(mat, playlist, N, depth + 1);
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
int N;
|
||||
int *playlist;
|
||||
lista *mat = leggi_brani(&N, &playlist);
|
||||
|
||||
if (mat == NULL)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
stampa_playlist(mat, playlist, N, 0);
|
||||
free_brani(mat, N, playlist);
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user