~misterio/BSI-SSC0502

e5504564f2928dc7ca56feb209ddad98f7392cc8 — Gabriel Fontes 9 months ago main
commit inicial
A  => Prova_ex6/1 +0 -0
A  => Prova_ex6/6.c +49 -0
@@ 1,49 @@
#include <stdio.h>
#include <stdbool.h>

#define SIZE 1000

//Recebe string com os colchetes, checa se são válidos
bool colchetes_validos(char *in) {
    int abertos = 0;

    //Caso a string não esteja alocada
    if (in == NULL) return false;

    //Percorrer string até chegar no caractere nulo
    for (int i = 0; in[i] != '\0'; i++) {
        //Caso seja um colchete de fechamento
        if (in[i] == '}') {
            //Se não existe nenhum colchete aberto, inválido
            if (abertos < 1) {
                return false;
            }
            //Caso exista aberto, decrementar contagem
            else {
                abertos -= 1;
            }
        }
        //Caso seja um colchete de abertura, incrementar contagem
        else if (in[i] == '{') {
            abertos += 1;
        }
    }
    //Depois de percorrer tudo
    //Caso exista algum colchete ainda aberto, retornar inválido
    if (abertos != 0) {
        return false;
    }
    else {
        return true;
    }
}


int main() {
    char entrada[SIZE];
    //Ler do input
    fgets(entrada, SIZE, stdin);

    if (colchetes_validos(entrada)) printf("Correto\n");
    else printf ("Errado\n");
}

A  => URI_exercicios/lista_1/1_Multiples.c +26 -0
@@ 1,26 @@
#include <stdio.h>
#include <stdbool.h>

//Checks if provided numbers are multiples
bool is_multiple (int a, int b){
  if (
    a % b == 0 ||
    b % a == 0
  ) {
    return true;
  }
  else {
    return false;
  }
}

int main() {
  int input_a, input_b;
  //Scanf is inside a for, to check if inputting was successful
  if (!scanf("%d %d", &input_a, &input_b)) return -1;

  if(is_multiple(input_a, input_b)) printf("Sao Multiplos\n");
  else                              printf("Nao sao Multiplos\n");

  return 0;
}

A  => URI_exercicios/lista_1/2_SixOddNumbers.c +42 -0
@@ 1,42 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

//Checks if given int is an even number
bool is_even(int in) {
  if(in % 2 == 0) {
    return true;
  }
  else {
    return false;
  }
}

//Returns an int array with the next six odd numbers from a provided int
int *get_odd_numbers(int in) {
  int *r = malloc(6 * sizeof(int));
  int i;

  for (i = 0; i < 6; i++) {
    int offset = 0; //We'll start from the number itself if it's odd.
    if(is_even(in)) offset = 1; //If it's even, offset by one.

    r[i] = in + offset + i*2;
  }
  return r;
}

int main() {
  int input, i;
  //Scanf is inside a for, to check if inputting was successful
  if (scanf("%d", &input)) {
    int *output = get_odd_numbers(input);
    for(i = 0; i < 6; i++) printf("%d\n", output[i]);

    free(output);
    return 0;
  }
  else {
    return -1;
  }
}

A  => URI_exercicios/lista_1/3_PlanejandoAEnergia.c +39 -0
@@ 1,39 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <math.h>
#include <locale.h>

#define MAX_SIZE 10000

double yearly_growth(int a, long long int b, int c, long long int d) {
    int length = c-a;
    long long int change = d-b;

    return change/(double)length;
}

int main() {
    double solution[MAX_SIZE]; 
    int n, i;
    if (!scanf("%d", &n)) return(-1);

    for (i = 0; i < n; i++) {
        int a, c;
        long long int b, d;

        if (!scanf("%d %lld %d %lld", &a, &b, &c, &d)) return(-1);
        solution[i] = ((long long int)(yearly_growth(a, b, c, d) * 100)) / 100.0;
    }

    for (i = 0; i < n; i++) {
        int intpart = (int)solution[i];
        int decpart = (solution[i] - intpart)*1000;
        if (decpart != 0 && ((decpart % 10)%10) > 5) decpart+= 10;
        printf("%d,%d", intpart, decpart/10);
        if (decpart == 0) printf("0");
        printf("\n");
    }

    return(0);
}

A  => URI_exercicios/lista_1/3_PlanningEnergy.c +25 -0
@@ 1,25 @@
#include <stdio.h>
#include <stdbool.h>
#include <locale.h>

float get_growth(int year_1, int year_1_usage, int year_2, int year_2_usage) {
	int years_no = year_2 - year_1;
	int usage_diff = year_2_usage - year_1_usage;
	return (usage_diff/(float)years_no);
}

int main () {
	int N;
	if (!scanf("%d", &N)) return -1;
	float solutions[N];

	for (int i = 0; i < N; i++) {
		int A, B, C, D;
		if (!scanf("%d %d %d %d", &A, &B, &C, &D)) return -1;

		solutions[i] = get_growth(A, B, C, D);		
	}
	for (int i = 0; i < N; i++) printf("%.2f\n", ((long int)(100 * solutions[i])) / 100.0);
	return 0;

}

A  => URI_exercicios/lista_1/4_ConstruindoCasas.c +42 -0
@@ 1,42 @@
#include <stdio.h>
#include <stdbool.h>
#include <math.h>
#include <unistd.h>

#define MAX_SIZE 10000

//Given desired building area, and max build percentage, return (truncated) plot dimension (assuming plot is square-shaped).
int get_plot_size(int build_area, int max_percentage) {
  double plot_area = build_area * (100 / (double)max_percentage);
  int plot_dim = (int)sqrt(plot_area);

  return plot_dim;
}



int main() {
  int solution[MAX_SIZE]; //Solutions array (so we can print them all together)
  int n_cases = 0; //Number of test cases (to be used when printing)
  int i;
  //Loop for each case
  for (i = 0; i < MAX_SIZE; i++) {
    int a, b, c;

    //Receive the 3 values
    //(All scanf returns are checked for best practices (and avoid -O2 compilation complaining))
    if (!scanf("%d", &a)) return -1;
    if (a == 0) break; //Exit loop if a is 0
    if (!scanf("%d %d", &b, &c)) return -1;

    //Store the solution
    solution[i] = get_plot_size(a*b, c);

    //Number of cases
    n_cases = i;
  }
  //Print array until position "cases"
  for (i = 0; i <= n_cases; i++) printf("%d\n", solution[i]);

  return 0;
}

A  => URI_exercicios/lista_1/5_LendoLivros.c +40 -0
@@ 1,40 @@
#include <stdio.h>
#include <stdbool.h>

#define MAX_SIZE 10000

//Checks if provided numbers are multiples
int pages_number(int q, int d, int p) {
  int r = d*q*p / (p-q);
  return r;
}

int main() {
  int solution[MAX_SIZE]; //Solutions array (so we can print them all together)
  int n_cases; //Number of test cases (to be used when printing)
  int i;

  //Loop for each case
  for (i = 0; i < MAX_SIZE; i++) {
    int q, d, p;

    //Scanf is inside a for, to check if inputting was successful
    if (!scanf("%d", &q)) return -1;
    if (q == 0) break;
    if (!scanf("%d %d", &d, &p)) return -1;

    solution[i] = pages_number(q, d, p);
    n_cases = i;
  }
  //Print array until position "cases"
  for (i = 0; i <= n_cases; i++) {
    if (solution[i] != 1) {
      printf("%d paginas\n", solution[i]);
    }
    else {
      printf("%d pagina\n", solution[i]);
    }
  }

  return 0;
}

A  => URI_exercicios/lista_2/fibonacci_vetor.c +44 -0
@@ 1,44 @@
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <math.h>
int maior_elemento_array(int *array, int tamanho) {
    int maior = INT_MIN;
    int i;
    for (i = 0; i < tamanho; i++) {
        if (array[i] > maior) {
            maior = array[i];
        }
    }
    return (maior);
}
long long int *gerar_fibonacci(int tamanho) {
    long long int *fib = (long long int *)malloc(sizeof(long long int)*tamanho);
    int i;
    for (i = 0; i < tamanho; i++) {
        if (i == 0) fib[i] = 0;
        else if (i == 1) fib[i] = 1;
        else fib[i] = fib[i-1] + fib[i-2];
    }
    return (fib);
}
int main(void) {
    int T;
    scanf("%d", &T);
    int *N = (int *)malloc(sizeof(int)*T);
    
    int i;
    for (i = 0; i < T; i++) {
        scanf("%d", &N[i]);
    }
    
    int maior = maior_elemento_array(N, T);
    long long int *fib = gerar_fibonacci(maior+1);
    
    for (i = 0; i < T; i++) {
        printf("Fib(%d) = %lld\n", N[i], fib[N[i]]);
    }

    free(fib);
    free(N);
}

A  => URI_exercicios/lista_2/preenchimento_vetor.c +23 -0
@@ 1,23 @@
#include <stdio.h>
#include <stdlib.h>

int *preencher_vetor_dobro(int tamanho, int primeiro) {
    int *vetor = (int *)malloc(sizeof(int)*tamanho);
    int i;
    vetor[0] = primeiro;
    for (i = 1; i < tamanho; i++) {
        vetor[i] = vetor[i-1]*2;
    }
    return (vetor);
}

int main(void) {
    int V, i, *N;

    scanf("%d", &V);
    N = preencher_vetor_dobro(10, V);
    for (i = 0; i < 10; i++)
        printf("N[%d] = %d\n", i, N[i]);
    free(N);
}
    

A  => URI_exercicios/lista_3/1_quantas_recursivas.c +34 -0
@@ 1,34 @@
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>

long long int fib(int n, int *cont, int b) {
    *cont += 1;
    printf(".");
    if (n == 0) return (0);
    else if (n == 1) return (1);
    else return (fib(n-1, cont, b)+fib(n-2, cont, b));
}

int contar_chamadas(int alvo, int base) {
    int chamadas = 0;
    fib(alvo, &chamadas, base);
    return (chamadas);
}

int main() {
    int N = 0;
    int B = 0;
    int casos = 0;
    do {
        scanf("%d %d", &N, &B);
        printf("Case %d: ", ++casos);
        if (B != 0) {
            int chamadas = contar_chamadas(N, B);
            while (chamadas >= B) chamadas %= B;
            printf("%d %d %d\n", N, B, chamadas);
        }
    } while (B != 0);

    return (0);
}

A  => URI_exercicios/lista_3/2_fila_do_banco.c +0 -0
A  => URI_exercicios/lista_3/3_lista_compras.c +7 -0
@@ 1,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char *remover_repetidos(char *in) {

}

A  => URI_exercicios/lista_3/4_assuntos_pendentes.c +28 -0
@@ 1,28 @@
#include <stdio.h>

int esta_aberto(char *in) {
    int abertos = 0;
    int i;
    for(i = 0; in[i] != '\0'; i++) {
        if (in[i] == '(') {
            abertos += 1;
        }
        else if (in[i] == ')') {
            if (abertos > 0) abertos -= 1;
        }
    }
    return abertos;
}

int main() {
    char entrada[100000];
    scanf("%s", entrada);

    int abertos = esta_aberto(entrada);
    if (abertos == 0) printf("Partiu RU!\n");
    else printf("Ainda temos %d assunto(s) pendentes!\n", abertos);

    return 0;
}



A  => URI_exercicios/lista_3/samuel.c +33 -0
@@ 1,33 @@
#include <stdio.h>


int main(){
    char vetor[30];
    char a[30];
    int indicador = 0;
    int i;

    //Enquanto o indicador for 0, significa que a chave não foi fechada
    printf("Digite qualquer combinacao de chaves:\n");
    scanf("%s", a);
    for (i = 0; a[i] != '\0';i++){
        if (a[i] == '{'){
            vetor[indicador] = a[i];
            indicador++;
        }
        else if (a[i] == '}'){
            if (indicador == 0){
                printf("Errado\n");
                return 0;
            }
            else{
                indicador--;
            }
        }
    }
      if (indicador == 0)
        printf("Correto\n");
    else
        printf("Errado\n");
    return 0;
}