排序玩家输入

时间:2014-01-27 03:26:19

标签: c++ sorting

我如何排序玩家输入并以从最低到最高的形式输出?我正在研究泡沫方法,可能永远不会那么有效,并想知道是否有更简单的方法来做它?

感谢您的帮助。

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 repeat\n";
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 may\n"
            << "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;
}

4 个答案:

答案 0 :(得分:1)

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

#include <algorithm>

std::sort(playerPicks, playerPicks + LOTTO_SIZE);

此函数将两个迭代器带到您要排序的范围。请注意,范围应该是数据开始数据结束的迭代器。在这种情况下,指针是可接受的随机访问迭代器。

如果您对我的第一个示例中的地址算法不满意,您还可以使用等效项:

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

答案 1 :(得分:1)

请使用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;
}

答案 2 :(得分:1)

我建议使用qsort

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

int pickCompare(void* o1, pickCompare* o2)
{
   return (*(short*)o1 - (*short*)o2);
}

qsort(playerPicks, LOTTO_SIZE, sizeof(short), pickCompare); 

答案 3 :(得分:0)

我不完全确定如何实现冒泡排序,但如果您对慢速(O(n^2))感觉不错,请查看Selection sort。基本思想如下,对于数组中的每个元素a [i],找到从a[i]a[length-1]的最小元素。如果最小元素是a[i],那么你就完成了。如果min!=a[i],请交换它们。请注意,在每次迭代后,我们会对a[0]a[i-1]进行排序。从这个循环不变量我们可以证明这个算法是正确的。

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