分类玩家输入
Sorting Players Input
我将如何对播放器输入进行分类并以最低形式到最高形式输出?我正在看泡沫方法,永远无法做到这一点,想知道是否有一种更简单的方法?
感谢提前的帮助。
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]
进行排序。从这个循环不变,我们可以证明此算法是正确的。
我提供的链接中有一个实现。
相关文章:
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- 2D数组来自文本输入,中间有空格
- 如何使用 < 和 > 命令获取 c++ 中的输入和输出?
- 检查输入是否不是整数或数字
- 正在尝试了解输入验证循环
- 读取文件并输入到矢量中
- C++如何通过用户输入删除列表元素
- 用c++从输入文件中读取另一行
- 读取文件的最后一行并输入到链接列表时出错
- 创建一个函数以在输入为负数或零时输出字符串.第一次执行用户定义的函数
- 如何使用用户输入在C++中正确填充2D数组
- C++MySQL C api用户输入行
- 输入到文件并输出到另一个文件,并将流文件传递给函数
- 用户定义函数中的指针和输入
- 如何在C++中检查2D数组中负值的输入验证
- C++ 如何处理所有可能的玩家移动输入?
- 接受玩家输入
- 分类玩家输入
- 多个玩家的键盘输入问题
- 将玩家输入与单词文件进行比较