Google Code Jam 2010 - Africa Classification Round A

Publicado por Enzo de Brito Ferber (última atualização em 20/12/2011)

[ Hits: 3.729 ]

Homepage: http://www.maximasonorizacao.com.br

Download StoreCredit.zip

Download 1324301436.StoreCredit.zip (versão 2)




Problema A.

http://code.google.com/codejam/contest/dashboard?c=351101#s=p1

Lê a descrição no google mesmo. Necessário conhecimento em inglês!

;)

Divirtam-se....

  



Versões atualizadas deste script

Versão 2 - Enviado por Enzo de Brito Ferber em 19/12/2011

Changelog: Erro de execução na função free() dentro de getnum().
Trocado free(aux) por free(s).

Download 1324301436.StoreCredit.zip


Esconder código-fonte

/* store.c
 * 
 * Enzo Ferber : <enzo@veloxmail.com.br>
 * dez 2011
 */
 
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int getnum ( char *str, int a, int b )
{
    char *s, *aux = (char *)malloc( (b - a + 1) * sizeof(char));
    register int i, r;
    
    // backup initial 'aux' addr
    s = aux;
    
    // copy the string from point A to B
    for ( i = a; i < b; i++ ) *(aux++) = str[i];
    
    // finishes the stirng with null-terminating byte
    *aux = 0x0;
    
    // result
    r = atoi(s);
    
    // free aux
    free ( aux );
    // return an INTEGER number   
    return r;
}

int *array( char *str, int n )
{
    register int i, j = 0;
    
    // this gets the new array of size 'n'
    int *p, *a = (int *)malloc(n * sizeof(int));
    p = a;
    
    for ( i = 0; i <= strlen(str); i++ )
    {
        if ( str[i] == ' ' || str[i] == '{FONTE}' ) {
             // j is the last start position in the string
             // i is the current space/null char in the string
             *(p++) = getnum(str, j, i);
             j = i;
        }
    }
    return a;
}

// JAM SUM
int *jsum ( int credit, int nitens, int *itens )
{
    register int i, j;
    int *r = (int*)malloc(2*sizeof(int));
    int f = 0;
    
    for ( i = 0; i < nitens; i++ )
    {
        for ( j = 0; j < nitens; j++ )
        {
            // can't buy same item twice!
            if ( j == i ) continue;
            
            // check the sum
            if ( (itens[i] + itens[j]) == credit) {
                 r[0] = (i < j) ? i+1 : j+1;
                 r[1] = (i < j) ? j+1 : i+1;
                 f = 1;
                 break;
            }
        }
        if( f ) break;
    }
    
    return r;
}

int main ( int argc, char *argv[] )
{
    FILE *input;
    register int i = 0, j = 0, lines = 0;
    char c, line[152][12000]; // large dataset default
    
    // JAM!
    int credit, nitens, *itens, *r;
    
    // arguments check
    if ( argc < 2 )
    {
         printf ( "Usage: %s <input_file>\n", argv[0] );
         exit ( -1 );
    }
    
    // try to open input file from arguments line
    if ( !(input = fopen(argv[1], "r")))
    {
         printf ( "# Could not open file %s for reading.\n", argv[1] );
         exit ( -1 );
    }
    
    // read file
    while ( (c = fgetc(input)) != EOF )
    {
          if ( c != '\n' )
             line[lines][j++] = c;
          else
          {
              line[lines][j] = 0x0;
              lines++;
              j = 0;
          }
    }
    
    // each test-case has 3 lines!
    for ( i = 1, j = 1; i < lines; i+=3, j++ )
    {
          itens = NULL;
          credit = atoi(line[i]);
          nitens = atoi(line[i+1]);
          itens = array(line[i+2], nitens);
          
          // jsum = JAM sum
          r = jsum(credit, nitens, itens);
          printf ( "Case #%d: %d %d\n", j, r[0], r[1]);
    }
    
    return 0;
}
    
    
    
    

Scripts recomendados

Arvores Red Black

Driver tgz

Pilha

Trabalhando com Matrizes

Jantar dos Filósofos - Programação Paralela


  

Comentários
[1] Comentário enviado por EnzoFerber em 19/12/2011 - 11:21h

Edit: Na função getnum(), troque free(aux) por free(s). ;)


Contribuir com comentário




Patrocínio

Site hospedado pelo provedor RedeHost.
Linux banner

Destaques

Artigos

Dicas

Tópicos

Top 10 do mês

Scripts