Magician licensing and Latin phalanx

Source: Internet
Author: User
This article uses the classic magician licensing problem and the Latin array to explain the use of the circular linked list and one-way linked list respectively. As a classic in the algorithm, it is of great help to learn and understand the linked list, take a look.
Magician licensing issues

Problem description:

The magician uses 13 black cards in a deck to arrange them in advance and stack them together. Said to the audience: "I don't watch a card. I can guess what each card is. I can count it out loud. Do you believe it? On-site demonstration ." The magician turned the card number at the top to 1. He turned it to black peach A and put black peach A on the table. The second time, 1, and 2, he put the first card under these cards, turn over the second card, which is exactly the peach 2, and put it on the table so that all 13 cards will be turned out in turn, accurate.

Q: How is the starting order of cards arranged?

The code for the classic cyclic linked list is as follows:

#include<iostream>using namespace std;#define CardNumber 13struct node{int data;struct node *next;}*linklist;void CreateLinkList(){linklist = NULL;node *p,*q;int i;p=linklist;for(i=0;i<CardNumber;i++){p=new node;p->data=0;if(linklist==NULL)linklist=p;elseq->next=p;q=p;}q->next=linklist;}void Magician(){node *p=linklist;int j;int count=2;p->data=1;while(1){for(j=0;j<count;j++){p=p->next;if(p->data!=0){j--;}}if(p->data==0){p->data=count;count++;if(count==14)break;}}}void print(){node *p=linklist;while(p->next!=linklist){cout<<p->data<<endl;p=p->next;}cout<<p->data<<endl;}int main(){CreateLinkList();Magician();print();return 0;}

Latin square matrix Problems

Problem description:

The Latin square matrix is a matrix of n × n. There are EXACTLY n different elements in the square matrix, each of which has n elements, and each element exactly appears in one row and one column.

The famous mathematician and physicist Euler used Latin letters as symbols of elements in the Latin square matrix, hence the name of the Latin square matrix.

For example:

1 2 3

2 3 1

3 1 2

Q: How to Construct the n-order Latin matrix? The general code is as follows: (all the Latin squares of level N)

# Include <iostream> using namespace STD; # define N 3/* determine N values */INT main () {int I, J, K, T; printf ("The possble Latin squares of order % d are: \ n", n); For (j = 0; j <n; j ++) /* construct n different Latin phalanx */{for (I = 0; I <n; I ++) {T = (I + J) % N; /* determine the value of the first element in row I of the Latin matrix */For (k = 0; k <n; k ++) /* output each element in the row in the form of a ring */cout <(K + T) % N + 1; cout <Endl ;} printf ("\ n ");}}

Code for single-chain table implementation: (standard type of the Latin square matrix. Other types can be obtained using a single-chain table method similar to this)

# Include <stdio. h> # include <stdlib. h> typedef struct node {int data; struct node * Next;} linklist; linklist * createlist (int n); // create a linked list linklist * createlist (int n) {linklist * head, * s, * r; head = NULL; r = head; For (INT I = 1; I <= N; I ++) {S = (linklist *) malloc (sizeof (node); s-> DATA = I; If (Head = NULL) Head = s; else R-> next = s; r = s;} r-> next = head; return head;} int main () {linklist * l, * hea D; int N; printf ("Enter the Order N:"); scanf ("% d", & N); L = createlist (N ); head = L; For (INT I = 1; I <= N; I ++) {L = head; For (Int J = 1; j <I; j ++) {L = L-> next;} For (int K = 1; k <= N & L-> next! = NULL; k ++) {printf ("% 4D", L-> data); L = L-> next;} printf ("\ n ");} return 0 ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.