Algorithm I-IV (C ++ Implementation) - Reading Notes (1)

xiaoxiao2021-03-06  38

Today, I officially study this algorithm, and I will find the foundation for the "computer program design art" in the future.

Because this is the photo version of the Higher Education, it will look slow, and it is not very meticulous. After all, I am poor. . .

First let's take a look at the first simple small example of this book - the problem of connectors

The problem is: If you enter a pair of numbers P and Q, it is assumed that this pair of connectivity, such as 1 connectivity 2, 2 connects 3, then 1 automatically connects 3, requires a program to enter P In the case of Q, the new communication is output.

#include

Static const INT n = 1000;

int main ()

{INT I, P, Q, ID [N];

For (i = 0; i

While (cin >> p >> q)

{INT T = ID [P];

IF (t == id [q]) Continue;

For (i = 0; i

IF (id [i] == t) ID [i] = id [q];

Cout << "<< p <<" << q << endl;

}

}

Analyze the procedures on the book, obviously, input P, ​​q, if the ID [P] == ID [q [q [q [q [q [q [q [q "can be abstract as communication. The first for statement initializes ID [], it is obvious, input P, ​​q, ID [P] == ID [q], ie, any number, in touch with yourself (anti-

). When the P, Q value is different, first use a FOR traversing array ID [], set all the array values ​​of all the variable t (Id [P]) as ID [q], at this time Id [P] == ID [q], the different P, Q can be considered communication. Then output P and Q

The key to this question is the abstraction of the connection, that is, any two element values ​​in the ID [] array, then two of the two differentiated numbers in the array.

转载请注明原文地址:https://www.9cbs.com/read-65877.html

New Post(0)