面试题

//第一步对输入的字符串转换成整形数组
//对整形数组进行选择或者冒泡排序
//按要求写出打印后排序数组的方法

#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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值