(c++)对数组中的重复项进行计数,然后将被计数的数字移动到另一个数组中的相应位置
(C++) Counting duplicates in an array, and then moving that counted number to a corresponding position in a separate array
我刚刚开始学习编写c++代码,所以请原谅我,我的知识非常非常有限。这是我从头开始编写的第一个程序。我正在编写一个程序,它查看一副五张牌,然后告诉用户它的类型,比如同花顺、直牌等。
首先,我让用户输入他们的手,然后将该手存储到一个数组中。
然后我需要做的是在数组中搜索重复项(例如数组中有两个3),计算重复项的数量(在本例中为2),然后将数字2放入另一个数组中对应的卡的位置。
例如:用户的手牌是S11H11D11C11S10 (S -黑桃,H -红心,D-方块,C -梅花)这里的数字11代表j, a是第1级,k是第13级。
用户手上有4张j,黑桃10。
我需要程序计算这四个j,然后在第11个位置放入另一个数组。这将告诉我用户有四张牌,并且它们是j。
你可以在这里查看我的代码:
头:#ifndef SUIT_H_INCLUDED
#define SUIT_H_INCLUDED
#include <string>
using namespace std;
struct Card {
int rank1;
int rank2;
int rank3;
int rank4;
int rank5;
char suit1;
char suit2;
char suit3;
char suit4;
char suit5;
};
#endif // SUIT_H_INCLUDED
主要功能:
#include <iostream>
#include <string>
#include "suit.h"
using namespace std;
int main()
{
int usrhand[10];
char a; //letters for declaring cards in hand
char b;
char c;
char d;
char e;
int f; //numbers for declaring rank of cards
int g;
int h;
int i;
int j;
Card hand; //initiates a structure for the players hand
hand.suit1 = a;
hand.suit2 = b;
hand.suit3 = c;
hand.suit4 = d;
hand.suit5 = e;
hand.rank1 = f;
hand.rank2 = g;
hand.rank3 = h;
hand.rank4 = i;
hand.rank5 = j;
cout << "Welcome to Card Sorter (TM) designed by ----" << endl;
cout << " " <<endl;
cout << "You will be prompted to enter your 5 card hand." <<endl;
cout << "I will prompt you for each card, one by one." <<endl;
cout << "I will ask you for the suit and then the rank of the card." <<endl;
cout << "Keep in mind: Spades=S, Hearts=H, Diamonds=D, Clubs=C" <<endl;
cout << "Also keep in mind the value of Ace is low, and is rank 1" <<endl;
cout << "While the value of a King is high and is rank 13." <<endl;
cout << "Let's begin." <<endl;
cout << " " << endl;
cout << "What is the suit of your first card (S,H,D,C)?" <<endl;
cin >> a;
cout << "What is the rank of your first card (1-13)" <<endl;
cin >> f;
cout << "What is the suit of your second card? (S,H,D,C)?" <<endl;
cin >> b;
cout << "What is the rank of your second card (1-13)" <<endl;
cin >> g;
cout << "What is the suit of your third card? (S,H,D,C)?" <<endl;
cin >> c;
cout << "What is the rank of your third card (1-13)" <<endl;
cin >> h;
cout << "What is the suit of your fourth card? (S,H,D,C)?" <<endl;
cin >> d;
cout << "What is the rank of your fourth card (1-13)" <<endl;
cin >> i;
cout << "What is the suit of your final card? (S,H,D,C)?" <<endl;
cin >> e;
cout << "What is the rank of your final card (1-13)" <<endl;
cin >> j;
usrhand[0] = a;
usrhand[1] = f;
usrhand[2] = b;
usrhand[3] = g;
usrhand[4] = c;
usrhand[5] = h;
usrhand[6] = d;
usrhand[7] = i;
usrhand[8] = e;
usrhand[9] = j;
cout <<" Lets take a look at your current hand. " <<endl;
printf("%c", usrhand[0]);
printf("%d", usrhand[1]);
printf("%c", usrhand[2]);
printf("%d", usrhand[3]);
printf("%c", usrhand[4]);
printf("%d", usrhand[5]);
printf("%c", usrhand[6]);
printf("%d", usrhand[7]);
printf("%c", usrhand[8]);
printf("%d", usrhand[9]);
//need to count duplicate ranks in this array (ie. player has 4 cards of the same value, let's say value is 5)
// and then place the counted value in another array at the
// correct position, the position that matches the value in this case (so array2[4] = 4)
// Then I will program something to examine the second array for its number values and print out the players hand
// (ie. array2[4] = 4, player has four 5's this is a Four of a Kind)
return 0;
}
因为你只需要找到重复的排名,你可以这样做:
int dupCounter[13];
for (int i=0; i<13; i++)
{
dupCounter[i] = 0;
}
for (int i=0; i<5; i++)
{
dupCounter[usrhand[i*2+1]-1] ++;
}
相关文章:
- 移动二维数组中的字符
- 新的整数数组向右移动?
- 当 std::move 与 C 样式数组或不移动对象时会发生什么
- 在数组中输入 n 个整数的列表,并以类似于钟摆来回移动的方式排列它们. 输入-1 3 2 5 4,输出5 3 1 2 4
- 数组指针表示法C++(移动数组时)
- C++ - 块不移动,我的数组不起作用
- 如何将数组中的元素向右移动?
- 如何将不可移动和不可复制的函数返回值获取到数组中
- C++ - 移动具有固定大小的 c 样式数组成员的类的构造函数
- C++将值移动到数组的开头
- 在 c++ 中将数据从未对齐结构数组移动到对齐结构数组
- 尝试通过多个函数移动数组,但遇到了我不知道如何修复的错误
- 如何在C++中有效地将数组移动到向量
- 移动数组C++中的特定元素
- 将数组中的零移动到末尾
- 如何在C++中将 std::vector 移动到原始数组中
- 从Boost Multi_index数组移动元素
- C++数组移动到元素
- 弹出后使用数组>>移动元素排队
- 从 C 数组移动到 std::map<int, val> 运算符 '-' 陷阱