21、指定元素置后——数组

将数组中指定元素置于最后

源程序代码如下:

/*
	2017年6月12日15:13:39
	功能:将数组中指定元素置于最后
*/
#include"stdio.h"
#define M 15
int main()
{
	int temp = 0;
	int a[M] = {0,1,9,9,4,0,5,0,8,0,8,9,9,5,6};
	int i,j;
	for(i = 1, j = M - 1; i < M ; )					//最后元素的下标值应该为:M-1
	{

		if(a[i] == 5 && a[j] == 5)
		{
			j--;
		}
		else if(a[i] != 5 && a[j] == 5)
		{
			i++;
			j--;
		}
		else if(a[i] != 5 && a[j] != 5)
		{
			i++;
		}
		else if(a[i] == 5 && a[j] != 5)
		{
			temp = a[i];
			a[i] = a[j];
			a[j] = temp;
			i++;
			j--;
		}
		if(i >= j)
			break;
	}
	printf("The new array is : ");
	printf("
");
	for(i = 1; i < M; i++)							//注意:最后一个元素a[M -1],取不到a[M]的值
		printf("%4d",a[i]);
		printf("
");
	return 0;
}
/*
	总结:
	在VC++6.0中的显示结果是:
	——————————————————————
	The new array is :
	1   9   9   4   0   6   0   8   0   8   9   9   5   5
	——————————————————————
*/
原文地址:https://www.cnblogs.com/wxt19941024/p/6993429.html