Kimossab

Exemplo Exame-1 IP

Jan 9th, 2014
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.94 KB | None | 0 0
  1. //Exercicio 1
  2. //i x
  3. //0 1
  4. //3 2
  5. //6 3
  6. //9 4
  7. //
  8. //Resposta: 9 porque o i++ mostra o 9 e só depois é que adiciona
  9.  
  10. //Exercicio 2
  11. bool perfeito(int n)
  12. {
  13.     int i, soma=0;
  14.     for(i=n-1; i>0; i--)
  15.         {
  16.         if(n%i == 0)
  17.             soma += i;
  18.     }
  19.     if(soma == n)
  20.         return true;
  21.     else return false;
  22. }
  23.  
  24. void NumPerfeito(int N)
  25. {
  26.     int i;
  27.     for(i=1; i<=N; i++)
  28.     {
  29.         if(perfeito(i))
  30.             printf("%d\t", i);
  31.     }
  32. }
  33.  
  34. //Exercicio 3 a)
  35. typedef struct
  36. {
  37.     char NomeCidade[MAX_CIDADE];
  38.     int Mes[N_MESES];
  39.     int Entradas[N_MESES];
  40. }Acessos;
  41.  
  42. //Exercicio 3 b)
  43. void MinimoMes(Acessos *vect)
  44. {
  45.     int aux[N_MESES], i, j;
  46.     for(i=0; i<N_MESES; i++)
  47.     {
  48.         aux[i] = 0;
  49.         for(j=0; j<N_CIDADES; j++)
  50.             if((vect+j)->Entradas[i] > aux[i])
  51.                 aux[i] = (vect+j)->Entradas[i];
  52.         printf("%d\t", aux[i]);
  53.     }
  54. }
  55.  
  56. //Exercicio 3 c)
  57. void MaisAcessos(Acessos *vect)
  58. {
  59.     int aux=0, i, j, ai, aj;
  60.     for(i=0; i<N_MESES; i++)
  61.         for(j=0; j<N_CIDADES; j++)
  62.             if((vect+j)->Entradas[i] > aux)
  63.             {
  64.                 ai = i;
  65.                 aj = j;
  66.                 aux = (vect+j)->Entradas[i];
  67.             }
  68.     printf("O mes com mais acessos foi %d na cidade %d", ai, aj);
  69. }
  70.  
  71. //Exercicio 3 d)
  72. //Nao faço a minima como o fazer mas suponho que seja isto : :/
  73. #define MAX_ANOS 10
  74.  
  75. Acessos Ace[MAX_ANOS];
  76.  
  77. //Exercicio 4 a)
  78. float SomatIt(int N)
  79. {
  80.     int i, j;
  81.     long pot=9, fact=1;
  82.     float frac, soma=0;
  83.     for(i=1; i<=N; i++)
  84.     {
  85.         pot *= 3;
  86.         for(j=1; j<i; j++)
  87.             fact *= j;
  88.         frac = pot/fact;   
  89.         soma += frac+(2*i);
  90.     }
  91.     return soma;
  92. }
  93.  
  94. //Exercicio 4 b) Acho que é assim :/
  95. float SomatRec(int N, long pot, long fact, float soma, int i)
  96. {
  97.     int frac;
  98.     pot *= 3;
  99.     fact *= i-1;
  100.     frac = pot/fact;
  101.     soma += frac + (2*i);
  102.     if(i<N)
  103.         return soma+SomatRec(N, pot, fact, soma, ++i);
  104.     return soma;
  105. }
  106.  
  107. //Exercicio 5
  108. bool perfeito(int n)
  109. {
  110.     int i, soma=0;
  111.     for(i=n-1; i>0; i--)
  112.     {
  113.         if(n%i == 0)
  114.             soma += i;
  115.     }
  116.     if(soma == n)
  117.         return true;
  118.     else return false;
  119. }
  120.  
  121. void PrimPerf(int n)
  122. {
  123.     printf("Primeiros %d numeros perfeitos\n", n);
  124.     int i=0, a=1;
  125.     do
  126.     {
  127.         if(perfeito(a))
  128.         {
  129.             printf("%d\t", a++);
  130.             i++;
  131.         }
  132.     }while(i != n);
  133. }
  134.  
  135. //Exercicio 6
  136. #include <stdio.h>
  137. #include <malloc.h>
  138.  
  139. int Fib(int N)
  140. {
  141.         if(N == 0 || N == 1)
  142.                 return N;
  143.         else return Fib(N-1)+Fib(N-2);
  144. }
  145.  
  146. void LerVector(int *Vec, int n)
  147. {
  148.         int i;
  149.         for(i = 0; i<n; i++)
  150.         {
  151.                 printf("Qual o valor da posicao [%d]:", i);
  152.                 scanf("%d", Vec++);
  153.         }
  154. }
  155.  
  156. int *AlocarVector(int n)
  157. {
  158.         int*A;
  159.         A = (int*)malloc(n*sizeof(int));
  160.         if(A == NULL)
  161.         {
  162.                 printf("\nMemoria Insuficiente.");
  163.                 return 0;
  164.         }
  165.         return A;
  166. }
  167.  
  168. void main()
  169. {
  170.         int *VecA, n, i;
  171.         printf("Diga o numero de lelementos:");
  172.         scanf("%d", &n);
  173.         VecA = AlocarVector(n);
  174.         LerVector(VecA, n);
  175.         for(i=0;i<n;i++)
  176.                 printf("%d\t", Fib(*(VecA++)));
  177. }
Advertisement
Add Comment
Please, Sign In to add comment