Character comparison error with strcmp

1

In the question you ask me to create a pair of Right and Left shoes so I used strcmp to compare, I used 1 because the strings have to be different, but I did not get the result that the question asked for.

Link to the question link

Here is my code

#include<stdio.h>
#include<stdlib.h>
#include <string.h>
int main(void)
{
  int i, teste, j, c = 0;
  while(scanf("%d", &teste) != EOF)
  {
    c = 0;
    int vet[teste];
    char nome[teste][100];
    for(i = 0; i < teste; i++)
    {
        scanf("%d %s", &vet[i], nome[i]);
    }
    for(i = 0; i < teste; i++)
    {
        for(j = 0; j < teste; j++)
        {
            if(vet[i] == vet[j])
            {
                if(strcmp(nome[i], nome[j]) == 1)
                {
                    c++;
                }
            }
        }
    }
    printf("%d\n", c);
}

}
    
asked by anonymous 28.12.2017 / 13:47

1 answer

1

You are traversing the wrong vector vector. In your logic if there are 1 million boots 40 D and in the end there is only one boot 40 E, its result will show that there are 1 million pairs. The right thing would be for you to check if a boot already has the foot found.

#include<stdio.h>
#include<stdlib.h>
#include <string.h>
int main(void)
{
  int i, teste, j, c = 0;
  while(scanf("%d", &teste) != EOF)
 {
   c = 0;
   int vet[teste];
   char nome[teste][100];
   //Criei esse vetor que marcar as botas
   int vestido[teste];
   for (i = 0 ; i < teste; i++)
   {
       vestido[i] = 0;
   }
   for(i = 0; i < teste; i++)
   {
       scanf("%d %s", &vet[i], nome[i]);
   }
   for(i = 0; i < teste; i++)
   {
       for(j = 0; j < teste ; j++)
       {
           //aqui eu verifico se a bota foi encontrada
           if(vet[i] == vet[j] && vestido[i]!=1 & vestido[j]!=1)
           {
               if(strcmp(nome[i], nome[j]) == 1)
               {
                   c++;
                   //aqui eu marco se a bota encontrada
                   vestido[i] = 1;
                   vestido[j] = 1;
               }
           }
       }
   }
  printf("%d\n", c);
 }
}

I tested it and it was accepted, there must be several ways to improve code and decrease computing.

    
28.12.2017 / 14:21