給定條件找最小值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.pswp.cn/news/379596.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/379596.shtml
英文地址,請注明出處:http://en.pswp.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…