And ARDEN works with an algorithm - Sixth Day

xiaoxiao2021-03-06  48

Sixth day

In fact, it is still recursive, and specializes in grants is that it separates the problem separately, and constantly analyzing the problem to get a direct sub-answer. I read here, I was thinking about the traversal of the tree. Isn't this a tree traversal? Yes it is.

The advantage of grants is that the computer will give a good thing, he will give a suitable method, greatly reduce the complexity of the program.

The following program calculates the maximum value of an array. It can't show the advantages of separation, and the loss is convenient for us to enter the door. In order to facilitate everyone to understand that Item can change the data type of everyone's habits.

Item Max (item a [], int L, int R)

{ITEM U, V; INT M = (L R) / 2;

IF (l == r) Return A [L];

u = max (a, l, m);

v = max (a, m 1, r);

IF (u> v) Return U; Else Return V;

}

His calling process is as follows:

Max (0, 10)

Max (0,5)

Max (0, 2)

Max (0, 1)

Max (0,0)

Max (1, 1)

Max (2, 2)

Max (3,5)

Max (3, 4)

Max (3, 3)

MAX (4, 4)

Max (5, 5)

Max (6,10)

Max (6,8)

Max (6,7)

Max (6,6)

Max (7,7)

Max (8, 8)

Max (9, 10)

Max (9, 9)

Max (10, 10)

We don't study how it is achieved at the memory level. Of course, if you are interested, you can refer to the reference book I mentioned in the first day. I recently got the three volumes of the Knuth. - "Computer Programming Art", I will try to digest some clearer knowledge and share with everyone below.

Then the back layout is left to the ancient Hano issue Tower of Hanoi:

Hanno Taga game is an old Asian game, the goal is to move all the dishes from the first needle to the third needle onto the third needle. You cannot put your big dish onto a small dish.

/ * Put the number from 1 to N from 1 to N from 1 to N from 1 to N, B is used as auxiliary tower * // * parameter N is the number of trays, A, B, and C Tower of the plate * /

/ * WRITE BY: Mr. Sun, Dalian University of Technology * / void Hanoi (int N, char A, char b, char c) {if (n> 0) {Hanoi (N-1, A, C, B); / * The 1 to N-1 tray moves from the A to the B, C is auxiliary tower * / printf ("% d from% C to% C / N", N, A, c); / * Output mobile steps * / Hanoi (N-1, B, A, C); / * Move 1 to N-1 tray from B to B, C is auxiliary tower * /}} main () {Int n; printf ("/ NPLEASE ENTER THE NUMBER OF TOWER:"); Scanf ("% D", & n); Printf ("/ n"); Hanoi (N, 'A', 'B', 'C'); / * Character 'a', 'b', 'c' is the name of 3 colors * /}

Ha, Robert Sedgewick said such a dope, saying that several monk in the temple completed this task, putting 40 gold plates on the diamond pile, then the end of the world will come. Put these 40 plates, you need to move 2 ^ 40-1 move. Move once every second and take 348 centuries. Long Shu is angry, and it is not so long.

Finally, we will write a two-point sort, think of it, go to the interview, people let me write a two-point method, I wrote a long time, full of sweating, code chaos. People despise me: (. I am just less than half of the programmer, proud is that I can soon write a two-point method J.//

ARDEN

Write binary search

2005-2-25

Int bsearch (int A [】, int pnumber, int start, int end)

{

While (Begin <= END)

{

INT POSITION = (end begin) / 2;

IF (a [position] == pnumber) return position;

IF (a [position]

Else Bsearch (A, PNumber, Position 1, End);

}

Return -1;

}

I have written some junior high school questions for my colleagues, and there is a problem that uses a granted method to solve:

Messen Mason:

The number of prime numbers such as 2P-1 is called Messen, and P must also be a number of pixels. But it is not necessarily, that is, if P is a single digit. 2P-1 is not necessarily the number. By the end of 1998, people have found 37 麦 森 森. The biggest one is P = 3021377, which has 909526 bits. Mesissen has many important applications, which are closely related to the whole number.

Task: Enter P from the file (1000

Calculate the value of 2P-1 (represented with a decimal high-precision number), and finally output on the text file:

(1) Chapter 1: Decoction of high-precision 2P-1 digits.

(2) Section 2-11: Decimal high-precision number 2P-1 last 500 digits. (50 digits per row, a total of 10 lines, when less than 500 digits is 500)

You don't have to verify that 2P-1 and P are in the number of prime.

// Write By

ARDEN

2005-01

//mason.c

#include

#include

// # include

#include

#define MaxN 500

void main ()

{

Char Fi [20], FO [20];

File * Pofile, * pifile;

Char n [MAXN];

Long i, j, k, s, m;

Long P, P0;

Char Num [8];

Char C [8];

Printf ("Input File Name: / N");

Scanf ("% s", fi);

Printf ("Output File Name: / N");

Scanf ("% s", fo);

PIFILE = FOPEN (FI, "R");

Pofile = FOPEN (FO, "W");

P = ATOI (FGETS (NUM, 8, PIFILE));

FPUTS (ITOA (Ceil (P * log (2) / log (10)), C, 10), POFILE

FPUTC ("/ n", Pofile;

For (i = 1; i <= maxn; i )

{

N [i] = 1;

m = 1;

}

For (i = 1; i <= 26; i ) m * = 2; p0 = p% 26;

p / = 26;

For (i = 1; i <= p; i )

{

K = 0;

For (j = 1; j <= maxn; j )

{

S = N [J] * m K;

K = S / 10;

N [j] = s% 10;

}

}

For (i = 1; i <= p0; i )

{

K = 0;

For (j = 1; j <= maxn; j )

{

S = N [J] * 2 K;

K = S / 10;

N [j] = s% 10;

}

}

N [i] = n [i-1];

For (i = 0; i <= 9; I -)

{

For (j = 1; j <= 50; J-)

FPUTC (N [i * 50 j], poent);

}

Fclose (pifile);

Fclose (Pofile);

}

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

New Post(0)