Recurrence of algorithms

zhaozj2021-02-16  48

topic:

1, calculate

(Granted method)

Problem Description

For a given N, it is required to calculate in the o (n) step.

At the same time, it analyzes the time complexity and spatial complexity of the program.

Enter: N,

Output:

thought:

1. Realize the amount of calculation with a common algorithm

Magnitude;

2. Implementation of the algorithm for the O (N) level

Different implementation of grants:

Utilize the function:

F (n) =

program

// ******************************************************** ************************************** For a given N, required in O (N The 2 N power of 2 2 is calculated, and the time complexity and spatial complexity of the program are analyzed. // Use the granted method // Author: dongkaiying // use the commitness, we can find, the 'n' can only be the // region Lower Than 9; So Weme Must Find A better method to reduce the complexity of the "fenzhifa" we caled.//************************************* *********************************************************** ********* # include #include Double Common_m (int N); double fenzhi_m (int N); void main () {cout << "please enter the ' n 'you need: << Endl; int N; cin >> n; // use the uasiest method to compute the value; double j = common_m (n); cout << "Use the commit method to compute the value is : "<< j << endl; double m = fenzhi_m (n); cout <<" Use another commit: "<< endl; return;} // this method's complexity.: 2's n cimidouble common_m (int N) {double result = 1; double result_1 = 1; for (int x = 1; x <= n; x ) {result * = 2;} for (x = 1; x <= result; X ) {result_1 * = 2;} Return Result_1;}

// USE the DIGUI METHOD; DOUBLE FENZHI_M (INT N) {IF (n == 1) Return 4; Else Return Fenzhi_m (N-1) * fenzhi_m (N-1);}

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

New Post(0)