(c++)对数组中的重复项进行计数,然后将被计数的数字移动到另一个数组中的相应位置

(C++) Counting duplicates in an array, and then moving that counted number to a corresponding position in a separate array

本文关键字:数组 移动 数字 另一个 位置 然后 c++      更新时间:2023-10-16

我刚刚开始学习编写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] ++;
}