#include <iostream>
using namespace std;
int main()
{
int cards[13] = {101, 113, 303, 206, 405, 208, 311, 304, 410, 309, 112, 207, 402};
for(int i = 1; i < 13; i++) //枚举每张待插入的牌
{
int target = cards[i], min = 500, pos = -1;
for(int j = 0; j < i; j++)
if(cards[j] > target)
if(cards[j] < min)
{
min = cards[j];
pos = j; //找到比待插入的牌大的最小的牌的位置
} //改编自查找扑克牌最小值的核心代码
if(pos != -1)
{
for(int j = i; j > pos; j--) //枚举从空位置到找到的位置的每一个位置
cards[j] = cards[j - 1]; //把前一张牌依次往后挪
cards[pos] = target; //把待插入的牌插入找到的位置
}
}
for(int i = 0; i < 13; i++)
cout << cards[i] << '\t';
cout << endl;
return 0;
}
MOOC清华《程序设计基础》第4章:整理扑克牌(插入排序)
最新推荐文章于 2022-12-20 11:06:00 发布