热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

通过使用动态编程的给定操作将N转换为M

通过使用动态编程的给定操作将N转换为M原文:https:/

通过使用动态编程的给定操作将 N 转换为 M

原文:https://www . geesforgeks . org/convert-n-to-m-with-给定-operations-use-dynamic-programming/

给定两个整数 NM ,任务是通过以下操作将 N 转换为 M :


  1. N 乘以 2 ,即 N = N * 2

  2. N 中减去 1 ,即N = N–1

示例:

输入: N = 4,M = 6
T3】输出: 2
执行操作 2:N = N–1 = 4–1 = 3
执行操作 1: N = N * 2 = 3 * 2 = 6

输入: N = 10,M = 1
T3】输出: 9

方法:创建一个大小为 MAX = 10 5 + 5 的数组 dp[] 来存储答案,以防止一次又一次的相同计算,并用-1 初始化所有数组元素。


  • 如果 N ≤ 0N ≥ MAX 表示不能转换为 M ,那么返回 MAX

  • 如果 N = M 则返回 0,因为 N 被转换为 M

  • 否则在DP【N】找到值,如果不是 -1 ,说明计算的比较早,所以返回DP【N】

  • 如果是 -1 ,那么将调用递归函数作为 2 * NN–1并返回最小值,因为如果 N 是奇数,那么只能通过执行N–1操作来达到,如果 N 是偶数,那么必须执行 2 * N 操作,因此检查两种可能性并返回最小值。

下面是上述方法的实现:

C++


// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
int n, m;
int dp[N];
// Function to return the minimum
// number of given operations
// required to convert n to m
int minOperations(int k)
{
    // If k is either 0 or out of range
    // then return max
    if (k <= 0 || k >= 2e4) {
        return 1e9;
    }
    // If k = m then conversion is
    // complete so return 0
    if (k == m) {
        return 0;
    }
    int& ans = dp[k];
    // If it has been calculated earlier
    if (ans != -1) {
        return ans;
    }
    ans = 1e9;
    // Call for 2*k and k-1 and return
    // the minimum of them. If k is even
    // then it can be reached by 2*k operations
    // and If k is odd then it can be reached
    // by k-1 operations so try both cases
    // and return the minimum of them
    ans = 1 + min(minOperations(2 * k),
                  minOperations(k - 1));
    return ans;
}
// Driver code
int main()
{
    n = 4, m = 6;
    memset(dp, -1, sizeof(dp));
    cout << minOperations(n);
    return 0;
}


Java 语言(一种计算机语言,尤用于创建网站)


// Java implementation of the approach
import java.util.*;
class GFG
{
    static final int N = 10000;
    static int n, m;
    static int[] dp = new int[N];
    // Function to return the minimum
    // number of given operations
    // required to convert n to m
    static int minOperations(int k)
    {
        // If k is either 0 or out of range
        // then return max
        if (k <= 0 || k >= 10000)
            return 1000000000;
        // If k = m then conversion is
        // complete so return 0
        if (k == m)
            return 0;
        dp[k] = dp[k];
        // If it has been calculated earlier
        if (dp[k] != -1)
            return dp[k];
        dp[k] = 1000000000;
        // Call for 2*k and k-1 and return
        // the minimum of them. If k is even
        // then it can be reached by 2*k operations
        // and If k is odd then it can be reached
        // by k-1 operations so try both cases
        // and return the minimum of them
        dp[k] = 1 + Math.min(minOperations(2 * k),
                             minOperations(k - 1));
        return dp[k];
    }
    // Driver Code
    public static void main(String[] args)
    {
        n = 4;
        m = 6;
        Arrays.fill(dp, -1);
        System.out.println(minOperations(n));
    }
}
// This code is contributed by
// sanjeev2552


Python 3


# Python3 implementation of the approach
N = 1000
dp = [-1] * N
# Function to return the minimum
# number of given operations
# required to convert n to m
def minOperations(k):
    # If k is either 0 or out of range
    # then return max
    if (k <= 0 or k >= 1000):
        return 1e9
    # If k = m then conversion is
    # complete so return 0
    if (k == m):
        return 0
    dp[k] = dp[k]
    # If it has been calculated earlier
    if (dp[k] != -1):
        return dp[k]
    dp[k] = 1e9
    # Call for 2*k and k-1 and return
    # the minimum of them. If k is even
    # then it can be reached by 2*k operations
    # and If k is odd then it can be reached
    # by k-1 operations so try both cases
    # and return the minimum of them
    dp[k] = 1 + min(minOperations(2 * k),
                    minOperations(k - 1))
    return dp[k]
# Driver code
if __name__ == '__main__':
    n = 4
    m = 6
    print(minOperations(n))
# This code is contributed by ashutosh450


C


// C# implementation of the approach
using System;
using System.Linq;
class GFG
{
    static int N = 10000;
    static int n, m;
    static int[] dp = Enumerable.Repeat(-1, N).ToArray();
    // Function to return the minimum
    // number of given operations
    // required to convert n to m
    static int minOperations(int k)
    {
        // If k is either 0 or out of range
        // then return max
        if (k <= 0 || k >= 10000)
            return 1000000000;
        // If k = m then conversion is
        // complete so return 0
        if (k == m)
            return 0;
        dp[k] = dp[k];
        // If it has been calculated earlier
        if (dp[k] != -1)
            return dp[k];
        dp[k] = 1000000000;
        // Call for 2*k and k-1 and return
        // the minimum of them. If k is even
        // then it can be reached by 2*k operations
        // and If k is odd then it can be reached
        // by k-1 operations so try both cases
        // and return the minimum of them
        dp[k] = 1 + Math.Min(minOperations(2 * k),
                             minOperations(k - 1));
        return dp[k];
    }
    // Driver Code
    public static void Main(String[] args)
    {
        n = 4;
        m = 6;
        //Arrays.fill(dp, -1);
        Console.Write(minOperations(n));
    }
}
// This code is contributed by
// Mohit kumar 29


java 描述语言


<script>
    let N = 10000;
    let n, m;
    let dp = new Array(N);
    function minOperations(k)
    {
        // If k is either 0 or out of range
        // then return max
        if (k <= 0 || k >= 10000)
            return 1000000000;
        // If k = m then conversion is
        // complete so return 0
        if (k == m)
            return 0;
        dp[k] = dp[k];
        // If it has been calculated earlier
        if (dp[k] != -1)
            return dp[k];
        dp[k] = 1000000000;
        // Call for 2*k and k-1 and return
        // the minimum of them. If k is even
        // then it can be reached by 2*k operations
        // and If k is odd then it can be reached
        // by k-1 operations so try both cases
        // and return the minimum of them
        dp[k] = 1 + Math.min(minOperations(2 * k),
                             minOperations(k - 1));
        return dp[k];
    }
    // Driver Code
    n = 4;
    m = 6;
    for(let i = 0; i < dp.length; i++)
    {
        dp[i] = -1;
    }
    document.write(minOperations(n));
// This code is contributed by unknown2108
script>

Output: 

2


推荐阅读
  • 本文为Codeforces 1294A题目的解析,主要讨论了Collecting Coins整除+不整除问题。文章详细介绍了题目的背景和要求,并给出了解题思路和代码实现。同时提供了在线测评地址和相关参考链接。 ... [详细]
  • Java太阳系小游戏分析和源码详解
    本文介绍了一个基于Java的太阳系小游戏的分析和源码详解。通过对面向对象的知识的学习和实践,作者实现了太阳系各行星绕太阳转的效果。文章详细介绍了游戏的设计思路和源码结构,包括工具类、常量、图片加载、面板等。通过这个小游戏的制作,读者可以巩固和应用所学的知识,如类的继承、方法的重载与重写、多态和封装等。 ... [详细]
  • 本文介绍了使用Java实现大数乘法的分治算法,包括输入数据的处理、普通大数乘法的结果和Karatsuba大数乘法的结果。通过改变long类型可以适应不同范围的大数乘法计算。 ... [详细]
  • 本文介绍了一个Java猜拳小游戏的代码,通过使用Scanner类获取用户输入的拳的数字,并随机生成计算机的拳,然后判断胜负。该游戏可以选择剪刀、石头、布三种拳,通过比较两者的拳来决定胜负。 ... [详细]
  • Commit1ced2a7433ea8937a1b260ea65d708f32ca7c95eintroduceda+Clonetraitboundtom ... [详细]
  • Java容器中的compareto方法排序原理解析
    本文从源码解析Java容器中的compareto方法的排序原理,讲解了在使用数组存储数据时的限制以及存储效率的问题。同时提到了Redis的五大数据结构和list、set等知识点,回忆了作者大学时代的Java学习经历。文章以作者做的思维导图作为目录,展示了整个讲解过程。 ... [详细]
  • 本文讨论了如何优化解决hdu 1003 java题目的动态规划方法,通过分析加法规则和最大和的性质,提出了一种优化的思路。具体方法是,当从1加到n为负时,即sum(1,n)sum(n,s),可以继续加法计算。同时,还考虑了两种特殊情况:都是负数的情况和有0的情况。最后,通过使用Scanner类来获取输入数据。 ... [详细]
  • 本文介绍了九度OnlineJudge中的1002题目“Grading”的解决方法。该题目要求设计一个公平的评分过程,将每个考题分配给3个独立的专家,如果他们的评分不一致,则需要请一位裁判做出最终决定。文章详细描述了评分规则,并给出了解决该问题的程序。 ... [详细]
  • 本文主要解析了Open judge C16H问题中涉及到的Magical Balls的快速幂和逆元算法,并给出了问题的解析和解决方法。详细介绍了问题的背景和规则,并给出了相应的算法解析和实现步骤。通过本文的解析,读者可以更好地理解和解决Open judge C16H问题中的Magical Balls部分。 ... [详细]
  • 本文介绍了如何在给定的有序字符序列中插入新字符,并保持序列的有序性。通过示例代码演示了插入过程,以及插入后的字符序列。 ... [详细]
  • baresip android编译、运行教程1语音通话
    本文介绍了如何在安卓平台上编译和运行baresip android,包括下载相关的sdk和ndk,修改ndk路径和输出目录,以及创建一个c++的安卓工程并将目录考到cpp下。详细步骤可参考给出的链接和文档。 ... [详细]
  • 本文讨论了一个关于cuowu类的问题,作者在使用cuowu类时遇到了错误提示和使用AdjustmentListener的问题。文章提供了16个解决方案,并给出了两个可能导致错误的原因。 ... [详细]
  • 本文介绍了P1651题目的描述和要求,以及计算能搭建的塔的最大高度的方法。通过动态规划和状压技术,将问题转化为求解差值的问题,并定义了相应的状态。最终得出了计算最大高度的解法。 ... [详细]
  • 本文详细介绍了Java中vector的使用方法和相关知识,包括vector类的功能、构造方法和使用注意事项。通过使用vector类,可以方便地实现动态数组的功能,并且可以随意插入不同类型的对象,进行查找、插入和删除操作。这篇文章对于需要频繁进行查找、插入和删除操作的情况下,使用vector类是一个很好的选择。 ... [详细]
  • 微软头条实习生分享深度学习自学指南
    本文介绍了一位微软头条实习生自学深度学习的经验分享,包括学习资源推荐、重要基础知识的学习要点等。作者强调了学好Python和数学基础的重要性,并提供了一些建议。 ... [详细]
author-avatar
缺心眼的小L
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有