给定条件找最小值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语言程序

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/379596.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

提高C#编程水平不可不读的50个要诀

提高C#编程水平的50个要点 1.总是用属性 (Property) 来代替可访问的数据成员 2.在 readonly 和 const 之间&#xff0c;优先使用 readonly 3.在 as 和 强制类型转换之间&#xff0c;优先使用 as 操作符 4.使用条件属性 (Conditional Attributes) 来代替条件编译语句 #if 5.总是…

那个年代的苏联歌曲

小时候&#xff0c;不时听父亲提起电影《这里的黎明静悄悄》&#xff0c;怎么也想不到如此美丽的名字为什么要和战争联系起来。后来在大学看了这部电影之后&#xff0c;开始认为这名字是合适的&#xff0c;因为电影讲的是女性——战场中的女性&#xff0c;各自都怀揣着爱情去保…

linux系统编程---进程总结

进程控制总结1 进程创建的三种方式forkvfrokclone2 进程终止进程正常退出returnexit_exit进程异常退出进程收到某个信号&#xff0c;而该信号使进程终止abort3 进程等待进程等待的方法waitwaitpid4 进程替换替换原理替换函数制作一个简单的shell1 进程创建的三种方式 参考文章…

银行账务转账系统(事务处理)

流程如下&#xff1a; 创建项目工程如下&#xff1a; transfer包下的代码如下&#xff1a; package beyond.transfer.dao;import java.sql.Connection; import java.sql.SQLException;import org.apache.commons.dbutils.QueryRunner;import beyond.utils.DataSourceUtils;pu…

【msdn wpf forum翻译】TextBox中文本 中对齐 的方法

原文链接&#xff1a;http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/49864e35-1dbf-4292-a361-93f1a8400558问题&#xff1a;TextBox中文本中对齐&#xff0c;使用 TextBox.HorizontalContentAlignment"Center"行不通&#xff08;TextBox.VerticalConte…

wifi操作及实例

1.什么事WIFI 利用无线路由器上网的协议2.获取WIFI网卡的状态 WIFI网卡的状态是由一系列的整形常量来表示的 有状态&#xff1a; 网卡不可用WIFI_STATE_DISABLED 对应值为1 网卡正在关闭WIFI_STATE_DISABLING 对应值为0 网卡可用WIFI_STATE_ENABLED 对应的值为3 …

c语言 函数的参数传递示例_C语言中带有示例的remove()函数

c语言 函数的参数传递示例C语言中的remove()函数 (remove() function in C) The remove() function is defined in the <stdio.h> header file. remove()函数在<stdio.h>头文件中定义。 Prototype: 原型&#xff1a; int remove(const char* filename);Parameter…

使用ThreadLocal绑定连接资源(事务)

dao层代码如下&#xff1a; package beyond.transfer.dao;import java.sql.Connection; import java.sql.SQLException;import org.apache.commons.dbutils.QueryRunner;import beyond.utils.DataSourceUtils; import beyond.utils.MyDataSourceUtils;public class TransferDa…

算法---栈和队列

栈和队列1 栈栈的顺序存储栈的链式存储2 队列队列的顺序存储队列的链式存储3 栈和队列的应用用栈实现队列用队列实现栈最小栈1 栈 参考文章&#xff1a; https://zhuanlan.zhihu.com/p/346164833 https://zhuanlan.zhihu.com/p/120965372#:~:text%E6%A0%88%E6%98%AF%E4%B8%80%…

学习网站LIST

面向对象的脚本语言Rubyhttp://rubycn.ce-lab.net/20020101.htmlRUBY文档中心http://www.moer.net/ruby/doc/TCL脚本http://www.tclchina.com/Python快速入门http://wiki.woodpecker.org.cn/moin/WeiZhong/2006-01-17Python 研究(Dive Into Python)http://www.woodpecker.org.c…

再次参加(第七届)商学院徒步戈壁挑战赛,赋词几首

2012年5月21-25日&#xff0c;再次踏上甘肃莫贺延碛戈壁&#xff0c;参加第七届商学院徒步戈壁挑战赛。时隔五年&#xff0c;时空转换。 少年游 ——戈壁缘 江南物华&#xff0c;远水碧山&#xff0c;灯火相掩映。暮宴朝欢&#xff0c;酒绿灯红&#xff0c;踯躅夜归人。 孤城落…

Java StackTraceElement toString()方法与示例

StackTraceElement类的toString()方法 (StackTraceElement Class toString() method) toString() method is available in java.lang package. toString()方法在java.lang包中可用。 toString() method is used to represent stack trace element as a string or in other word…

增删改查

web层代码如下&#xff1a; package beyondwsq.web;import java.io.IOException; import java.sql.SQLException; import java.util.List;import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; imp…

在WebBrowser中通过模拟键盘鼠标操控网页中的文件上传控件

引言 这两天沉迷了Google SketchUp&#xff0c;刚刚玩够&#xff0c;一时兴起&#xff0c;研究了一下WebBrowser。 我在《WebBrowser控件使用技巧分享》一文中曾谈到过“我现在可以通过WebBrowser实现对各种Html元素的操控&#xff0c;唯独无法控制Html的上传控件”&#xff0c…

编写最简单的字符设备驱动

编写最简单的字符设备驱动1 编写驱动代码2 编写makefile3 编译和加载驱动4 编写应用程序测试驱动参考文章&#xff1a; linux驱动开发第1讲&#xff1a;带你编写一个最简单的字符设备驱动 linux驱动开发第2讲&#xff1a;应用层的write如何调用到驱动中的write 1 编写驱动代码…

Java ObjectStreamField toString()方法与示例

ObjectStreamField类toString()方法 (ObjectStreamField Class toString() method) toString() method is available in java.io package. toString()方法在java.io包中可用。 toString() method is used to return a string that defines this field. toString()方法用于返回定…

linux内核文件描述符fd、文件索引节点inode、文件对象file关系

文件描述符fd、文件索引节点inode、文件对象file关系1 VFS对象1.1 超级块对象1.2 索引节点对象1.3 文件对象1.4 进程描述符1.5 files_struct2 如何根据文件描述符fd找到文件&#xff1f;1 VFS对象 在说fd、inode和file关系之前&#xff0c;我们先了解VFS的几个概念。分别是进程…

sql2005 获取表字段信息和视图字段信息

获取表字段名,和字段说明SELECT[Table Name]OBJECT_NAME(c.object_id), [ColumnName]c.name, [Description]ex.value FROMsys.columns c LEFTOUTERJOINsys.exte…

解析css之position

CSS的很多其他属性大多容易理解&#xff0c;比如字体&#xff0c;文本&#xff0c;背景等。有些CSS书籍也会对这些简单的属性进行大张旗鼓的介绍&#xff0c;而偏偏忽略了对一些难缠的属性讲解&#xff0c;有避重就轻的嫌疑。CSS中主要难以理解的属性包括盒型结构&#xff0c;以…

Java ObjectInputStream readLong()方法(带示例)

ObjectInputStream类readLong()方法 (ObjectInputStream Class readLong() method) readLong() method is available in java.io package. readLong()方法在java.io包中可用。 readLong() method is used to read 8 bytes (i.e. 64 bit) of long value from this ObjectInputSt…