一、应用1
- 解码,对于一串数字,将第一个数字删除,第二个数字排至这串数字的末尾,将第三个数字删除,将第四个数字排至末尾,循环下去直到这串数字都删除了。删除的数字就是真正的密码。举个例子:
2. 代码实现
#include#include #define SIZE 512 char queue[SIZE]; int head, tail = 0;//队列的两个关键要素,头和尾。 void enqueue(char c); char dequeue(void); int is_empty(void); int is_full(void); int main(void) { char code[12]; int n; int i = 0; char num; printf("Please en a code:n"); gets(code); for(n = 0; n < strlen(code); n++) { if(!is_full()) { enqueue(code[n]);//入队列 } } while(!is_empty())//当不为空的时候, { code[i++] = dequeue();//出队列,放在code里面,第一次执行,将第一个元素放入code[]数组。牢记出队列的特点。 if(!is_empty()) { num = dequeue();//第二次出队列,暂存在num里, if(!is_full()) enqueue(num);//放在队列的最后, } //直到队列为空。 } printf("Origral code is :n"); for(n = 0; n < strlen(code); n++) printf("%c", code[n]); printf("n"); } void enqueue(char c) { queue[tail] = c;//不断有元素入队列,表现形式是队列的尾在不断增加。 tail = (tail + 1) % SIZE; } char dequeue(void) { char ch; ch = queue[head]; head = (head + 1) % SIZE; return ch;//出队列,队列的头不断增加。 } int is_empty(void) { return head == tail; } int is_full(void) { return ((tail + 1) % SIZE) == head; }
运行结果:
Please en a code: 765730314892 Origral code is : 753349608721



