Problem Description:
The magician used a deck of 13 black label, pre-lined them up and stacked together, face down. Said to the audience: "I do not look at cards, only a few can guess what each card is, I loudly count, you listen, do not believe?" Live demonstrations. "The magician put the top card number at 1, turn him over. It is a spade a, put the spades a on the table, the second number 1, 2, the first card placed under these cards, the second card turned over, just the Spades 2, also put it on the table in turn will be 13 cards all turned out, accurate.
Solution:
Using the circular link list to implement
Code Description:
#include <stdio.h> #include <stdlib.h> #define N 13//Magician's number of cards (a-k in turn with 1~13) typedef struct NODE{INT data; struct Node *next; }NODE;TYPEDEF struct node *linklist;//constructs a circular list node* CreatList1 (struct node *la,int n) {//constructs a circular linked list (the value of the node is 0) returns the first address of the loop list int i ; struct Node *p,*s; La = (linklist) malloc (sizeof (Node)); La->next = Null;p = la;for (i = 1;i <= n;i++) {s = (linklist) malloc (sizeof (Node)); s->data = 0;s->next = P->ne Xt;p->next = S;p = P->next;} P->next = La->next;return p;} Analog magician cards to list assignment node* magician (struct Node *la,int n)//l->la n->n{int i,j,count = 2;//count used to count Node *p,*q;p = la-& Gt;next;//p->data = = 1p->data = 1;for (i = 2;i <= n;i++) {for (j = 1;j <= count;j++) {p = p->next;if (p->data ! = 0) {j--;}} P->data = i;count++;} return la->next;} int main (int argc,char* argv[]) {int i; Node *l,*p,*q; L = CreatList1 (l,n); L = Magician (l,n);//l->la n->np = l;printf ("Pre-order emission sequence: \ n"); for (i = 1;i <= n;i++) {printf ("%3d", p->data);p = P->next;} printf ("\ n"); System ("pause"); return 1;}
Magician Licensing Issues