diff options
Diffstat (limited to 'Random')
-rw-r--r-- | Random/det.c | 50 | ||||
-rw-r--r-- | Random/list_all_substrings.c | 24 | ||||
-rw-r--r-- | Random/quicksort.c | 45 | ||||
-rw-r--r-- | Random/recursividade_sequencia.c | 42 |
4 files changed, 161 insertions, 0 deletions
diff --git a/Random/det.c b/Random/det.c new file mode 100644 index 0000000..8e3f524 --- /dev/null +++ b/Random/det.c @@ -0,0 +1,50 @@ +#include <stdio.h> +void printm(int MATRIZ[][100], int n){ + for(int i=0; i<n; ++i){ + for(int j=0; j<n; ++j){ + printf("%d ", MATRIZ[i][j]); + } + printf("\n"); + } +} + +void copy(int MATRIZ[][100], int COPY[][100], int n, int y, int x){ + int COPY_Y = 0, COPY_X = 0; + for(int i = 0; i < n; ++i){ + if(i != y){ + COPY_X = 0; + for(int j = 0; j < n; ++j){ + if(j != x){ + COPY[COPY_Y][COPY_X] = MATRIZ[i][j]; + COPY_X++; + } + } + COPY_Y++; + } + } +} + +int det(int MATRIZ[][100], int n){ + int d = 0; + if(n == 1) return MATRIZ[0][0]; + int COPY[100][100]; + int sinal = -1; + for(int i=0; i<n; ++i){ + sinal *= -1; + copy(MATRIZ, COPY, n, 0, i); +// printm(COPY, n-1); +// printf("\n"); + d += sinal * MATRIZ[0][i] * det(COPY, n-1); + } + return d; +} + +int main(){ + int n; + scanf("%d", &n); + int MATRIZ[100][100]; + for(int i=0; i<n; ++i) + for(int j=0; j<n; ++j) + scanf("%d", &MATRIZ[i][j]); + printf("%d\n", det(MATRIZ, n)); +} diff --git a/Random/list_all_substrings.c b/Random/list_all_substrings.c new file mode 100644 index 0000000..e51d1ea --- /dev/null +++ b/Random/list_all_substrings.c @@ -0,0 +1,24 @@ +#include <stdio.h> +int count = 0; +void resp(char *s, int i, int n){ + printf("\n"); + if(s[n+1] == '\0' && i <= n){ + for(int j=i; j <= n; ++j){ + printf("%c", s[j]); + } + count++; + resp(s, 0, n-i-1); + }else if(i <= n){ + for(int j=i; j <= n; ++j){ + printf("%c", s[j]); + } + count++; + resp(s, i+1, n+1); + } + return; +} + +int main(){ + resp("ABCDEFG", 0, 6); + printf("%d\n", count); +} diff --git a/Random/quicksort.c b/Random/quicksort.c new file mode 100644 index 0000000..3d261db --- /dev/null +++ b/Random/quicksort.c @@ -0,0 +1,45 @@ +#include <stdio.h> +#include <stdlib.h> +int ve = 0; +void quick_sort(int *v, int inicio, int fim){ + if(inicio >= fim) return; + int posPivot = (inicio+fim)/2; + int pivot = v[posPivot]; + int sp = inicio; + int sv = fim; + while(sp != sv){ + ve++; + if(sp < posPivot){ + if(v[sp] > pivot){ + v[posPivot] = v[sp]; + posPivot = sp; + }else{ + sp++; + } + }else if(sv > posPivot){ + if(v[sv] < pivot){ + v[posPivot] = v[sv]; + posPivot = sv; + }else{ + sv--; + } + } + } + v[sv] = pivot; + + quick_sort(v, inicio, sv-1); + quick_sort(v, sv+1, fim); +} + +int main(){ + + int v[3] = {7,4,2}; + + for(int i = 0; i < 3; ++i) printf("%d ", v[i]); + printf("\n"); + quick_sort(v, 0, 2); + for(int i = 0; i < 3; ++i) printf("%d ", v[i]); + printf("\n"); + printf("%d\n", ve); + return 0; +} diff --git a/Random/recursividade_sequencia.c b/Random/recursividade_sequencia.c new file mode 100644 index 0000000..2c32ee1 --- /dev/null +++ b/Random/recursividade_sequencia.c @@ -0,0 +1,42 @@ +#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#define MAX 100
+
+int verificar(char *c, int inicio, int fim){
+ int tam = fim - inicio;
+ int v = 1, f1 = 0;
+ for(int i = inicio; i < fim+1; ++i){
+ if(c[i] == '1' && f1 == 1)
+ v = 0;
+ if(c[i] == '1')
+ f1 = 1;
+ if(c[i] == '0')
+ f1 = 0;
+ }
+ return v;
+}
+
+int seq(char* c, int inicio, int fim){
+ if(c[fim+1] == '\0' && fim >= inicio){
+ if(verificar(c, inicio, fim)){
+ return 1 + seq(c, 0, fim-inicio-1);
+ }else{
+ return seq(c, 0, fim-inicio-1);
+ }
+ }else if(fim >= inicio){//1
+ if(verificar(c, inicio, fim)){
+ return 1 + seq(c, inicio+1, fim+1);
+ }else{
+ return seq(c, inicio+1, fim+1);
+ }
+ }
+ return 0;
+}
+int main(){
+ int inicio = 0, fim = 3;
+ char *c = "0111";
+ printf("%d\n", seq(c, inicio, fim));
+
+ return 0;
+}
|