Quaisquer perguntas de recém-chegados sobre MQL4 e MQL5, ajuda e discussão sobre algoritmos e códigos - página 143

 
fxsaber:
Funcionou como está.
template <typename T>
int Strange( const T &InArray[] )
{
  int Res = 1;  
  
  T Array[];
  
  const int Size = ArraySize(InArray);
  
  if ((ArrayCopy(Array, InArray) == Size) && ArraySort(Array))
  {    
    int Tmp = 1;
    
    ArrayPrint(Array);
    
    for (int i = 1; i < Size; i++)
    {
      if (Array[i - 1] != Array[i])
      {
        if (Tmp > Res)
          Res = Tmp;
        
        Tmp = 0;
      }
        
      Tmp++;
    }
  }

  return(Res);
}

void OnStart()
{
  int Array[] = {1, 2, 3, 1, 2, 1, 2, 2};
  
  Print(Strange(Array));
}
Funciona.

Isto é o que ele dá:

2017.03.07 14:42:44.141 1 1 1 2 2 2 2 3
2017.03.07 14:42:44.141 4

A linha superior é a matriz de entrada classificada, a linha inferior é o resultado de Strange().

Se olharmos cuidadosamente a matriz ordenada, e marcarmos com a cor dos fósforos, e depois contarmos o número de números mais de um com a mesma cor, obtemos o resultado:

1 1 1 2 2 2 2 3- total , você deveria ter retornado 2

E a função retornou 4. Embora - também progredir - comigo sempre retornou 1 - como assim - não entendeu.

 
Artyom Trishkin:

1 1 1 2 2 22 3 - no total , deveria ter retornado 2

E a função retornou 4. Embora - também progredir - comigo sempre retornou 1 - como assim - não entendeu.

Você precisa devolver o número ou o elemento mais freqüente? Se o segundo, então
template <typename T>
int Strange( const T &InArray[] )
{
  int Res = 0;
  
  T Array[];
  
  const int Size = ArraySize(InArray);
  
  if ((ArrayCopy(Array, InArray) == Size) && ArraySort(Array))
  {    
    int Max = 0;
    int Tmp = 0;
    
    ArrayPrint(Array);
    
    for (int i = 0; i < Size - 1; i++)
    {
      Tmp++;
      
      if ((Array[i] != Array[i + 1]) || (i == Size - 2))
      {
        if (Tmp > Max)
        {
          Max = Tmp;
          
          Res = Array[i];
        }
        
        Tmp = 0;
      }        
    }
  }

  return(Res);
}

void OnStart()
{
  int Array[] = {1, 2, 3, 1, 2, 1, 2, 2, 1, 1, 1, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3};
  
  Print(Strange(Array));
}
 
fxsaber:
Você quer devolver o número ou o item mais freqüente?

O número de diferentes tomadas.

Qual é o retorno de sua função? O maior número de partidas? Isso também é uma coisa útil...

 
Artyom Trishkin:

O número de diferentes tomadas.

Você o formula de forma estranha. "0, 0, 1, 1, 2, 2" - 3?

Qual é o retorno de sua função? O maior número de partidas?

Sim.
 
fxsaber:

Você o formula de forma estranha. "0, 0, 1, 1, 2, 2" - 3?

Sim - três correspondências de três números diferentes.
 
Artyom Trishkin:
Sim - três correspondências de três números diferentes.
template <typename T>
int Strange( const T &InArray[] )
{
  int Res = 0;  
  
  T Array[];
  
  const int Size = ArraySize(InArray);
  
  if ((ArrayCopy(Array, InArray) == Size) && ArraySort(Array))
  {    
    int Tmp = 1;
    
    ArrayPrint(Array);
    
    for (int i = 1; i < Size; i++, Tmp++)
      if (Array[i - 1] != Array[i])
      {
        if (Tmp > 1)
          Res++;
        
        Tmp = 0;
      }
      
    if (Tmp > 1)    
      Res++;
  }

  return(Res);
}

void OnStart()
{
  int Array[] = {1, 2, 3, 1, 2, 1, 2, 2, 3, 4, 4};
  
  Print(Strange(Array));
}
 
fxsaber:
Então, você quer devolver o número ou o item mais freqüente? Se este último, então
template <typename T>
int Strange( const T &InArray[] )
{
  int Res = 0;
  
  T Array[];
  
  const int Size = ArraySize(InArray);
  
  if ((ArrayCopy(Array, InArray) == Size) && ArraySort(Array))
  {    
    int Max = 0;
    int Tmp = 0;
    
    ArrayPrint(Array);
    
    for (int i = 0; i < Size - 1; i++)
    {
      Tmp++;
      
      if ((Array[i] != Array[i + 1]) || (i == Size - 2))
      {
        if (Tmp > Max)
        {
          Max = Tmp;
          
          Res = Array[i];
        }
        
        Tmp = 0;
      }        
    }
  }

  return(Res);
}

void OnStart()
{
  int Array[] = {1, 2, 3, 1, 2, 1, 2, 2, 1, 1, 1, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3};
  
  Print(Strange(Array));
}

Legal. Mas um pouco diferente - é uma função de modelo, certo:

template <typename T>
T Strange( const T &InArray[] )
{
  T Res = 0;
  
  T Array[];
  
  const int Size = ArraySize(InArray);
  
  if ((ArrayCopy(Array, InArray) == Size) && ArraySort(Array))
  {    
    int Max = 0;
    int Tmp = 0;
    
    ArrayPrint(Array);
    
    for (int i = 0; i < Size - 1; i++)
    {
      Tmp++;
      
      if ((Array[i] != Array[i + 1]) || (i == Size - 2))
      {
        if (Tmp > Max)
        {
          Max = Tmp;
          
          Res = Array[i];
        }
        
        Tmp = 0;
      }        
    }
  }

  return(Res);
}

void OnStart()
{
  double Array[] = {1, 2, 3, 1, 2, 1, 2, 2, 1, 1, 1, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3};
  
  Print(Strange(Array));
}
 
fxsaber:
template <typename T>
int Strange( const T &InArray[] )
{
  int Res = 0;  
  
  T Array[];
  
  const int Size = ArraySize(InArray);
  
  if ((ArrayCopy(Array, InArray) == Size) && ArraySort(Array))
  {    
    int Tmp = 1;
    
    ArrayPrint(Array);
    
    for (int i = 1; i < Size; i++)
    {
      if (Array[i - 1] != Array[i])
      {
        if (Tmp > 1)
          Res++;
        
        Tmp = 0;
      }
        
      Tmp++;
    }
  }

  return(Res);
}

void OnStart()
{
  int Array[] = {1, 2, 3, 1, 2, 1, 2, 2};
  
  Print(Strange(Array));
}

O que este retorna?

Se você fizer a matriz diferente, ela não é correta.

template <typename T>
int Strange( const T &InArray[] )
{
  int Res = 0;  
  
  T Array[];
  
  const int Size = ArraySize(InArray);
  
  if ((ArrayCopy(Array, InArray) == Size) && ArraySort(Array))
  {    
    int Tmp = 1;
    
    ArrayPrint(Array);
    
    for (int i = 1; i < Size; i++)
    {
      if (Array[i - 1] != Array[i])
      {
        if (Tmp > 1)
          Res++;
        
        Tmp = 0;
      }
        
      Tmp++;
    }
  }

  return(Res);
}

void OnStart()
{
  int Array[] = {1, 2, 3, 1, 2, 1, 2, 2, 1, 1, 1, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3};
  
  Print(Strange(Array));
}

Ele retorna.

2017.03.07 15:48:26.985 1 1 1 1 1 1 2 2 2 2 3 3 3 3 3 3 3 3 3 3 3 3 3
2017.03.07 15:48:26.985 2

Está procurando exatamente o número de números correspondentes na matriz? Ou talvez você tenha feito algo diferente, e eu estarei procurando por um erro

 

Eu já estou confuso)

Você tem uma matriz:

Array[] = {1, 2, 3, 1, 2, 1, 2, 2, 1, 1, 1, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3};

O que a função deve eventualmente retornar e por quê? O número de correspondências para cada número, ou um número máximo específico, ou ... Escrevi alguma coisa, mas deve estar errada, e errada?

 
Artyom Trishkin:

Está procurando exatamente o número de números correspondentes na matriz? Ou talvez você tenha feito algo mais e eu esteja procurando por um erro.

Você verificou o código antigo. Verifique novamente.
Razão: