- 论坛徽章:
- 0
|
挑战数组腾挪大法
#include <stdio.h>;
#include <fcntl.h>;
#include <sys/errno.h>;
extern int errno;
#define MAX_SIZE 10
#define OBJECT_NUMBER 7
#define INT_EXCHANGE(a,b) \
{\
int temp_int; \
temp_int = my_array[a]; \
my_array[a] = my_array; \
my_array = temp_int; \
}
main ()
{
int my_array[MAX_SIZE] = {3,5,7,1,8,7,6,7,2,0};
int last_less_room, last_more_room,
object_current_room, temp_room;
for (last_less_room = 0, last_more_room = MAX_SIZE -1 ;
last_less_room < last_more_room;
{
{
int i;
for (i=0; i<MAX_SIZE; i++) printf ("%d ", my_array);
printf ("\n" ;
}
while (my_array[last_less_room] < OBJECT_NUMBER) last_less_room ++;
while (my_array[last_more_room] >; OBJECT_NUMBER) last_more_room --;
if (last_less_room >;= last_more_room)
{
break;
}
if (my_array[last_less_room] == my_array[last_more_room])
{
for (temp_room = last_less_room+1;
(temp_room<(last_more_room-1))
&& (my_array[temp_room] == OBJECT_NUMBER); temp_room++);
if (my_array[temp_room] == OBJECT_NUMBER)
{
break;
}
else if (my_array[temp_room] >; OBJECT_NUMBER)
{
INT_EXCHANGE (temp_room, last_more_room);
}
else if (my_array[temp_room] < OBJECT_NUMBER)
{
INT_EXCHANGE (temp_room, last_less_room);
}
continue;
}
if (my_array[last_less_room] == OBJECT_NUMBER)
{
object_current_room = last_less_room;
while (((last_less_room + 1) < (MAX_SIZE - 1))
&& (my_array[last_less_room + 1] < OBJECT_NUMBER))
{
last_less_room ++;
}
INT_EXCHANGE (last_less_room, object_current_room);
if (((last_less_room + 1) < MAX_SIZE)
&& (my_array[last_more_room] < OBJECT_NUMBER))
{
INT_EXCHANGE (last_less_room + 1, last_more_room);
}
}
if (my_array[last_more_room] == OBJECT_NUMBER)
{
object_current_room = last_more_room;
while (((last_more_room - 1) >; 0)
&& (my_array[last_more_room - 1] >; OBJECT_NUMBER))
{
last_more_room --;
}
INT_EXCHANGE (last_more_room, object_current_room);
if ((last_more_room >; 0)
&& (my_array[last_less_room] >; OBJECT_NUMBER))
{
INT_EXCHANGE (last_more_room - 1, last_less_room);
}
}
}
{
int i;
for (i=0; i<MAX_SIZE; i++) printf ("%d ", my_array);
printf ("\n" ;
}
}
输出的结果是:
3 5 7 1 8 7 6 7 2 0
3 5 1 7 0 7 6 7 2 8
3 5 1 0 7 2 6 7 7 8
3 5 1 0 2 7 6 7 7 8
3 5 1 0 2 6 7 7 7 8
3 5 1 0 2 6 7 7 7 8
|
|