I. Prototype of the topic
Chinese chess friends know that both sides of the "will" and "handsome" far apart, and they can not face. In the game of chess, many masters can use this rule to get out of the exquisite killing. Suppose the board has only "will" and "handsome" two sons (as shown in Figure 1-3) for the following purposes, we agreed to use A for "will", B for "handsome", then A and b of the movement will be limited to their own 3x3 lattice, a, B can be horizontal or vertical movement of a grid, but can not move diagonally. When A and B are in a straight line, the chess game ends. In other words, A and B cannot be on a straight line, write a program to output all possible locations for a, B.
Second, mathematical abstraction
The general idea of this problem is to iterate over the possible locations of each A and B, then combine, judge, and output if the conditions are met. Based on this idea, we find that the position of a and B can be described by these 9 numbers. So how do you judge whether these two points are on a line? After careful study, we found that we can solve with the remainder, so the code of the program comes out:
for (int i = 1; I <= 9; i++)
{for
(int j = 1; J <= 9; j +)
{
if (i% 3!= J% 3) {
Console. WriteLine ("A Possible combination is" + "(" + i + "," + j + ")");}}
Oh, the first "beauty of programming" article on such a happy start it.