Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

Diamantes e Areia (código não compila em C)

+10 votes
asked Jul 20 by paulo magalhaes (1,240 points)

 Diamantes e Areia: João está trabalhando em uma mina, tentando retirar o máximo que consegue de diamantes "<>". Ele deve excluir todas as particulas de areia "." do processo e a cada retirada de diamante, novos diamantes poderão se formar. Se ele tem como uma entrada .<...<<..>>....>....>>>., três diamantes são formados. O primeiro é retirado de <..>, resultando  .<...<>....>....>>>. Em seguida o segundo diamante é retirado, restando .<.......>....>>>. O terceiro diamante é então retirado, restando no final .....>>>., sem possibilidade de extração de novo diamante.

Entrada

Deve ser lido um valor inteiro N que representa a quantidade de casos de teste. Cada linha a seguir é um caso de teste que contém até 1000 caracteres, incluindo "<,>, ."

Saída

Você deve imprimir a quantidade de diamantes possíveis de serem extraídos em cada caso de entrada.

OBS – Deve ser utilizado pilha.

Eu fiz o codigo mas não esta dando certo. Quando eu coloco >< ele também conta como um diamante

Se alguém puder me ajudar. 

#include <cstdio>
#include <cstring>
int main ()
{
  int q, w, guaxima, greater, liryus;
  char lista[1001];

  scanf("%d", &q);

  while (q--) 
  {
    scanf("%s", lista);
    guaxima = greater = liryus =  0;
    for (w=0; w<strlen(lista); w++) 
    {
      if (lista[w] == '<') 
      {
        guaxima++;
      } 
      else 
        if (lista[w] == '>') 
        {
          if (guaxima > 0) 
          {
            liryus++;
            guaxima--;
          }
        }
    }
    printf("%d\n", liryus);
  }
  return 32768;
}

2 Answers

0 votes
answered Jul 20 by Peter Minarik (32,880 points)
edited Jul 24 by Peter Minarik

If I provide "><" to your code, it says 0, so I'm not sure why you say your program doesn't work correctly for this input.

Can you provide the actual input you used and what result you got?

Also, what's with the variable q? What is the purpose of that in your code? How many lines of input you want to proceed? It is a good idea in general to tell the user what input you're expecting them to provide. (printf)

Furthermore, why the return value is 32768? Typically a successful return code is 0.

One small thing is that you check for the length of list in every loop. If you are concerned about performance, you can calculate it and store it in a variable and use that in your loops condition instead. Just to save some time. ;)

One more thing: your code is C, but your using C++ style headers, so your code cannot be compiled by the C compiler. Use stdio.h and string.h instead to remain C compatible.

Alternatively, you can let go of the C library and use C++ libraries instead (iostream).

0 votes
answered Jul 23 by Héctor Murcia Forero (220 points)
The problen is generated by the spaces in the input. Change these by _ for example and it works fine!
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...