本文发表在 rolia.net 枫下论坛#include <stdio.h>
int print_array(int *input, int number)
{
int i;
printf("number = %d [", number);
for(i=0; i<number; i++)
{
printf("%d ", *input);
input++;
}
printf("]\n");
}
int re_order(int *input, int number)
{
int i;
int final_number = 0;
int available_position = 0;
int offset = 0;
if (number <= 1)
return number;
final_number = 1;
available_position++;
offset++;
for(i=1; i<number; i++)
{
if (*(input+offset) == *(input+available_position-1))
{
*(input+offset) = 0;
offset++;
}
else
{
final_number++;
*(input+available_position) = *(input+offset);
available_position++;
offset++;
}
}
return final_number;
}
int main()
{
// int int_array[]={};
// int int_array[]={0};
// int int_array[]={0, 0};
// int int_array[]={0, 0, 2};
int int_array[]={0, 0, 0, 2, 3, 3, 5, 5, 5, 7, 9, 9};
int new_number;
print_array(&int_array[0], sizeof(int_array)/sizeof(int));
new_number = re_order(&int_array[0], sizeof(int_array)/sizeof(int));
print_array(&int_array[0], new_number);
}更多精彩文章及讨论,请光临枫下论坛 rolia.net
int print_array(int *input, int number)
{
int i;
printf("number = %d [", number);
for(i=0; i<number; i++)
{
printf("%d ", *input);
input++;
}
printf("]\n");
}
int re_order(int *input, int number)
{
int i;
int final_number = 0;
int available_position = 0;
int offset = 0;
if (number <= 1)
return number;
final_number = 1;
available_position++;
offset++;
for(i=1; i<number; i++)
{
if (*(input+offset) == *(input+available_position-1))
{
*(input+offset) = 0;
offset++;
}
else
{
final_number++;
*(input+available_position) = *(input+offset);
available_position++;
offset++;
}
}
return final_number;
}
int main()
{
// int int_array[]={};
// int int_array[]={0};
// int int_array[]={0, 0};
// int int_array[]={0, 0, 2};
int int_array[]={0, 0, 0, 2, 3, 3, 5, 5, 5, 7, 9, 9};
int new_number;
print_array(&int_array[0], sizeof(int_array)/sizeof(int));
new_number = re_order(&int_array[0], sizeof(int_array)/sizeof(int));
print_array(&int_array[0], new_number);
}更多精彩文章及讨论,请光临枫下论坛 rolia.net