Advertisement
mantertius

aprendi_ftt.c

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