// paixu.cpp : 定义控制台应用程序的入口点。
//
/*
本算法是对一个数组进行排序,类似我们打牌时抓到的一把牌进行排序
比如假设1 最小 13最大 单张牌按大小排列 对子比单大要排在前面 三张比两张大
如果手上的牌是1,2,3,2,5,3,2
最终的排序是 2,2,2,3,3,5,1
*/
#include "stdafx.h"
#include <stdio.h>
#include <algorithm>
//牌的内容
int num[] = { 1,2,4,6,8,7,7,8,9,12,11,11,11,13,13,6,6,6 };
//int num_sort[6] = { 1,1,2,3,4,4 };
int s_num_index = -1;
struct s_num
{
int num;
int len;
};
int Comp(const void *p1, const void *p2)
{
return (*(int*)p2 - *(int * )p1 );
}
/*
int Comp_s(const void *p1, const void *p2)
{
//return ((s_num*)p2)->len - (s_num*)p1)->len);
return ((int *)p2 - (int *)p1);
}
*/
struct s_num data_num[20];
/* 把数组中的数据和长度信息提取出来 放到结构体数组中 */
void ini_data_num()
{
int start = -1;
bool start_flag = 0;
int current_num = -1;
int len = 0;
int maxlen = 0;
int start_pos = -1;
//int length;
int j = 0;
bool ini_flag = 1;
for (int i = 0; i < sizeof(num)/sizeof(int); i++)
{
if (!start_flag )
{
if (current_num != num[i])
{
if (ini_flag)
{
current_num = num[i];
maxlen = 1;
len = 1;
start_pos = i;
//start_flag = 1;
ini_flag = 0;
}
else
{
if (i == (sizeof(num) / sizeof(int) - 1))
{
printf("num[%d]:%d\n",i,num[i]);
int num_temp = current_num;
data_num[j].len = len;
data_num[j].num = num_temp;
j++;
data_num[j].len = 1;
data_num[j].num = num[i];
}
else
{
int num_temp = current_num;
current_num = num[i];
if (len > maxlen)
{
maxlen = len;
}
data_num[j].len = len;
data_num[j].num = num_temp;
j++;
start_pos = i;
//start_flag = 1;
len = 1;
}
}
}
else
{
len++;
if (len > maxlen)
{
maxlen = len;
}
if (i == (sizeof(num) / sizeof(int) - 1))
{
int num_temp = current_num;
data_num[j].len = len;
data_num[j].num = num_temp;
j++;
}
}
}
}
for (int i = 0; i <= j; i++)
{
printf("data_num[%d].num:%d,data_num[%d].len:%d\n",i,data_num[i].num, i, data_num[i].len);
//system("pause");
}
// system("pause");
s_num_index = j +1;
}
//交换两个结构体
void swap(s_num * p1, s_num *p2)
{
s_num temp;
temp = *p1;
*p1 = *p2;
*p2 = temp;
}
void sort_data_num_len()
{
printf("s_num_index :%d\n", s_num_index);
//冒泡排序法
for (int i = 0; i < s_num_index -1 ; i++)
{
for (int j = 0; j < s_num_index - i - 1; j++)
{
if ((data_num[j].len < data_num[j + 1].len)) //&& (data_num[j].num < data_num[j + 1].num))
{
swap(&data_num[j], &data_num[j + 1]);
}
}
}
//打印最终的结果
for (int i = 0; i < s_num_index; i++)
{
for (int j = 0; j < data_num[i].len; j++)
{
printf("data_num[%d]:%d\n", j,data_num[i].num);
}
}
}
int main()
{
//首先对数组进行Qsort 排序
qsort(num, sizeof(num)/ sizeof(num[0]), sizeof(num[0]), Comp);
for (int i = 0; i < sizeof(num) / sizeof(num[0]); i++)
{
printf("num[%d]:%d\n",i,num[i]);
}
//qsort(num, sizeof(num) / sizeof(num[0]), sizeof(num[0]), Comp);
//把排序后的数组的每个长度和数值提取出来 放到一个结构体数组里
ini_data_num();
//按长度对结构体数组进行排序,后面用冒泡排序法对结构体数组进行按结构体内部的长度变量进行排序
sort_data_num_len();
//qsort(data_num,sizeof(data_num)/ sizeof(data_num[0]), sizeof(data_num[0]),Comp_s)
system("pause");
return 0;
}