给定条件找最小值c语言程序_根据给定条件最小化n的最小步骤

给定条件找最小值c语言程序

Problem statement:

问题陈述:

Given a number n, count minimum steps to minimize it to 1 performing the following operations:

给定数字n ,执行以下操作,计算最少的步骤以将其最小化为1:

  1. Operation1: If n is divisible by 2 then we may reduce n to n/2.

    运算1:如果n可被2整除,则可以将n减小为n / 2

  2. Operation2: If n is divisible by 3 then you may reduce n to n/3.

    运算2:如果n可被3整除,则可以将n减小为n / 3

  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,

喜欢,

  1. 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)

  2. 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)

  3. 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)

  4. 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)

  5. 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

Minimum steps to minimize n

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语言程序

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值