-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
339 additions
and
0 deletions.
There are no files selected for viewing
10 changes: 10 additions & 0 deletions
10
codcad/programming-techniques/dynamic-programming/trabalho-do-papa/main.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
#include<iostream> | ||
#include<algorithm> | ||
|
||
using namespace std; | ||
|
||
int main(void){ | ||
|
||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
#include<stdio.h> | ||
#include<stdlib.h> | ||
|
||
typedef struct bolsa bolsa; | ||
struct bolsa{ | ||
int duracao; | ||
int entrega; | ||
}; | ||
|
||
int comp(const void * a, const void * b){ | ||
printf("%d %d\n", (*(bolsa*)a).entrega, ((*(bolsa*)b).entrega )); | ||
return ((*(bolsa*)a).entrega < (*(bolsa*)b).entrega); | ||
} | ||
|
||
int max(int a, int b){ | ||
return a > b? a : b; | ||
} | ||
|
||
int atraso(bolsa b, int tempoAtual){ | ||
return max(0, tempoAtual + b.duracao - b.entrega); | ||
} | ||
|
||
int maiorAtraso(bolsa * bolsas, int qtdBolsas){ | ||
int tempoAtual = 0; | ||
int maiorAtrasoTmp = 0; | ||
|
||
for(int i = 0; i < qtdBolsas; i++){ | ||
maiorAtrasoTmp = max(atraso(bolsas[i], tempoAtual), maiorAtrasoTmp); | ||
tempoAtual += bolsas[i].duracao; | ||
} | ||
|
||
return maiorAtrasoTmp; | ||
} | ||
|
||
int main(void){ | ||
// leitura dos dados | ||
int qtdBolsas; | ||
scanf("%d", &qtdBolsas); | ||
|
||
bolsa bolsas[qtdBolsas]; | ||
for(int i = 0; i < qtdBolsas; i++){ | ||
scanf("%d %d", &(bolsas[i].duracao), &(bolsas[i].entrega)); | ||
} | ||
|
||
for(int i = 0; i < qtdBolsas; i++){ | ||
printf("%d ", bolsas[i].entrega); | ||
} | ||
printf("\n"); | ||
qsort(bolsas, qtdBolsas, sizeof(bolsa), comp); | ||
for(int i = 0; i < qtdBolsas; i++){ | ||
printf("%d ", bolsas[i].entrega); | ||
} | ||
|
||
printf("\n"); | ||
printf("%d\n", maiorAtraso(bolsas, qtdBolsas)); | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
#include<iostream> | ||
#include<algorithm> | ||
|
||
using namespace std; | ||
|
||
typedef struct bolsa bolsa; | ||
struct bolsa{ | ||
int duracao; | ||
int entrega; | ||
}; | ||
|
||
int comp(bolsa a, bolsa b){ | ||
return a.entrega < b.entrega; | ||
} | ||
|
||
int max(int a, int b){ | ||
return a > b? a : b; | ||
} | ||
|
||
int atraso(bolsa b, int tempoAtual){ | ||
return max(0, tempoAtual + b.duracao - b.entrega); | ||
} | ||
|
||
int maiorAtraso(bolsa * bolsas, int qtdBolsas){ | ||
int tempoAtual = 0; | ||
int maiorAtrasoTmp = 0; | ||
|
||
for(int i = 0; i < qtdBolsas; i++){ | ||
maiorAtrasoTmp = max(atraso(bolsas[i], tempoAtual), maiorAtrasoTmp); | ||
tempoAtual += bolsas[i].duracao; | ||
} | ||
|
||
return maiorAtrasoTmp; | ||
} | ||
|
||
int main(void){ | ||
// leitura dos dados | ||
int qtdBolsas; | ||
scanf("%d", &qtdBolsas); | ||
|
||
bolsa bolsas[qtdBolsas]; | ||
for(int i = 0; i < qtdBolsas; i++){ | ||
scanf("%d %d", &(bolsas[i].duracao), &(bolsas[i].entrega)); | ||
} | ||
|
||
sort(bolsas, bolsas + qtdBolsas, comp); | ||
|
||
printf("%d\n", maiorAtraso(bolsas, qtdBolsas)); | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
2 | ||
1 100 | ||
10 10 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
2 | ||
1 2 | ||
10 10 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
8 | ||
1 | ||
3 | ||
5 | ||
6 | ||
8 | ||
10 | ||
12 | ||
13 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
10 | ||
8 | ||
11 | ||
21 | ||
16 | ||
11 | ||
8 | ||
27 | ||
57 | ||
27 | ||
21 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
7 | ||
2 | ||
2 | ||
4 | ||
3 | ||
12 | ||
12 | ||
3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
#include<stdio.h> | ||
#include<stdlib.h> | ||
|
||
int comp(const void * a, const void * b){ | ||
return (*(int*)a > *(int*)b); | ||
} | ||
|
||
int printVetor(int * vetor, int n){ | ||
for(int i = 0; i < n; i++){ | ||
printf("%d ", vetor[i]); | ||
} | ||
printf("\n"); | ||
} | ||
|
||
char * seqName(int value){ | ||
char * c; | ||
switch(value){ | ||
case 0: | ||
c = "do\0"; | ||
break; | ||
|
||
case 1: | ||
c = "do#\0"; | ||
break; | ||
|
||
case 2: | ||
c = "re\0"; | ||
break; | ||
|
||
case 3: | ||
c = "re#\0"; | ||
break; | ||
|
||
case 4: | ||
c = "mi\0"; | ||
break; | ||
|
||
case 5: | ||
c = "fa\0"; | ||
break; | ||
|
||
case 6: | ||
c = "fa#\0"; | ||
break; | ||
|
||
case 7: | ||
c = "sol\0"; | ||
break; | ||
|
||
case 8: | ||
c = "sol#\0"; | ||
break; | ||
|
||
case 9: | ||
c = "la\0"; | ||
break; | ||
|
||
case 10: | ||
c = "la#\0"; | ||
break; | ||
|
||
default: | ||
c = "si\0"; | ||
break; | ||
} | ||
|
||
return c; | ||
} | ||
|
||
int pattern[] = {2, 2, 1, 2, 2, 2, 1}; | ||
int patternSize = 7; | ||
int qtdNotas = 12; | ||
|
||
int * geradorDePatterns(int notaInicial) { | ||
int * newPattern = (int*) malloc((patternSize+1)* sizeof(int)); | ||
newPattern[0] = notaInicial; | ||
|
||
for(int i = 1; i <= patternSize; i++){ | ||
newPattern[i] = (newPattern[i-1] + pattern[i-1]) % qtdNotas; | ||
} | ||
|
||
return newPattern; | ||
} | ||
|
||
int diffNotas(int a, int b){ | ||
int diff = b - a; | ||
diff = (diff < qtdNotas - diff) ? diff : qtdNotas - diff; | ||
|
||
return diff; | ||
} | ||
|
||
int checkPattern(int * somePattern, int x){ | ||
printVetor(somePattern, patternSize); | ||
int isPattern = 1; | ||
for(int i = 0; i < 7 && isPattern; i++){ | ||
isPattern = somePattern[i] == pattern[(i+x) % patternSize]; | ||
} | ||
|
||
return isPattern; | ||
} | ||
|
||
int main(void) { | ||
for(int i = 0; i < 12; i++){ | ||
printVetor(geradorDePatterns(i), patternSize+1); | ||
} | ||
printf("----\n"); | ||
|
||
long n; | ||
scanf("%ld", &n); | ||
|
||
// le as notas | ||
int notas[n]; | ||
for(long i = 0; i < n; i++){ | ||
int nota; | ||
scanf("%d", ¬a); | ||
notas[i] = (nota-1) % 12; | ||
} | ||
|
||
// transforma em um vetor de notas individuais | ||
qsort(notas, n, sizeof(int), comp); | ||
int notasInd[12]; | ||
int iNotasInd = 0; | ||
notasInd[iNotasInd++] = notas[0]; | ||
long i; | ||
for(i = 1; i < n; i++){ | ||
while(notas[i] == notas[i-1] && i < n) i++; | ||
if(i == n) break; | ||
|
||
notasInd[iNotasInd++] = notas[i]; | ||
} | ||
|
||
printVetor(notasInd, iNotasInd); | ||
|
||
int isPattern = 0; | ||
|
||
|
||
|
||
char * patternName; | ||
if(!isPattern){ | ||
patternName = "desafinado\0"; | ||
}else{ | ||
patternName = seqName(notas[0] + i - 1); | ||
} | ||
|
||
printf("%s\n", patternName); | ||
return 0; | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
#include<stdlib.h> | ||
#include<stdio.h> | ||
int QTD_NOTAS = 12; | ||
|
||
typedef struct noLigado * noLigado; | ||
struct noLigado { | ||
int valor; | ||
noLigado prox; | ||
}; | ||
|
||
void adicionaNo(noLigado * lista, noLigado no){ | ||
if(*lista == NULL){ | ||
*lista = no; | ||
}else{ | ||
noLigado cabeca = *lista; | ||
while(lista->prox != cabeca){ | ||
|
||
} | ||
} | ||
} | ||
|
||
int main(void){ | ||
long n; | ||
scanf("%ld", &n); | ||
|
||
int tmp; | ||
int notas[n]; | ||
for(long i = 0; i < n; i++){ | ||
scanf("%d", &tmp); | ||
notas[i] = (tmp-1) % QTD_NOTAS; | ||
} | ||
|
||
|
||
return 0; | ||
} |