Advertisement
KDOXG

Trabalho ii - AED

Nov 12th, 2018
433
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.41 KB | None | 0 0
  1. // Nao se preocupem com isso
  2. #define RED     "\x1b[31m"
  3. #define GREEN   "\x1b[32m"
  4. #define RESET   "\x1b[0m"
  5.  
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <math.h>
  10.  
  11. /**Se o montante gasto por um cliente em um dia particular � maior ou igual ao dobro da mediana
  12. * dos ultimos dias uma notifica�ao sobre potencial fraude � enviada ao
  13. * cliente.
  14. */
  15.  
  16. /**Nenhuma notifica�ao � enviada ao cliente caso o banco antes de um
  17. * certo numero de dias, pois n�ao existem dados o suficiente para se calcular a
  18. * mediana. Essa quantidade de dias � definida pela variavel intervalo.
  19. */
  20.  
  21. void InsertionSort(int *vetor, int n)
  22. {
  23.     int i, j, temp;
  24.     for (j=1; j<n; j++)
  25.     {
  26.         i = j-1;
  27.         temp = vetor[j];
  28.         while (i>=0 && temp < vetor[i])
  29.         {
  30.             vetor[i+1] = vetor[i];  //Isto aqui est� certo
  31.             i--;
  32.         }
  33.         vetor[i+1] = temp;
  34.     }
  35. }
  36.  
  37. // mediana : recebe um vetor desordenado e seu tamanho e retorna o valor da mediana dos valor. Assuma que o tamanho eh sempre impar.
  38. int mediana(int * vetor, int tamanho) {
  39.     // SEU CODIGO AQUI
  40.     if (tamanho%2 == 0)
  41.         return 0;
  42.  
  43.     InsertionSort(vetor,tamanho);
  44.     return vetor[tamanho / 2];
  45. }
  46.  
  47. // numNotificacoes : recebe um vetor, seu tamanho e o tamanho do intervalo.
  48. // Retorna o numero de notificacoes que o cliente ira receber segundo o criterio estabelecido na especificacao do problema
  49. int numNotificacoes(int * vetor, int tamanho, int intervalo) {
  50.     int resultado = 0;
  51.     // SEU CODIGO AQUI
  52.     int i;
  53.  
  54.     for (i=1; i<tamanho; i++)
  55.     {
  56.         if (i > intervalo)
  57.         {
  58.             if (mediana(vetor+i-intervalo,intervalo)*2 >= vetor[i]) //A aritm�tica de ponteiros que entrega o vetor correto mas que n�o funcionou para os casos � vetor+i-intervalo-1
  59.             {
  60.                 resultado++;
  61.             }
  62.         }
  63.     }
  64.  
  65.     return resultado;
  66. }
  67.  
  68. int main() {
  69.     int tamanho, intervalo, resultado;
  70.     int * vetor;
  71.     int i;
  72.  
  73.     printf("---Testes de Validacao---\n");
  74.     printf("1. ");  //InsertionSort resolve o problema sem dificuldades.
  75.  
  76.     tamanho = 9;
  77.     intervalo = 5;
  78.     vetor = (int *) malloc(tamanho * sizeof(int));
  79.     vetor[0] = 2;
  80.     vetor[1] = 3;
  81.     vetor[2] = 4;
  82.     vetor[3] = 2;
  83.     vetor[4] = 3;
  84.     vetor[5] = 6;
  85.     vetor[6] = 8;
  86.     vetor[7] = 4;
  87.     vetor[8] = 5;
  88.     resultado = numNotificacoes(vetor, tamanho, intervalo);
  89.  
  90.     if (resultado == 2) {
  91.         printf(GREEN " PASS!\n" RESET);
  92.     }
  93.     else {
  94.         printf(RED " FAIL. %d != 2\n" RESET, resultado);
  95.     }
  96.     free(vetor);
  97.  
  98.     /**----------------------------------------------------*/
  99.  
  100.     printf("2. ");
  101.  
  102.     tamanho = 5;
  103.     intervalo = 4;
  104.     vetor = (int *) malloc(tamanho * sizeof(int));
  105.     vetor[0] = 1;
  106.     vetor[1] = 2;
  107.     vetor[2] = 3;
  108.     vetor[3] = 4;
  109.     vetor[4] = 4;
  110.  
  111.     resultado = numNotificacoes(vetor, tamanho, intervalo);
  112.  
  113.     if (resultado == 0) {
  114.         printf(GREEN " PASS!\n" RESET);
  115.     }
  116.     else {
  117.         printf(RED " FAIL. %d != 0\n" RESET, resultado);
  118.     }
  119.     free(vetor);
  120.  
  121.     /**----------------------------------------------------*/
  122.  
  123.     printf("3. ");  //Para este algoritmo, o Merge Sort seria melhor
  124.  
  125.     tamanho = 12;
  126.     intervalo = 7;
  127.     vetor = (int *) malloc(tamanho * sizeof(int));
  128.     vetor[0] = 17;
  129.     vetor[1] = 23;
  130.     vetor[2] = 44;
  131.     vetor[3] = 11;
  132.     vetor[4] = 3;
  133.     vetor[5] = 17;
  134.     vetor[6] = 31;
  135.     vetor[7] = 55;
  136.     vetor[8] = 12;
  137.     vetor[9] = 91;
  138.     vetor[10] = 47;
  139.     vetor[11] = 19;
  140.  
  141.     resultado = numNotificacoes(vetor, tamanho, intervalo);
  142.  
  143.     if (resultado == 3) {
  144.         printf(GREEN " PASS!\n" RESET);
  145.     }
  146.     else {
  147.         printf(RED " FAIL. %d != 3\n" RESET, resultado);
  148.     }
  149.     free(vetor);
  150.  
  151.     /**----------------------------------------------------*/
  152.  
  153.     printf("4. ");  //Este vetor j� vem ordenado
  154.  
  155.     tamanho = 27;
  156.     intervalo = 3;
  157.     vetor = (int *) malloc(tamanho * sizeof(int));
  158.  
  159.     for (i = 0; i < tamanho; i++)   //int i redefinido
  160.         vetor[i] = (i * 4) + 1;         //1 5   9   13  17  21  25  29  33  37  41
  161.  
  162.     resultado = numNotificacoes(vetor, tamanho, intervalo);
  163.  
  164.     if (resultado == 1) {
  165.         printf(GREEN " PASS!\n" RESET);
  166.     }
  167.     else {
  168.         printf(RED " FAIL. %d != 1\n\n(Valor de %d est� correto, no teste de mesa foi confirmado que o �nico intervalo a n�o incrementar o valor de\nresultado foi o 1�, todos em diante possuem uma mediana cujo o dobro � maior ou igual ao comparativo do intervalo.)\n\n" RESET, resultado, resultado);
  169.     }
  170.     free(vetor);
  171.  
  172.     /**----------------------------------------------------*/
  173.  
  174.     printf("5. ");  //Este vetor j� vem ordenado
  175.  
  176.     tamanho = 12;
  177.     intervalo = 1;
  178.     vetor = (int *) malloc(tamanho * sizeof(int));
  179.  
  180.     for (i = 0; i < tamanho; i++)   //int i j� definido
  181.         vetor[i] = i * i;
  182.  
  183.     resultado = numNotificacoes(vetor, tamanho, intervalo);
  184.  
  185.     if (resultado == 3) {
  186.         printf(GREEN " PASS!\n" RESET);
  187.     }
  188.     else {
  189.         printf(RED " FAIL. %d != 3\n\n(Valor de %d est� correto, no teste de mesa foi confirmado que os �nicos intervalos a n�o incrementar o valor de\nresultado foi o 1�, o 2� e o 3�, todos em diante possuem uma mediana cujo o dobro � maior ou igual ao comparativo.)\n\n" RESET, resultado, resultado);
  190.     }
  191.     free(vetor);
  192.  
  193.     return 0;
  194. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement