如何在数组中查找特定值的频率? 我是一个C++编程新手,我在这本书中被Alex Allain称为跳进C++,书中有一个TIC TAC趾练习,我很难完成这项练习,到目前为止我所做的是提示用户输入X或O值,这些值存储在2D数组中,例如,我想跟踪char X是否在数组中出现了3次,到目前为止,我使用的是counter++但它只增加了一次。下面是我到目前为止所做工作的来源,希望它能让我的问题更清楚,并让我知道我在代码外观方面做得如何,比如它的结构和功能: #include "stdafx.h" #include "iostream" #include "string" using namespace std; void display_array(char array[][3]); void check_input(char array[][3], int input); void check_winner(char array[][3]); int check_x(char array[][3]); int check_o(char array[][3]); int _tmain(int argc, _TCHAR* argv[]) { char array[3][3]; int counter = 0; for(int row = 0; row < 3; row++){ cout << "\n"; for(int col = 0; col < 3; col++){ counter++; array[row][col] = counter; } } display_array(array); system("PAUSE"); return 0; } void display_array(char array[][3]){ int position_input; string symbol_input; do{ for(int i=0; i < 3; i++){ for(int j=0; j < 3; j++){ cout << " [ "; cout << array[i][j]; cout << " ] "; } cout << "\n"; } cout << "Position: "; cin >> position_input; check_input(array, position_input); }while(position_input != 0); } void check_input(char array[][3], int input) { char user_input = input; for(int i = 0; i < 3; i++) { for(int j = 0; j < 3; j++) { if(user_input == array[i][j]) { cout << "array[" << i << "][" << j << "] replace with: "; cin >> array[i][j]; } } } check_winner(array); } void check_winner(char array[][3]){ cout << check_x(array); if(check_x(array) == 3){ cout << check_x(array); } else if(check_o(array) == true){ cout << "o"; } } int check_x(char array[][3]){ int counter, x[3]; counter = 0; for(int i = 0; i < 3; i++){ for(int j = 0; j < 3; j++){ if(array[i][j] == array[i][j]){ counter++; } x[i] = counter; return x[i]; } } } int check_o(char array[][3]){ int counter; counter = 0; for(int i = 0; i < 3; i++){ for(int j = 0; j < 3; j++){ if(array[i][j] == 'o'){ counter++; return counter; }else{ return counter; } } } } #包括“stdafx.h” #包括“iostream” #包括“字符串” 使用名称空间std; 无效显示_数组(字符数组[][3]); 无效检查_输入(字符数组[][3],int输入); 无效检查(字符数组[][3]); int check_x(字符数组[][3]); int check_o(字符数组[][3]); int _tmain(int argc,_TCHAR*argv[] { 字符数组[3][3]; int计数器=0; 对于(int行=0;行

如何在数组中查找特定值的频率? 我是一个C++编程新手,我在这本书中被Alex Allain称为跳进C++,书中有一个TIC TAC趾练习,我很难完成这项练习,到目前为止我所做的是提示用户输入X或O值,这些值存储在2D数组中,例如,我想跟踪char X是否在数组中出现了3次,到目前为止,我使用的是counter++但它只增加了一次。下面是我到目前为止所做工作的来源,希望它能让我的问题更清楚,并让我知道我在代码外观方面做得如何,比如它的结构和功能: #include "stdafx.h" #include "iostream" #include "string" using namespace std; void display_array(char array[][3]); void check_input(char array[][3], int input); void check_winner(char array[][3]); int check_x(char array[][3]); int check_o(char array[][3]); int _tmain(int argc, _TCHAR* argv[]) { char array[3][3]; int counter = 0; for(int row = 0; row < 3; row++){ cout << "\n"; for(int col = 0; col < 3; col++){ counter++; array[row][col] = counter; } } display_array(array); system("PAUSE"); return 0; } void display_array(char array[][3]){ int position_input; string symbol_input; do{ for(int i=0; i < 3; i++){ for(int j=0; j < 3; j++){ cout << " [ "; cout << array[i][j]; cout << " ] "; } cout << "\n"; } cout << "Position: "; cin >> position_input; check_input(array, position_input); }while(position_input != 0); } void check_input(char array[][3], int input) { char user_input = input; for(int i = 0; i < 3; i++) { for(int j = 0; j < 3; j++) { if(user_input == array[i][j]) { cout << "array[" << i << "][" << j << "] replace with: "; cin >> array[i][j]; } } } check_winner(array); } void check_winner(char array[][3]){ cout << check_x(array); if(check_x(array) == 3){ cout << check_x(array); } else if(check_o(array) == true){ cout << "o"; } } int check_x(char array[][3]){ int counter, x[3]; counter = 0; for(int i = 0; i < 3; i++){ for(int j = 0; j < 3; j++){ if(array[i][j] == array[i][j]){ counter++; } x[i] = counter; return x[i]; } } } int check_o(char array[][3]){ int counter; counter = 0; for(int i = 0; i < 3; i++){ for(int j = 0; j < 3; j++){ if(array[i][j] == 'o'){ counter++; return counter; }else{ return counter; } } } } #包括“stdafx.h” #包括“iostream” #包括“字符串” 使用名称空间std; 无效显示_数组(字符数组[][3]); 无效检查_输入(字符数组[][3],int输入); 无效检查(字符数组[][3]); int check_x(字符数组[][3]); int check_o(字符数组[][3]); int _tmain(int argc,_TCHAR*argv[] { 字符数组[3][3]; int计数器=0; 对于(int行=0;行,c++,C++,无论被测试的元素是否为“o”,在一个循环后都将返回。将其更改为以下内容: for(int i = 0; i < 3; i++){ for(int j = 0; j < 3; j++){ if(array[i][j] == 'o') counter++; } } return counter; for(int i=0;i

无论被测试的元素是否为“o”,在一个循环后都将返回。将其更改为以下内容:

for(int i = 0; i < 3; i++){
    for(int j = 0; j < 3; j++){
        if(array[i][j] == 'o')
            counter++;
    }
}

return counter;
for(int i=0;i<3;i++){
对于(int j=0;j<3;j++){
if(数组[i][j]='o')
计数器++;
}
}
返回计数器;
我的答案:

首先是问题的最后一部分:代码看起来不错,如果您对一些操作进行了评论,我会很高兴,即使程序非常引人注目(//**/)

在我看来,你的问题在解决练习方面有点误导。我一开始也以同样的方式想到了你的方法。问题是,模式不只是以3的频率被检测到,因为它会允许错误的检查模式。要使其可视化:

789
4 5 6
1 2 3
f、 e.频率为3,如1,2,5或9 6 5不是正确答案,但-1 5 9是正确答案。即使你计算每个频率模式,它也比我在解决方案的以下博客中定义的解决方案编码得多:

status_check=可能性(场,玩家,2,0,1,1,0,2);//对角线1 5 9
状态检查=可能性(场,球员,0,0,1,1,2,2);//对角线7 5 3
状态检查=可能性(场,玩家,0,0,1,0,2,0);//垂直7 4 1
状态检查=可能性(场,玩家,0,1,1,2,1);//垂直8 5 2
状态检查=可能性(场,玩家,0,2,1,2,2);//垂直9 6 3
状态检查=可能性(场,玩家,0,0,0,1,0,2);//水平7 8 9
状态检查=可能性(场,球员,1,0,1,1,1,2);//水平4 5
状态检查=可能性(场,玩家,2,0,2,1,2,2);//水平1 2 3
*这就是我如何利用num-pad进行训练的。(我真的为这段代码感到骄傲。:-)

顺便说一句,我喜欢那本书,但遗憾的是,我想不出那本: “奖金:你能让你的程序检测到游戏是否不能赢吗?” 在填充整个网格之前,在任意一侧?” 但如果有人跳过这一章,这个解决方案肯定会有所帮助:*

练习如下(第121页,跳转到Cpp):

编写一个小的tic-tac-toe程序,允许两个玩家玩 你的程序应该检查一下 玩家已经赢了,或者如果棋盘上的棋子已经满了(游戏) 奖金:你能让你的程序检测到游戏 在整个网格填满之前,任何一方都无法获胜

下面的代码可以用C++ 17标准简单编译和执行,所有的库都可以使用GCC(GCC)82.1

#包括
#包括
#包含//仅用于输入验证
使用名称空间std;
int输入检查(字符字段[3][3],字符播放器);
无效显示字段(字符字段[3][3],整数大小);
无效初始化_字段(字符字段[3][3],整数大小,字符播放器);
字符串win_检查(字符字段[3][3],字符播放器,整数大小);
int代码(字符字段[3][3],int i,int j,字符播放器);
int可能性(字符字段[3][3],字符播放器,int a,int aa,int b,int bb,int c,int cc);
int main()
{
字符字段[3][3];
int size=3;
字符播放器='-';
初始化_字段(字段、大小、播放器);
显示_字段(字段、大小);
while(字段[0][0]!='w')
{
玩家='X';
输入检查(场、球员);
系统(“清除”);
显示_字段(字段、大小);

请记住C++与C和C是不同的,它们的名称是混淆的,但是它们的区别是明显的,程序员不能帮助其他人。我知道这一点,我很抱歉。你的主要问题是你似乎不知道范围和乐趣。对话是有效的。是的,但人不是马。通常他们在跑步前必须学会走路。我没有检查答案是否正确。但即使代码是正确的,在我看来,它仍然忽略了问答平台的意义,因为答案应该解释OP在显示的代码中做错了什么。你至少应该告诉自己为什么这样做nk您的解决方案比OP解决问题的方法更好。感谢您的回复。正确的观点-我很兴奋发布我的第一个答案,我忘记了最重要的部分。我编辑了它,谢谢。