队列应用
已知一个数列组合是经过加密后的一串数字,那么现在知道这串加密后的数字,并且指导如何破解的方法,求加密前数字的组合是什么。
破解的方法:首先将第i个数删除,紧接着将第2个数放到
这串数的末尾,再将第3个数删除并将第4个数放到这串数的末尾,再将第5个数删除……直到剩下最后一个数,将最后一个数也删除。按照刚才删
除的顺序,把这些删除的数连在一起就是原始的数据。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 512
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
void enqueue(char c);
char dequeue();
char array[SIZE];
int is_empty();
int is_full();
int tail=0;
int head=0;
int main(int argc, char *argv[]) {
char ch[521];
int n ;
printf("Enter encrypt content: ");
gets(ch);
int len = strlen(ch);
printf("strlen : %d\n",len);
for(n=0;n<len;n++)
{
enqueue(ch[n]);
}
printf("Enter Queue Finfish !\n");
printf("Decrypt content ---> ");
while(!is_empty())
{
//printf("arraychar #%s\n",array);
printf("%c",dequeue());
if(!is_empty())
{
enqueue(dequeue());
}
}
return 0;
}
void enqueue(char c)
{
array[tail] = c ;
tail = (tail+1)%SIZE;
}
char dequeue()
{
char c;
c = array[head];
head++;
return c;
}
int is_empty()
{
return tail==head;
}
int is_full()
{
return (tail+1)%SIZE==head;
}