规则是这样的:首先将第 1个数删除,紧接着将第 2 个数放到这串数的末尾,再将第 3 个数删除并将第 4 个数放到这串数的末尾,再将第 5 个数删除……。直到剩下最后一个数,将最后一个数也删除。按照刚才删除的顺序,把这些删除的数连在一起就是QQ。

假设是9位的qq。

解决思路:用队列的head指向给出乱序qq的第一个元素,让tail指向qq的末尾的下一个(这样做的目的是为了当head=tail,队列为空)。当删除时,只要head++,放到末尾时,将tail++就OK。


队列:队列是一种特殊的线性结构,它只允许在队列的首部(head)进行删除操作,这称为“出队”,而在队列的尾部(tail)进行插入操作,这称为“入队”。当队列中没有元素时(即 head=tail),称为空队列。因此,队列满足“先进先出”的原则。


上代码:

#include <stdio.h>
#include <stdlib.h>

typedef struct	queue{
	int queueArr[100];//定义队列空间
	int head;         //队列头部
	int tail;         //队列尾部
}queue;

/*
解密qq
*/
int queueDecodeQQ ( int *queueQQ,int head,int tail ,int *desQQ )
{
	int index = 0;
	if (( NULL == queueQQ )&&( NULL == desQQ))
	{
		return -1;
	}
	//进行解密
	while( head < tail )//条件是不为空
	{
		desQQ[index] = queueQQ[head];//将删除的数保留到desQQ数组,即就是真正的qq序列
		head++;
		queueQQ[tail] = queueQQ[head];//将head放到尾部
		tail++;
		head++;
		index++;
	}
	return 0;
}

/*
主函数
*/
int main()
{
	int index = 0;
	//定义队列
	queue queueQQ;
	//定义目的qq存放数组
	int desQQ[9] = {0};
	//输入
	printf("请输入要解密的qq:");
	for ( ; index < 9; index++ )
	{
		scanf("%d",&queueQQ.queueArr[index]);
	}
	//初始化对头和队尾
	queueQQ.head = 0;
	queueQQ.tail = 9;//队列尾部应该指向实际队列的最后一个数的下一个,保证当tail = head队列为空。
	//解密
	queueDecodeQQ (queueQQ.queueArr,queueQQ.head,queueQQ.tail,desQQ);
	//输出打印
	for ( index = 0; index < 9; index++ )
	{
		printf("%d ",desQQ[index]);
	}
	//换行
	printf("\n");
	//停止程序,可以保留那个黑框框(doc命令框)便于查看
	system("pause");
	return 0;
}