好友
阅读权限 10
听众
最后登录 1970-1-1
本帖最后由 Zzdouble 于 2020-1-31 01:55 编辑
代码如下:
#include <stdlib.h>
#include <stdio.h>
// positionalInformation 记录数据位置信息的结构体
// @site 记录数组位置的指针
// @length 指针长度
typedef struct positionalInformation {
int* site;
int length;
}arrayPostion;
// freePositionalInformation 结构体指针内存释放函数
void freePositionalInformation(arrayPostion* q) {
free(q->site);
free(q);
}
// arraySecedentMax函数 比较数组项数本身与之后项数,得到第一个比本身大的值,没有的话返回本身项数,输入错误会提醒并返回0
// @nums 数组首地址
// @Number 数组项数
// @arrayLength 数组长度
int arraySecedentMax(int* nums, int number, int arrayLength) {
if (number > arrayLength - 2) {
printf("number > arrayLength - 2(This is not right!)");
return 0;
}
int loop = 0;
int max = *(nums + number);
while (loop != arrayLength - number) {
loop += 1;
if (max < *(nums + number + loop))
{
max = *(nums + number + loop);
return max;
}
}
return *(nums + number);
}
// getArrayMax 获取数组中最大值 数组长度请正确输入
// @nums 数组首地址
// @arrayLength 数组长度
int getArrayMax(int* nums, int arrayLength) {
int max = -32786;
int loop = 0;
while (loop != arrayLength)
{
if (*(nums + loop) > max)
max = *(nums + loop);
loop += 1;
}
return max;
}
// getSameNumOfLocations函数 获取数字为num在数组nums中的位置,如果数组中不存在这个数字或这个数只有一个那么返回NULL,
// 否则返回一个动态数组其中不包含第一个数
// @nums 数组首地址
// @Num 具体数字
// @arrayLength 数组长度
arrayPostion* getSameNumOfLocations(int* nums, int num, int arrayLength)
{
int counts = 0;
int number = 0;
arrayPostion* postion = NULL;
for (int i = 0; i < arrayLength; i++) {
if (*(nums + i) == num)
counts += 1;
}
if (counts < 2) {
return postion;
}
postion = (arrayPostion*)malloc(sizeof(arrayPostion));
if (postion == NULL) {
printf("postion 动态申请失败");
return NULL;
}
postion->site = (int*)malloc(counts * sizeof(int));
postion->length = counts;
if (postion->site == NULL) {
printf("postion->site 动态申请失败");
return NULL;
}
//counts = 0;
counts = 1;
for (int i = 0; i < arrayLength; i++) {
if (*(nums + i) == num) {
// counts += 1;
if (counts > 1) {
*(postion->site + counts - 2) = i;
}
counts += 1;
}
}
return postion;
}
// arraySort_1 对数组重新排序排序
// @nums 数组首地址
// @arrayLength 数组长度
// @site 重复数字的位置
void arraySort_1(int* nums, int arrayLength, int site) {
int loop = 0;
int record_num = 0;
while (loop != arrayLength - site - 1) {
record_num = *(nums + site + loop);
*(nums + site + loop) = *(nums + site + loop + 1);
*(nums + site + loop + 1) = record_num;
loop += 1;
}
}
// arraySort函数 根据arrayPostion* 结构体指针重新对数组指针排序
// @nums 数组首地址
// @arrayLength 数组长度
// @arrayPostion 重复数字的位置信息
void arraySort(int* nums, int arrayLength, arrayPostion* postion) {
int loop = 0;
int nextMax = 0;
int arrayMax = getArrayMax(nums, arrayLength);
while (loop != postion->length) {
if (loop == 0) {
nextMax = arraySecedentMax(nums, *(postion->site), arrayLength);
*(nums + *(postion->site)) = nextMax;
}
else {
*(nums + *(postion->site)) = arrayMax;
arraySort_1(nums, arrayLength, *(postion->site));
}
loop += 1;
}
freePositionalInformation(postion);
}
// 删除重复
// @nums 数组首地址
// @numsSize 数组字节大小
int removeDuplicates(int* nums, int arrayLength) {
int num = 0;
int length = arrayLength;
int loop = 0;
int arrayMax = *(nums + (length - 1));
// counst为1是因为最后的数不需要排序
int counst = 1;
arrayPostion* postion = NULL;
while (loop != length) {
if (arrayMax != *(nums + loop)) {
counst += 1;
postion = getSameNumOfLocations(nums, *(nums + loop), length);
if (postion != NULL) {
// 排序处理
arraySort(nums, length, postion);
}
}
loop += 1;
}
return counst;
}
int main()
{
int array[] = { 1,1,1,1,1,3,3,3,3,4,4,4,4,4,4,4,5,5,5,5,7,7,7,7,7,7,8,8,8,8,8,9,10,10,13,18,19,20,20,20,78,78,99,101,101,202,202,333,333,444};
//int array[] = { 1,1,2,3, };
int length = sizeof(array) / 4;
//int num = removeDuplicates(array, sizeof(array));
int number = removeDuplicates(array, length);
//int* q = (int*)malloc(20 * sizeof(int));
//*q = 1;
//*(q + 1) = 1;
//*(q + 2) = 1;
//*(q + 3) = 1;
//*(q + 4) = 1;
//*(q + 5) = 1;
//*(q + 6) = 2;
//*(q + 7) = 2;
//*(q + 8) = 2;
//*(q + 9) = 3;
//*(q + 10) = 3;
//*(q + 11) = 3;
//*(q + 12) = 4;
//*(q + 13) = 4;
//*(q + 14) = 5;
//*(q + 15) = 6;
//*(q + 16) = 7;
//*(q + 17) = 7;
//*(q + 18) = 7;
//*(q + 19) = 8;
//number = removeDuplicates(q, 20);
//for (int i = 0; i < 20; i++)
//{
// printf("%d", *(q + i));
//}
printf("hello world");
}