Magician licensing and Latin phalanx, magician Latin phalanx

Source: Internet
Author: User

Magician licensing and Latin phalanx, magician Latin phalanx
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 ;}



What is the Latin matrix?

The following is a definition and an example:

In the number array of N rows and N columns, the number K (1 <= K <= N) appears in each row and column and only
Once, such a number array is called the N-order Latin matrix. For example, it is a fifth-order Latin matrix.
1 2 3 4 5
2 3 4 5 1
3 4 5 1 2
4 5 1 2 3
5 1 2 3 4

In addition, I will give you a simple C ++ program for printing the Latin square matrix:
(If you have any questions, send them to my mailbox)

# Include <iostream. h>

Int main ()
{
Int n, I, j;
Int Num [20];
Cout <"Enter the square matrix dimension = ";
Cin> n;
For (I = 1; I <= 2 * n; I ++) // assign an initial value to the array
{
If (I> n)
{
Num[ I-1] = I-n;
}
Else Num [I-1] = I;
}
For (I = 0; I <n; I ++) // The External Loop ensures that n rows are output.
{
For (j = I; j <n + I; j ++) // each number in a row is output in an inner loop.
{
Cout <Num [j] <'\ T ';
If (j = n + i-1) cout <'\ n ';
}
}
Return 0;
}

Logo Program: Latin phalanx

Problem Analysis and Algorithm Design
There are many ways to construct the Latin square matrix. Here is the simplest method. By observing the example given, we can find that if the number in the first column in each row is connected to the number in the last column to form a ring, the positive ring is composed of 1 to N order. For row I, the start Number of the ring is I. According to this rule, you can easily write programs. The procedure for constructing the 6-order Latin matrix is given below.
* Program and program comments
# Include <stdio. h>
# Define N 6/* determine N value */
Void 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 ++)/* Outputs each element in the row in the form of a ring */
Printf ("% d", (k + t) % N + 1 );
Printf ("\ n ");
}
Printf ("\ n ");
}
}
* Running result
The possble Latin Squares of order 6 are:
1 2 3 4 5 6 2 4 5 6 1 3 4 5 6 1 2
2 3 4 5 6 1 3 4 5 6 1 2 2 4 5 6 1 3
3 4 5 6 1 2 4 5 6 1 3 5 6 1 2 4
4 5 6 1 2 3 5 6 1 3 3 4 6 1 2 4 5
5 6 1 2 3 4 6 1 3 4 5 1 2 4 5 6
6 1 2 3 4 5 1 2 4 5 6 2 3 4 5 6 1

4 5 6 1 2 3 5 6 1 3 3 4 6 1 2 4 5
5 6 1 2 3 4 6 1 3 4 5 1 2 4 5 6
6 1 2 3 4 5 1 2 4 5 6 2 3 4 5 6 1
1 2 3 4 5 6 2 4 5 6 1 3 4 5 6 1 2
2 3 4 5 6 1 3 4 5 6 1 2 2 4 5 6 1 3
3 4 5 6 1 2 4 5 6 1 2 3 5 6 1 2 ...... the remaining full text>

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: info-contact@alibabacloud.com 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.