Generate the Magic square (weekly 05-03)

Source: Internet
Author: User
Tags count integer tostring
Recently stepped up to learn C #, but also want to look at the algorithm. Who wants us in the research mouth, is a own time more, of course, money is less.

Books bought a lot, what "21 days learn through C #", "C#primer" and "Microsoft." NET Framework Program Design (revised edition). Of course, the choice of these books is I read a lot of book reviews before buying, look at all is a good choice, a good book!

Although the book is good, but also want to look at Ah, I use the spare time basic has been browsed, finer points of knowledge will be in the future study well mastered.

Speaking of here, you will not believe me to die in the unit is still a small head, on weekdays in the suffering of young and young, up and down the "gas", only after work in the book to find a moment of peace. The purpose of reading is a sad thing to be seen.

I made up my mind that no matter how busy the unit and the family matter, write a weekly study week, not a diary, the diary is certainly no time to write.

First easy after difficult, look at a long time ago bought a "Programmer advanced Programmer Level Program Design (second edition)" This book has a number of good algorithm examples, they are all written in C, I use C # rewrite, in a small tool to show, so more intuitive.

The first one is to see the P96 on the formation of the magic square.

Rubik's Cube array refers to the elements of the natural number of 1,2,...,n2 nxn square, each element value is not equal, each row, column and the main, vice diagonal on the sum of the n elements are equal.

For the odd-order demon Phalanx, the dole Rob algorithm can be used to generate the procedure: Starting from 1, to insert the natural numbers, until N2. Select the insert position principle as:

A. The first position is in the middle of the first line;

B. The new location should be at the top right of the nearest insertion position, but if the upper-right position has exceeded the boundary on the square, the new position takes the Mayors column's next position; if the right boundary is exceeded, the new boundary takes the leftmost position of the selected row;

C. If the most recent integer that inserts an element of n is multiple, select the same row as the new position.

The realization of this algorithm needs to use two-dimensional integer array to realize the square.

The core algorithms are as follows:

private void Button1_Click (object sender, System.EventArgs e)

{//This part is the dole Rob algorithm for generating the magic square.

int Count,curi,curj;

int i,j;

int[,] magic=new int[3,3];

String A;





curi=0;

Curj=1;

for (count=1;count<=9;count++)

{

Magic[curi,curj]=count;

if ((count%3) ==0)//The most recent insertion element is an integer multiple of the order, select the next row on the same column as the new position.

{

Curi+=1;

Continue

}

Curi=curi-1;

curj=curj+1;

if (curi<0)

curi+=3;

else if (curj==3)

curj-=3;

}

Textbox1.appendtext ("\ n"); The generated magic squares are shown in the textBox1 below

for (i=0;i<3;i++)

{Textbox1.appendtext ("\ n");



for (j=0;j<3;j++)

{a = Convert.ToString (Magic[i,j]);

Textbox1.appendtext (a);

Textbox1.appendtext ("");



}

Textbox1.appendtext ("\ n");

}

Label1. Text= "In this example, the order of the magic square is taken here 3, of course, you can also increase the control of the order of the control, in order to simply no longer give here. ";



}





Figure 1 before clicking the Create Cube block button







Figure 2 After you click the Build Cube block button



When rewriting the algorithm, there are a few questions that impress me:

A. Magic[curi,curj] is written in this way, rather than written as a magic[curi],[curj, which is the writing of C.

B. A = Convert.ToString (Magic[i,j]) This sentence is critical, otherwise the result cannot be shown in TextBox1, the Convert class is in the System namespace, and some of its methods are useful for converting data types. Why to convert because Textbox1.appendtext (a) must be of type string.

C. In the C # language, it is strictly case-sensitive.

Well, just write it here, you don't say do you like things even if tired heart also sweet.

Tomorrow again ..., when the people are happiest but the people's bonuses are less, contradictions ah, hey, J.



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.