Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- int frequencyNumber(int array[], int x, int i, int j){
- int m;
- int retorno = 0;
- int k;
- m = (i+j)/2;
- if(j == 1 && array[m] == x){
- retorno++;
- m = 0;
- }
- if(i < j - 1){
- if(array[m] > x){
- j = m;
- return frequencyNumber(array, x, i, j);
- }else if(array[m] < x){
- i = m;
- return frequencyNumber(array, x, i, j);
- }else{
- retorno++;
- k = m - 1;
- while(k >= i && array[k] == x){
- retorno++;
- k--;
- }
- k = m + 1;
- while(k <= j && array[k] == x){
- retorno++;
- k++;
- }
- }
- }
- return retorno;
- }
- int main()
- {
- int *array = NULL;
- int *numero_frequencia = NULL;
- int *caso_array = NULL;
- int *caso_numero_frequencia = NULL;
- int T;
- int N = 0, Q = 0;
- int i, j;
- int resultado;
- FILE* entrada;
- FILE* saida;
- entrada = fopen("T1Q1.in.txt", "r");
- saida = fopen("T1Q1.out.txt", "w");
- fscanf(entrada, "%d", &T);
- for(j = 1; j <= T; j++)
- {
- fscanf(entrada, "%d", &N);
- fscanf(entrada, "%d", &Q);
- caso_array = (int*)realloc(array, N*sizeof(int));
- caso_numero_frequencia = (int*)realloc(numero_frequencia, Q*sizeof(int));
- for(i = 0; i < N; i++){
- fscanf(entrada, "%d", &caso_array[i]);
- }
- for(i = 0; i < Q; i++){
- fscanf(entrada, "%d", &caso_numero_frequencia[i]);
- }
- fprintf(saida, "caso %d :\n", j);
- for(i = 0; i < Q; i++){
- resultado = frequencyNumber(caso_array, caso_numero_frequencia[i], 0, N - 1);
- fprintf(saida, "%d\n", resultado);
- }
- }
- fclose(entrada);
- fclose(saida);
- free(caso_array);
- free(caso_numero_frequencia);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement