给定条件找最小值c语言程序
Problem statement:
问题陈述:
Given a number n, count minimum steps to minimize it to 1 performing the following operations:
给定数字n ,执行以下操作,计算最少的步骤以将其最小化为1:
Operation1: If n is divisible by 2 then we may reduce n to n/2.
运算1:如果n可被2整除,则可以将n减小为n / 2 。
Operation2: If n is divisible by 3 then you may reduce n to n/3.
运算2:如果n可被3整除,则可以将n减小为n / 3 。
Operation3: Decrement n by 1.
运算3:将n减1。
Input:
输入:
Input is a single integer, n.
输入是一个整数n 。
Output:
输出:
Output the minimum number of steps to minimize the number performing only the above operations.
输出最小步数以最小化仅执行上述操作的步数 。
Constraints:
限制条件:
1 <= N <= 10000
Example:
例:
Test case 1:
Input:
N=10
Output:
Minimum number of steps needed: 3
Test case 2:
Input:
N=6
Output:
Minimum number of steps needed: 2
Explanation:
说明:
For the above test case,
N=10
N is not divisible by 3, but by 2
So,
10->5
Now % is again neither divisible by 3 nor 2
So, only option is to decrement
Hence
5->4
4 can be decremented to 2 by dividing by 2
4->2
2->1
So,
The conversion path will be
10->5->4->2->1
Total 4 steps
But is this the optimal one?
Answer is no
The optimal will be
10 converted to 9 by decrementing
10->9
9->3->1
So, total of 3 steps which is the minimum number
Hence answer would be 3
Solution Approach:
解决方法:
The above problem is a classic recursion example.
上面的问题是一个经典的递归示例。
Like,
喜欢,
If n is divided by both 2 and 3
如果n除以2和3
Recur for possibilities
重复可能性
(n/3), (n/2), (n-1)
(n / 3),(n / 2),(n-1)
If n is divided by only 3 but not by 2 then
如果n仅除以3而不是2,则
Recur for possibilities
重复可能性
(n/3), (n-1)
(n / 3),(n-1)
If n is divided by only 2 but not by 3 then
如果n仅被2除而不是3,则
Recur for possibilities
重复可能性
(n/2), (n-1)
(n / 2),(n-1)
If n is divided by only 3 but not by 2 then
如果n仅除以3而不是2,则
Recur for possibilities
重复可能性
(n/3), (n-1)
(n / 3),(n-1)
If n is not divisible by both 2 and 3
如果n不能被2和3整除
Then only recur for
然后只重复
(n-1)
(n-1)
We can write all this with help of recursive function,
我们可以借助递归函数来编写所有这些代码,
Let,
f(n) = the minimum number of steps to convert n to 1
If you draw the recursion tree for f(10) you will find many overlapping sub-problems. Hence, we need to store all the already computed sub-problems through memorization.
如果为f(10)绘制递归树,则会发现许多重叠的子问题。 因此,我们需要通过记忆存储所有已经计算出的子问题。
Function minimumSteps(n)
if(n==1)
return 0;
// memoization here, no need to compute if already computed
if(dp[n]!=-1)
return dp[n];
// store if not computed
if(n%3==0 && n%2==0)
dp[n]=1+min(minimumSteps(n-1),min(minimumSteps(n/3),minimumSteps(n/2)));
else if(n%3==0)
dp[n]=1+min(minimumSteps(n-1),minimumSteps(n/3));
else if(n%2==0)
dp[n]=1+min(minimumSteps(n-1),minimumSteps(n/2));
else
dp[n]=1+minimumSteps(n-1);
end if
return dp[n]
End Function
C++ Implementation:
C ++实现:
#include <bits/stdc++.h>
using namespace std;
int dp[10001];
int minimumSteps(int n)
{
if (n == 1)
return 0;
if (dp[n] != -1)
return dp[n];
if (n % 3 == 0 && n % 2 == 0) {
dp[n] = 1 + min(minimumSteps(n - 1), min(minimumSteps(n / 3), minimumSteps(n / 2)));
}
else if (n % 3 == 0) {
dp[n] = 1 + min(minimumSteps(n - 1), minimumSteps(n / 3));
}
else if (n % 2 == 0) {
dp[n] = 1 + min(minimumSteps(n - 1), minimumSteps(n / 2));
}
else
dp[n] = 1 + minimumSteps(n - 1);
return dp[n];
}
int main()
{
int n, item;
cout << "enter the initial number, n \n";
cin >> n;
for (int i = 0; i <= n; i++)
dp[i] = -1;
cout << "Minimum number of steps: " << minimumSteps(n) << endl;
return 0;
}
Output:
输出:
RUN 1:
enter the initial number, n
15
Minimum number of steps: 4
RUN 2:
enter the initial number, n
7
Minimum number of steps: 3
翻译自: https://www.includehelp.com/icp/minimum-steps-to-minimize-n-as-per-given-condition.aspx
给定条件找最小值c语言程序