Fondamenti di Programmazione a.a. 2011-2012
Soluzioni prova scritta - 25 gennaio 2012
int isqunif(float A[], int i, int j, float d) {
float ave = 0;
for (int k = i ; k <= j ; k++)
ave += A[k];
ave /= (j - i + 1);
for (int k = i ; k <= j ; k++)
if (A[k] - ave > d || A[k] - ave < -d) return 0;
return 1;
}
int qunif(float A[], int n, float d) {
int max = 0;
for (int i = 0 ; i < n ; i++)
for (int j = i ; j < n ; j++)
if (j - i + 1 > max && isqunif(A, i, j, d))
max = j - i + 1;
return max;
}
int blkcnt(char *s, char start, char end) {
int count = 0, started = 0;
for (int i = 0 ; s[i] != '\0' ; i++) {
if (s[i] == start)
started = 1;
if (s[i] == end) {
if (started)
count++;
started = 0;
}
}
return count;
}
#include <stdlib.h>
int **zerosum(int n, int M[n][n], int *size) {
int max = 0, rmax, cmax;
for (int r = 0 ; r < n ; r++) {
for (int c = 0 ; c < n ; c++) {
int k = (r > c ? n - r : n - c);
while (k > max) {
int s = 0;
for (int i = 0 ; i < k ; i++)
for (int j = 0 ; j < k ; j++)
s += M[r + i][c + j];
if (s == 0) {
max = k;
rmax = r;
cmax = c;
}
k--;
}
}
}
*size = max;
if (max > 0) {
int **subm = malloc(max*sizeof(int *));
for (int i = 0 ; i < max ; i++) {
subm[i] = malloc(max*sizeof(int));
for (int j = 0 ; j < max ; j++)
subm[i][j] = M[rmax + i][cmax + j];
}
return subm;
} else
return NULL;
}
#include <stdlib.h>
#include <string.h>
void inscat(LStr L) {
if (L == NULL) return;
EStr *p = L;
while (p->next != NULL) {
if (strcmp(p->str, (p->next)->str) == 0) {
EStr *e = malloc(sizeof(EStr));
e->str = malloc(2*strlen(p->str) + 1);
strcpy(e->str, p->str);
strcat(e->str, p->str);
e->next = p->next;
p->next = e;
p = e->next;
} else
p = p->next;
}
}
#include <stdio.h>
void fextract(char *fname1, char *fname2, int g, int m, int a) {
FILE *f1 = fopen(fname1, "r");
FILE *f2 = fopen(fname2, "a");
if (f1 == NULL || f2 == NULL) return;
char s[41];
while (fscanf(f1, "%s", s) == 1) {
int gg, mm, aa;
fscanf(f1, "%d/%d/%d", &gg, &mm, &aa);
if (aa > a || ((aa == a && mm > m) || (aa == a && mm == m && gg >= g)))
fprintf(f2, "%s\n", s);
}
fclose(f1);
fclose(f2);
}
#include <stdlib.h>
#include <string.h>
static int isin(List L, char *v) {
while (L != NULL && strcmp(L->val, v) != 0)
L = L->next;
return (L != NULL);
}
List symdiff(List A, List B) {
List BA = NULL;
Elem *p = B;
while (p != NULL) {
if (!isin(A, p->val)) {
Elem *e = malloc(sizeof(Elem));
strcpy(e->val, p->val);
e->next = BA;
BA = e;
}
p = p->next;
}
Elem **pp = &A;
while (*pp != NULL) {
if (isin(B, (*pp)->val)) {
Elem *e = *pp;
*pp = e->next;
free(e);
} else
pp = &((*pp)->next);
}
*pp = BA;
return A;
}