Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- //EXAME 2011/2012 IP
- //1
- //i x
- //0 5
- //0 6 < 10
- // 0+2=2
- //2 7 < 10
- // 2+2=4
- //4 8 < 10
- // 4+2=6
- //6 9 < 10
- // 6+2=8
- //8 10 < 10 falso
- //++i = 8+1 = 9 < printf
- //2
- void NumeroPerfeito(int N)
- {
- printf("Numeros perfeitos:\n");
- int total;
- for(int i=2; i<=N; i++)
- {
- total = 0;
- for(int n=i-1; n>0; n--)
- {
- if(i%n == 0)
- total+=n;
- //if(total > i) break;
- }
- if(total == i) printf("%d\n", i);
- }
- }
- //3
- #define N_CIDADES 50
- #define N_MESES 12
- #define MAX_CIDADE 20
- //a
- typedef struct
- {
- char cidade[MAX_CIDADE];
- int ent[N_MESES];
- }entradas;
- entradas pais[N_CIDADES]; //nao necessario
- //b
- int *MinimoMes(entradas *e)
- {
- int aux[N_MESES];
- for(int o=0; o<N_MESES; o++)
- aux[o] = e[0].ent[o];
- for(int i=1; i<N_CIDADES; i++)
- {
- for(int n=0; n<N_MESES; n++)
- {
- if(e[i].ent[n] < aux[n])
- aux[n] = e[i].ent[n];
- }
- }
- return aux;
- }
- //c
- //teoricamente isto não é possível sem uma estrutura auxiliar
- struct cidmes
- {
- int cidade; // numero da cidadde do array pais criado anteriormente
- int mes;
- };
- struct cidmes CidMesMais()
- {
- int m, c, max=0;
- for(int i=0; i<N_CIDADES; i++)
- {
- for(int n=0; n<N_MESES; n++)
- {
- if(pais[i].ent[n] > max)
- {
- max = pais[i].ent[n];
- c=i;
- m=n;
- }
- }
- }
- //return {c,m}; // para nao complicar-vos a cabeça vou por da "maneira normal"
- struct cidmes aux;
- aux.cidade=c;
- aux.mes=m;
- return aux;
- }
- //4
- //a
- float SomaIt(int N)
- {
- float Soma=0.0, pot=4, fact=1; //pot = 2^2 | fact = 1! porque i começa em 1
- for(int i=1; i<=N; i++)
- {
- pot *= 2; //2^3 = 2^2 *2
- fact *= i; //2! = 2*1 ...
- Soma += pot/fact + i;
- }
- return Soma;
- }
- //b
- float SomaRec(int N, int i=1, float pot=4, float fact=1)
- {
- pot*=2;
- fact*=i;
- float sum = pot/fact + i; // não me perguntem porque, mas se não fizer estas 3 contas primeiro isto nao da certo -.-
- if(N == i) return sum;
- return sum + SomaRec(N,++i,pot,fact);
- }
- //5
- void NPrim(int N)
- {
- int i=2, n;
- printf("Primeiros %d numeros primos\n", N);
- if(N<=0)
- return;
- printf("[2]");
- while(N>1)
- {
- while(true)
- {
- i++;
- for(n=2; n<i; n++)
- if(i%n == 0)
- break;
- if(n == i)
- {
- printf("[%d]", i);
- N--;
- break;
- }
- }
- }
- }
- //6
- int Factorial(int n)
- {
- if(n == 1) return 1;
- return n*Factorial(n-1);
- }
- int *InvFact(int vect[], int tam)
- {
- int *f = (int *)malloc(sizeof(int) * tam);
- for(int i=0; i<tam; i++)
- f[sizeof(vect)-(i+1)] = Factorial(vect[i]);
- return f;
- }
- void main()
- {
- //6
- //"supondo n = 4"
- int fact[4];
- for(int i=0; i<4; i++)
- {
- printf("\nInsira um numero:");
- scanf("%d", &fact[i]);
- }
- printf("\n");
- int *invf = InvFact(fact, 4);
- for(int i=0; i<4; i++)
- printf("[%d]", invf[i]);
- //6 - Dinamico (malloc)
- int n;
- printf("Introduza o tamanho do vetor: ");
- scanf("%d", &n);
- int *fact = (int *)malloc(sizeof(int) * n);
- for(int i=0; i<n; i++)
- {
- printf("\nInsira um numero:");
- scanf("%d", &fact[i]);
- }
- int *v = InvFact(fact, n);
- printf("\nVector Factorial Invertido :");
- for(int i=0; i<n; i++)
- printf("[%d]", v[i]);
- free(v);
- free(fact); // sempre necessário free depois de usar o malloc para ser possivel reutilizar a memoria
- }
Advertisement
Add Comment
Please, Sign In to add comment