Advertisement
mantertius

aprendi_ftt_v2.c

Dec 17th, 2020
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.01 KB | None | 0 0
  1. //https://thehuxley.com/problem/1036
  2. #include <stdio.h>
  3. #include <string.h>
  4.  
  5. #define MAXN 10000
  6. int p[MAXN];//coeficientes de p
  7. int q[MAXN];//coeficientes de q
  8. int r[MAXN];//coeficientes de resultado
  9.  
  10. static int caso = 0; //numero de casos
  11.  
  12. void imprimir(int tamanho, int atual)
  13. {
  14.     if (tamanho == atual)
  15.     {
  16.         printf("\n");
  17.         return;
  18.     }
  19.     else
  20.     {
  21.         printf(" %d",r[atual]);
  22.         imprimir(tamanho,atual+1);
  23.     }
  24.    
  25. }
  26.  
  27. void poli_multiplier(int array1[],int array2[], int tamanho_a,int tamanho_b,int array3[], int index_a, int index_b)
  28. //index_a fica travado e index_b vai aumentando até que index_b == tamanho_b, dai recomeça e  vai até index_a == tamanho_a e index_b == tamanho b
  29. {  
  30.     int grau_result = index_a+index_b;
  31.     int anterior = r[grau_result];
  32.    
  33.     printf("p[%d]*q[%d] = r[%d], anterior[%d] = %d →→ r[%d] = ",index_a,index_b,grau_result,grau_result,anterior,grau_result);
  34.     printf("%d*%d\n", array1[index_a],array2[index_b]);
  35.     if(index_b == tamanho_b)
  36.     {
  37.        
  38.         if(index_a < tamanho_a-1)
  39.         {
  40.             r[grau_result] = ((array1[index_a]*array2[index_b])+anterior);
  41.             printf("%d\n",r[grau_result]);
  42.             poli_multiplier(array1,array2,tamanho_a,tamanho_b,array3,index_a+1,0);
  43.         }
  44.         else if (index_a == tamanho_a-1)//chegou ao fim
  45.         {
  46.             r[grau_result] = ((array1[index_a]*array2[index_b])+anterior);
  47.             printf("%d\n",r[grau_result]);
  48.             caso = caso+1;
  49.             return;
  50.         }
  51.     }
  52.     else //index_b != tamanho_b
  53.     {
  54.         r[grau_result]=((array1[index_a]*array2[index_b])+anterior);
  55.         printf("%d\n",r[grau_result]);
  56.         poli_multiplier(array1,array2,tamanho_a,tamanho_b,array3,index_a,index_b+1);
  57.        
  58.     }
  59.    
  60.  
  61. }
  62.  
  63. int scan_coef(int numero_de_coef, int counter, int tipo) //escaneia até chegar no numero maximo de coeficientes = grau+1
  64. {
  65.     if (counter>=numero_de_coef) //caso de parada
  66.     {
  67.         return counter;
  68.     }
  69.  
  70.     int coef;
  71.     scanf("%d",&coef);
  72.     //printf("coef%d:%d\n",counter,coef);
  73.  
  74.     if(tipo == 1)
  75.     {
  76.         p[counter] = coef;
  77.         return scan_coef(numero_de_coef,counter+1,tipo);
  78.        
  79.     }
  80.     else
  81.     {
  82.         q[counter] = coef;
  83.         return scan_coef(numero_de_coef,counter+1,tipo);
  84.     }
  85. }
  86. void scan_eof() //scaneia até chegar em EOF
  87. {
  88.     int g1,g2;
  89.     int hm = scanf("%d",&g1);
  90.     if(hm == EOF)
  91.     {
  92.         return;
  93.     }
  94.     scanf("%d",&g2);
  95.    
  96.     int tamanho_p = scan_coef(g1+1,0,1); //numero de coeficientes em p
  97.     int tamanho_q = scan_coef(g2+1,0,2); //numero de coeficientes em q
  98.    
  99.     int tamanho_r = g1+g2+1; //numero de coeficientes em r
  100.     //printf("~~tamanho do r[]:%d~~\n",tamanho_r);
  101.    
  102.     //limpa_array(r,MAXN,0);
  103.     memset(r,0,sizeof(r));
  104.     poli_multiplier(p,q,tamanho_p,tamanho_q,r,0,0);
  105.     printf("Caso #%d:", caso);
  106.     imprimir(tamanho_r, 0);
  107.     scan_eof();
  108.  
  109. }
  110. int main ()
  111. {
  112.     scan_eof();
  113.     return 0;
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement