//第一步对输入的字符串转换成整形数组
//对整形数组进行选择或者冒泡排序
//按要求写出打印后排序数组的方法
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include <string>
using namespace std;
void CharToInt(char * Cards, int *IntCard)
{
char CardIndex[14] = { '0','2', '3','4','5', '6', '7', '8', '9', '10', '11', '12', '13', '14' };
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 14; j++)
{
if (Cards[i] == CardIndex[j])
{
IntCard[i] = j;
}
}
}
}
void Sort(int * IntCard, int len)
{
for (int i = 0; i < len; i++)
{
int max = i;
for (int j = i + 1; j < len; j++)
{
if (IntCard[j] > IntCard[max])
{
max = j;
}
}
if (max != i)
{
int temp = IntCard[i];
IntCard[i] = IntCard[max];
IntCard[max] = temp;
}
}
}
void Print(int *IntCard, int len)
{
for (int i = 0; i < len; i++)
{
if (IntCard[i] == IntCard[i + 1])
{
if (IntCard[i] == 11)
{
cout << 'J' << ',' << 'J' << ',';
}
else if (IntCard[i] == 12)
{
cout << 'Q' << ',' << 'Q' << ',';
}
else if (IntCard[i] == 13)
{
cout << 'K' << ',' << 'K' << ',';
}
else if (IntCard[i] == 12)
{
cout << 'A' << ',' << 'A' << ',';
}
else
{
cout << IntCard[i] << ',' << IntCard[i] << ',';
}
}
}
for (int i = 0; i < len; i++)
{
if (IntCard[i] != IntCard[i + 1])
{
if (IntCard[i] == 11)
{
cout << 'J' << ',';
}
else if (IntCard[i] == 12)
{
cout << 'Q' << ',';
}
else if (IntCard[i] == 13)
{
cout << 'K' << ',';
}
else if (IntCard[i] == 12)
{
cout << 'A' << ',';
}
else
{
cout << IntCard[i] << ',';
}
}
}
cout << endl;
}
void test()
{
char Cards[5] = { 0 };
int IntCard[5] = { 0 };
int i = 0;
printf("Please Input One Card:\n");
while (i < 5)
{
scanf("%c", &Cards[i]);
i++;
}
//首先把字符数组转换成整形数组
CharToInt(Cards, IntCard);
//对整形数组进行从大到小的排序
Sort(IntCard, 5);
//按需求规则对排序后的整形数组进行打印操作
Print(IntCard, 5);
}
int main()
{
test();
system("pause");
return EXIT_SUCCESS;
}
面试题
最新推荐文章于 2021-11-27 20:55:45 发布