Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #define TAM_AMIGO 100
- #define TAM_TOTAL 200
- // alfa_tarefa_a.c, versão 1.0 @ Sun Mar 3 11:14:59 BRT 2013
- // alfa_tarefa_a.c, versão 1.1 @ Sun Mar 10 00:06:35 BRT 2013
- int soma_vetor(int *v, int len) ;
- void *my_malloc(int tam) ;
- int soma_vetor(int *v, int len) {
- int i ;
- int soma=0 ;
- for(i=0; i<len; i++) soma+=v[i] ;
- return soma ;
- }
- void *my_malloc(int tam) {
- char *p ;
- p = malloc(tam * sizeof(int)) ;
- if(p == NULL) {
- printf("Erro: Impossivel alocar memora.\n") ;
- exit(-1) ;
- }
- return p ;
- }
- int main(void) {
- int *vetor_tesouros ;
- int *vetor_amigo1 ;
- int *vetor_amigo2 ;
- int tam_amigo1, tam_amigo2 ;
- int N, i, j, t ;
- printf("Digite o valor do bau: ") ;
- scanf("%d", &N) ;
- /* Aloca memória para os vetores */
- vetor_tesouros = my_malloc(N) ;
- vetor_amigo1 = my_malloc(N) ;
- vetor_amigo2 = my_malloc(N) ;
- /* leitura dos dados */
- for(i=0; i<N; i++) {
- printf("Digite o valor de numero %d: ", i+1) ;
- scanf("%d", &vetor_tesouros[i]) ;
- }
- /* ordenação */
- for(i=0; i<N; i++) {
- for(j=i+1; j<N; j++) {
- if(vetor_tesouros[i]<vetor_tesouros[j]){
- t=vetor_tesouros[i];
- vetor_tesouros[i]=vetor_tesouros[j];
- vetor_tesouros[j]=t;
- }
- }
- }
- /* distribuição dos tesouros */
- vetor_amigo1[0] = vetor_tesouros[0] ; tam_amigo1 = 1 ;
- vetor_amigo2[0] = vetor_tesouros[1] ; tam_amigo2 = 1 ;
- for(i=2; i<N; i++) {
- /* se a soma do amigo1 for menor então coloca o elemento nele
- * caso contrário coloca o elemento no amigo2. */
- if(soma_vetor(vetor_amigo1, tam_amigo1) < soma_vetor(vetor_amigo2, tam_amigo2)) {
- vetor_amigo1[tam_amigo1++] = vetor_tesouros[i] ;
- } else {
- vetor_amigo2[tam_amigo2++] = vetor_tesouros[i] ;
- }
- }
- /* output */
- printf("output: %d\n", abs(soma_vetor(vetor_amigo1, tam_amigo1)-soma_vetor(vetor_amigo2, tam_amigo2))) ;
- free(vetor_tesouros) ;
- free(vetor_amigo1) ;
- free(vetor_amigo2) ;
- return 0 ;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement