Kimossab

EXAME 2011/2012 IP

Jan 6th, 2015
341
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.25 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. //EXAME 2011/2012 IP
  5.  
  6. //1
  7. //i     x  
  8. //0     5
  9. //0     6 < 10
  10. //  0+2=2  
  11. //2     7 < 10
  12. //  2+2=4  
  13. //4     8 < 10
  14. //  4+2=6  
  15. //6     9 < 10
  16. //  6+2=8
  17. //8     10 < 10 falso
  18. //++i = 8+1 = 9 < printf
  19.  
  20. //2
  21. void NumeroPerfeito(int N)
  22. {
  23.     printf("Numeros perfeitos:\n");
  24.     int total;
  25.     for(int i=2; i<=N; i++)
  26.     {
  27.         total = 0;
  28.         for(int n=i-1; n>0; n--)
  29.         {
  30.             if(i%n == 0)
  31.                 total+=n;
  32.             //if(total > i) break;
  33.         }
  34.         if(total == i) printf("%d\n", i);
  35.     }
  36. }
  37.  
  38. //3
  39. #define N_CIDADES 50
  40. #define N_MESES 12
  41. #define MAX_CIDADE 20
  42. //a
  43. typedef struct
  44. {
  45.     char cidade[MAX_CIDADE];
  46.     int ent[N_MESES];
  47. }entradas;
  48. entradas pais[N_CIDADES]; //nao necessario
  49. //b
  50. int *MinimoMes(entradas *e)
  51. {
  52.     int aux[N_MESES];
  53.     for(int o=0; o<N_MESES; o++)
  54.         aux[o] = e[0].ent[o];
  55.     for(int i=1; i<N_CIDADES; i++)
  56.     {
  57.         for(int n=0; n<N_MESES; n++)
  58.         {
  59.             if(e[i].ent[n] < aux[n])
  60.                 aux[n] = e[i].ent[n];
  61.         }
  62.     }
  63.     return aux;
  64. }
  65. //c
  66. //teoricamente isto não é possível sem uma estrutura auxiliar
  67. struct cidmes
  68. {
  69.     int cidade; // numero da cidadde do array pais criado anteriormente
  70.     int mes;
  71. };
  72. struct cidmes CidMesMais()
  73. {
  74.     int m, c, max=0;
  75.     for(int i=0; i<N_CIDADES; i++)
  76.     {
  77.         for(int n=0; n<N_MESES; n++)
  78.         {
  79.             if(pais[i].ent[n] > max)
  80.             {
  81.                 max = pais[i].ent[n];
  82.                 c=i;
  83.                 m=n;
  84.             }
  85.         }
  86.     }
  87.     //return {c,m}; // para nao complicar-vos a cabeça vou por da "maneira normal"
  88.     struct cidmes aux;
  89.     aux.cidade=c;
  90.     aux.mes=m;
  91.     return aux;
  92. }
  93. //4
  94. //a
  95. float SomaIt(int N)
  96. {
  97.     float Soma=0.0, pot=4, fact=1; //pot = 2^2 | fact = 1! porque i começa em 1
  98.     for(int i=1; i<=N; i++)
  99.     {
  100.         pot *= 2; //2^3 = 2^2 *2
  101.         fact *= i; //2! = 2*1 ...
  102.         Soma += pot/fact + i;
  103.     }
  104.     return Soma;
  105. }
  106. //b
  107. float SomaRec(int N, int i=1, float pot=4, float fact=1)
  108. {
  109.     pot*=2;
  110.     fact*=i;
  111.     float sum = pot/fact + i; // não me perguntem porque, mas se não fizer estas 3 contas primeiro isto nao da certo -.-
  112.     if(N == i) return sum;
  113.     return sum + SomaRec(N,++i,pot,fact);
  114. }
  115. //5
  116. void NPrim(int N)
  117. {
  118.     int i=2, n;
  119.     printf("Primeiros %d numeros primos\n", N);
  120.     if(N<=0)
  121.         return;
  122.     printf("[2]");
  123.     while(N>1)
  124.     {
  125.         while(true)
  126.         {
  127.             i++;
  128.             for(n=2; n<i; n++)
  129.                 if(i%n == 0)
  130.                     break;
  131.             if(n == i)
  132.             {
  133.                 printf("[%d]", i);
  134.                 N--;
  135.                 break;
  136.             }
  137.         }
  138.     }
  139. }
  140. //6
  141. int Factorial(int n)
  142. {
  143.     if(n == 1) return 1;
  144.     return n*Factorial(n-1);
  145. }
  146. int *InvFact(int vect[], int tam)
  147. {
  148.     int *f = (int *)malloc(sizeof(int) * tam);
  149.     for(int i=0; i<tam; i++)
  150.         f[sizeof(vect)-(i+1)] = Factorial(vect[i]);
  151.     return f;
  152. }
  153. void main()
  154. {
  155.     //6
  156.     //"supondo n = 4"
  157.     int fact[4];
  158.     for(int i=0; i<4; i++)
  159.     {
  160.         printf("\nInsira um numero:");
  161.         scanf("%d", &fact[i]);
  162.     }
  163.     printf("\n");
  164.     int *invf = InvFact(fact, 4);
  165.     for(int i=0; i<4; i++)
  166.         printf("[%d]", invf[i]);
  167.  
  168.     //6 - Dinamico (malloc)
  169.     int n;
  170.     printf("Introduza o  tamanho do vetor: ");
  171.     scanf("%d", &n);
  172.     int *fact = (int *)malloc(sizeof(int) * n);
  173.     for(int i=0; i<n; i++)
  174.     {
  175.         printf("\nInsira um numero:");
  176.         scanf("%d", &fact[i]);
  177.     }
  178.     int *v = InvFact(fact, n);
  179.     printf("\nVector Factorial Invertido :");
  180.     for(int i=0; i<n; i++)
  181.         printf("[%d]", v[i]);
  182.  
  183.     free(v);
  184.     free(fact); // sempre necessário free depois de usar o malloc para ser possivel reutilizar a memoria
  185. }
Advertisement
Add Comment
Please, Sign In to add comment