Would you like to improve the performance of this code?

-3

I already found the resolution of this question and all the results are in agreement with what the question asks, but I think the solution is a bit slow, because it is giving Time limit exceeded

Issue Link

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

 struct p
 {
    char presente[3][100], nome[100];
 };

 int main(int argc, char** argv)
 {
   int  i, j;
   int teste;
   char nome1[100], nome2[100];
   while(scanf("%d", &teste) != EOF)
   {
     struct p presentes[teste];

      for(i = 0; i < teste; i++)
      {
         scanf("%s", presentes[i].nome);
         for(j = 0; j < 3; j++)
         {
            scanf("%s", presentes[i].presente[j]);
         }
      }
      int achou = 0, c = 0;
      do
      {
         scanf("%s %s", nome1, nome2);
         for(i = 0, achou = 0; i < teste; i++)
         {
            for(j = 0; j < 3; j++)
            {
                if(0 == strcmp(presentes[i].nome, nome1) && (0 == strcmp(presentes[i].presente[j], nome2)))
                {
                    achou = 1;
                    c++;
                }
            }
        }
        if(achou == 1)
        {
            printf("Uhul! Seu amigo secreto vai adorar o/\n");
        }
        else
        {
            printf("Tente Novamente!\n");
        }
    }
    while(c != 3);
}
  system("pause");
  return 0;
}
    
asked by anonymous 20.01.2018 / 22:16

1 answer

-1

First, please put the problem to evaluate us as well, I suppose this problem is someone from the right URI? But looking at your code looks like it could be improved in some ways

for(j = 0; j < 3; j++)
        {
            if(0 == strcmp(presentes[i].nome, nome1) && (0 == strcmp(presentes[i].presente[j], nome2)))
            {
                achou = 1;
                c++;
                break;
            }
        }

Using the break you to run when you find such a situation.

    
20.01.2018 / 23:00