动态编程语⾔静态编程语⾔_什么是动态编程?
动态编程语⾔静态编程语⾔
介绍 (Introduction)
Today in this tutorial, we are going to discuss the concept of Dynamic Programming. It is a much more optimal approach to programming.
今天,在本教程中,我们将讨论动态编程的概念。 这是⼀种更好的编程⽅法。
动态编程 (Dynamic Programming)
Theoretically, Dynamic Programming is a problem-solving technique that solves a problem by dividing it into sub-problems. When the sub-problems are same and dependent, Dynamic programming comes into the picture.
从理论上讲, 动态编程是⼀种解决问题的技术,它通过将问题分成⼦问题来解决。 当⼦问题相同且相互依赖时,动态编程就会出现。
It is very similar to the Divide and Conquer approach to problem-solving. But in some cases, the Divide and Conquer technique may perform some operations or tasks multiple times (increasing time complexity).
它与解决问题的“ 分⽽治之”⽅法⾮常相似。 但是在某些情况下,分⽽治之技术可能会多次执⾏某些操作或任务(增加时间复杂度)。
Hence, the Dynamic Programming method (DP) is normally used to optimize a specific problem.
因此,动态编程⽅法(DP)通常⽤于优化特定问题。
Further, DP follows the Principle of Optimality which states that for an optimal sequence of decisions, each sub-sequence must also be optimal.
此外, DP遵循最优性原则,该原则指出,对于最佳决策序列,每个⼦序列也必须是最优的。
⽰例–使⽤DP的斐波那契 (Example – Fibonacci Using DP )
Now that we have discussed the basic concept of Dynamic Programming, let us look at how to solve a problem using this method.
现在我们已经讨论了动态编程的基本概念,让我们看看如何使⽤这种⽅法解决问题。
In the example below, we try to calculate the number at the position n (user input) in the .
在下⾯的⽰例中,我们尝试计算数列中位置n (⽤户输⼊)处的数字。
As you know, a Fibonacci series looks like this,
如您所知,斐波那契数列看起来像这样,
0, 1, 1, 2, 3, 5, 8 … upto n terms.
0、1、1、2、3、5、8…最多n个词。
Here, the starting two digits are 0 and 1.
在这⾥,起始两位数字是0和1 。
Let’s write a program to automatically find the right number at a specific point in the Fibonacci sequence based on the user input.
让我们编写⼀个程序,根据⽤户输⼊在斐波那契数列的特定点⾃动到正确的数字。
Using Recursion(in C):
使⽤递归(在C中):
#include<stdio.h>
//fibonacci calculating function
int fibo(int n)
{
if(n==1)
return 0;
else if(n==2)
return 1;
else
return fibo(n-1) + fibo(n-2);
}
int main()
{
int n,res;
printf("Enter n : ");
scanf("%d",&n);
res=fibo(n);
printf("fibo(%d) = %d",n,res);
return 0;
}
Output:
输出 :
Enter n : 5
fibo(5) = 3
Fibo2
Fibonacci using Recursion( if n=5 )
斐波那契使⽤递归(如果n = 5)
So as you can see, in our above example taking n=5, recursion breaks the problem into sub-problems using recursive calls.如您所见,在我们上⾯的⽰例中,使⽤n=5 ,递归使⽤递归调⽤将问题分解为⼦问题。
Take a closer look, notice that in this case, fibo(3) is calculated twice. Which then breaks down to two recursive calls for
fibo(1) and fibo(2). They have values 0 and 1 respectively. Finally, we get our desired , fibo(5) = 3.
仔细观察⼀下,请注意,在这种情况下, fibo(3)是两次计算的。 然后将其分解为对fibo(1)和fibo(2)两个递归调⽤。 它们的值分别为0和1 。最后,我们得到所需的输出,即fibo(5) = 3 。
Applying DP(in C):
应⽤DP(在C中):
#include<stdio.h>
int fibo_series[100];
void set_zero()
{
int i;
for (i = 1; i <= 100; i++)
fibo_series[i] = 0;
}
//fibonacci calculating function
int fibo(int n)
{
if (fibo_series[n] == 0)
{
if (n <= 1)
fibo_series[n] = n;
else
fibo_series[n] = fibo(n - 1) + fibo(n - 2);
}
return fibo_series[n];
}
int main ()
{
int n,res;
set_zero();
printf("Enter n : ");
scanf("%d",&n);
if(n>=100 || n<=0)
printf("Entered number is out of range!");
else
{
res=fibo(n-1);学编程学什么语言更好
printf("fibo(%d) = %d",n,res);
}
return 0;
}
Output:
输出 :
Enter n : 5
fibo(5) = 3
This time firstly we initialize an array fibo_series[] with a maximum 100 elements to have a value 0, using the function set_zero(),
这次我们⾸先使⽤函数set_zero()初始化⼀个数组fibo_series[] ,该数组最多包含100个元素,值为0 。
This array is further going to be used to store the Fibonacci series’s elements as soon as one is calculated,
⼀旦计算出该数组,该数组将进⼀步⽤于存储斐波那契数列的元素,
Inside the fibo() function first, we check whether the required element has been already calculated and stored inside the array or not. If not, it is calculated. And if yes, the element is directly passed to the site of the function call,
⾸先在fibo()函数内部,我们检查所需的元素是否已经计算并存储在数组中。 如果不是 ,则进⾏计算。 如果是 ,则将元素直接传递到函数调⽤的位置,
This ensures that the same function(as we saw earlier fibo(3)) is not called twice. Hence in this way, we get the optimal solution.
这样可以确保同⼀函数(如我们先前看到的fibo(3)) 不会被调⽤两次。 因此,通过这种⽅式,我们获得了最佳解决⽅案。
Note: We pass (n-1) while calculating res to the function fibo() since the array index starts from zero. Whereas, we are considering the elements of the series to start with index 1.
注意:由于数组索引从零开始,因此在计算res时将(n-1)传递给函数fibo() 。 鉴于我们正在考虑从索引1开始的系列元素。
For the same reason, we have used an if-else statement to eliminate the cases where the user enters n as n>=100 or n<=0.
出于相同的原因,我们使⽤了if-else语句来消除⽤户以n>=100或n<=0输⼊n的情况。
结论 (Conclusion)
So in this tutorial, we learned about the Dynamic Programming technique with its implementation and how it gives us an optimal solution.
因此,在本教程中,我们学习了动态编程技术及其实现⽅法,以及它如何为我们提供最佳解决⽅案。
For any further questions, feel free to use the comments below.
如有其他疑问,请随时使⽤以下评论。
参考资料 (References)
,
– Wikipedia,
–,
– Stack Overflow Question.
–堆栈溢出问题。
动态编程语⾔静态编程语⾔

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。