分类玩家输入

Sorting Players Input

本文关键字:输入 玩家 分类      更新时间:2023-10-16

我将如何对播放器输入进行分类并以最低形式到最高形式输出?我正在看泡沫方法,永远无法做到这一点,想知道是否有一种更简单的方法?

感谢提前的帮助。

void game(const bool& type)
{
short *playerPicks,  // numbers chosen by the player
*randPicks;  // random numbers (winning picks)
float counter = 1.0f;    // counter of number of tries
bool win =true;  // tracks win condition
playerPicks = makePicks(LOTTO_SIZE);
cout << endl
    << "You've chosen: ";
for (short i = 0; i < LOTTO_SIZE; i++)
    cout << playerPicks[i] << " ";
_getch();
{
    while (!win)
    {
        randPicks = makePicksRand(LOTTO_SIZE);
        cout << "nTry " << counter << ": ";
        for (short i = 0; i < LOTTO_SIZE; i++)
        cout << randPicks[i] << " ";
        cout << endl;
        win = checkWin(playerPicks, randPicks, type);
        counter++;
        delete[] randPicks;
    }
}
}

short* makePicks(const short& size)
{
short *temp = new short[size];
bool repeat = false;
cout << "Pick your first five numbers.n" 
     << "Choices must be from 1-55, and may not repeatn";
for (short i = 0; i < LOTTO_SIZE;)
{
    if ((i == 5) && (!repeat))
    {
        cout << "Now, pick your powerball number.n" 
            << "Choice must be from 1-42, and mayn"
            << "repeat any previous pick.n";
        repeat = true;
    }
    cout << "Pick " << (i + 1) << ": ";
    cin >> temp[i];
    if (!cin)
    {
        cin.clear();
        cin.ignore();
        cout << "Invalid input.n";
    }
    else
    {
        if (validate(i, temp))
            i++;
        else
            cout << "Pick " << (i + 1) << " conflicts with a previous n"
                 << "choice or is invalid.n";
    }
}
return temp;
}

我建议您使用C 标准库的std::sort算法。

#include <algorithm>
std::sort(playerPicks, playerPicks + LOTTO_SIZE);

此功能将两个迭代器带到您要排序的范围。请注意,该范围应该是数据和的末端的启动的迭代器。在这种情况下,指针是可接受的随机访问迭代器。

如果您对我的第一个示例中的地址算术不满意,也可以使用等价:

std::sort(&playerPicks[0], &playerPicks[LOTTO_SIZE]);

只是使用std::sort()。这很简单。

#include "stdafx.h"
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
  const int LOTTO_SIZE = 5;
  short unsorted_number[LOTTO_SIZE] = {5, 12, 3, 45,22};
  cout << "unsorted_number" << endl;
  for_each(std::begin(unsorted_number), std::end(unsorted_number), [&](const short& number)
  {
      cout << number << ", ";
  });
  cout << endl << endl;
  //Here, 'unsorted_number' is sorted.
  std::sort(std::begin(unsorted_number), std::end(unsorted_number));
  cout << "sorted_number" << endl;
  for_each(std::begin(unsorted_number), std::end(unsorted_number), [&](const short& number)
  {
      cout << number << ", ";
  });
  getchar();
  return 0;
}

我将建议使用qsort

定义比较短裤的函数。在参数中使用它到标准库函数qsort

int pickCompare(void* o1, pickCompare* o2)
{
   return (*(short*)o1 - (*short*)o2);
}
qsort(playerPicks, LOTTO_SIZE, sizeof(short), pickCompare); 

我不确定如何实现气泡排序,但是如果您的速度很好(O(n^2)),请查看选择排序。基本思想如下,对于数组中的每个元素a [i],找到从a[i]a[length-1]的最小元素。如果最小的元素是a[i],则完成了。如果min!=a[i],请交换它们。请注意,每次迭代后,我们将a[0]a[i-1]进行排序。从这个循环不变,我们可以证明此算法是正确的。

我提供的链接中有一个实现。