導航:首頁 > 源碼編譯 > 演算法實現走樓梯

演算法實現走樓梯

發布時間:2022-08-11 02:14:09

㈠ C++爬樓梯,遞歸函數的使用

(1)首先我們建立一個函數,判斷函數的合法性

[cpp] view plain
void jump_ladder(int layer, int* stack, int* top)
{
if(layer <= 0)
return;

return;
}

(2)判斷當前的層數是為1或者是否為2

[cpp] view plain
void jump_ladder(int layer, int* stack, int* top)
{
if(layer <= 0)
return;

if(layer == 1){
printf_layer_one(layer, stack, top);
return;
}

if(layer == 2){
printf_layer_two(layer, stack, top);
return;
}

return;
}
(3)對於2中提及的列印函數進行設計,代碼補全

[cpp] view plain
#define GENERAL_PRINT_MESSAGE(x)\
do {\
printf(#x);\
for(index = (*top) - 1 ; index >= 0; index --)\
printf("%d", stack[index]);\
printf("\n");\
}while(0)

void printf_layer_one(int layer, int* stack, int* top)
{
int index ;
GENERAL_PRINT_MESSAGE(1);
}

void printf_layer_two(int layer, int* stack, int* top)
{
int index;

GENERAL_PRINT_MESSAGE(11);
GENERAL_PRINT_MESSAGE(2);
}
註: a)代碼中我們使用了宏,注意這是一個do{}while(0)的結構,同時我們對x進行了字元串強轉

b)當剩下台階為2的時候,此時有兩種情形,要麼一次跳完;要麼分兩次

(4)當階梯不為1或者2的時候,此時需要遞歸處理

[cpp] view plain
void _jump_ladder(int layer, int* stack, int* top, int decrease)
{
stack[(*top)++] = decrease;
jump_ladder(layer, stack, top);
stack[--(*top)] = 0;
}

void jump_ladder(int layer, int* stack, int* top)
{
if(layer <= 0)
return;

if(layer == 1){
printf_layer_one(layer, stack, top);
return;
}

if(layer == 2){
printf_layer_two(layer, stack, top);
return;
}

_jump_ladder(layer- 1, stack, top, 1);
_jump_ladder(layer- 2, stack, top, 2);
}
這里在函數的結尾添加了一個函數,主要是遞歸的時候需要向堆棧中保存一些數據,為了代碼簡練,我們重新定義了一個函數。

總結:
1)這道題目和斐波那契數列十分類似,是一道地地道道的遞歸題目
2)遞歸的函數也需要好好測試,使用不當,極容易堆棧溢出或者死循環。對此,我們可以按照參數從小到大的順序依次測試,比如說,可以測試樓梯為1、2、3的時候應該怎麼運行,同時手算和程序相結合,不斷修正代碼,完善代碼。

㈡ c語言 設計 爬樓梯的方法

我是學pascal的~只說得上來演算法
你說的
爬樓梯
是指有n階樓梯,每次可以上1,2……,p階(1<=p<=n),問走到最上面有多少種不同的走法吧?
這個就是
遞推
啊~
設上i級台階共有f(i)種不同的方法,很簡單就可以知道f(1)=1,f(2)=2……
當i大於2時,分n種情況討論:第一步上了1級台階,第一步上了2級台階,……第一步上了n級台階。
如果第一步上了1級樓梯,那麼還剩下i-1級樓梯,要走完這i-1級樓梯,一共有f(i-1)種方法。
如果第一步上了2級樓梯,那麼還剩下i-2級樓梯,要走完這i-2級樓梯,一共有f(i-2)種方法。
……
如果第一步上了n級樓梯,那麼還剩下i-n級樓梯,要走完這i-2級樓梯,一共有f(i-n)種方法。
所以,在第一種情況下有f(i-1)種不同走法,第二種情況有f(i-2)種不同走法……這n種情況既沒有重復方案,也沒有遺漏,因此得出f(i)=f(i-1)+f(i-2)+……+f(i-n)
接著就行了,200階樓梯都不成問題。

python爬樓梯求至少多少階梯

假設你正在爬樓梯。需要 n階你才能到達樓頂。每次你可以爬 1 或 2 個台階。

注意:給定 n 是一個正整數。

示例 1:

輸入: 2

輸出: 2

解釋: 有兩種方法可以爬到樓頂。1 階 + 1 階 和 2 階

解題思路:

實現了兩種方法,但是第一種超出時間限制(。ì _ í。),因為遞歸的時候方法實際計算了兩次。兩種方法都使用了動態規劃思想,比如對於爬10階樓梯,我們最後一步爬上第10階只會有兩種情況,一種是從9階樓梯爬1個台階,一種是從8階台階爬2兩個台階上來。所以10階台階問題可以劃分為爬9階和8階兩個子問題,一直遞歸劃分到只剩2階(2種方法)和1階(一種方法)。

超出時間限制的代碼:

class Solution:

def climbStairs(self, n: int) -> int:

if n<=2:

if n==2:

㈣ 幼兒園爬樓梯演算法什麼意思

爸爸媽媽要注意觀察,當寶寶練至身體微微出汗時就應該停止,以防止運動過度,讓寶寶厭倦並以後討厭走樓梯這個活動,同時也可以防止過度的訓練影響寶寶身體骨骼的發育。

㈤ C語言 爬樓梯的問題

用遞歸解決比較方便:
#include<stdio.h>

int fibonacci (int n)
{
if (n > 2)
return fibonacci(n - 1) + fibonacci(n - 2);
else
return 1;
}

int main()
{
int data[20];
int t;
int i;

printf ("Please input T and the nums: \n");
scanf ("%d", &t);
for (i=0; i<t; i++)
{
scanf("%d", &data[i]);
}
printf("The output: \n");
for (i=0; i<t; i++)
{
printf("%d\n", fibonacci (data[i]+1));
}

return 0;
}
/* 這是斐波那契演算法的一個應用,你可以搜索一下 */

㈥ 演算法設計 爬樓梯類型,共n階樓梯,一次最多爬m階,用JAVA或者C或者C++

循環,,,,,好多循環,,,,哇,,,,實現了
#include<string.h>
#include<iostream>
#include<stack>
using namespace std;

int main()
{
int n = 0, m = 0, sum = 0, cnt = 0;

cout << "輸入階梯數:" << endl;
cin >> n;
cout << "輸入最多爬的階梯數:" << endl;
cin >> m;

if (n <= 0)
{
cout << "就一種,上去了!" << endl;
}
if (m <= 0)
{
cout << "你想上是不可能的!" << endl;
}
int i;
stack<int> sk;

do
{

if (sum <= n)
{
++cnt;

while (sum <= n)
{
sk.push(1);
++sum;
}
}

if (!sk.empty())
{
sum -= sk.top();
sk.pop();
}
else
{
cout << cnt << endl;
return 0;
}
HHH:
if (!sk.empty())
{
i = sk.top();
}
else
{
cout << cnt << endl;
return 0;
}

if (i < m)
{
++i;
}
else
{
if (!sk.empty())
{
sum -= sk.top();
sk.pop();
goto HHH;
}
else
{
cout << cnt << endl;
return 0;
}

}
if (!sk.empty())
{
sum -= sk.top();
sk.pop();
}
else
{
cout << cnt << endl;

return 0;
}
sk.push(i);
sum += i;

} while (1);

return 0;
}

㈦ A/B/C三人同時由地面爬樓梯登上一座高塔

由題可知:
設A/B/C三人所需的步數分別為:a/b/c ,台階數為X, 則有
X=3a+2=4b+3=5c+4

分析:台階數不可同時整除3,4,5,則反推,可同時整除3,4,5的最小數為:3*4*5=60,由於台階數在100~150之間,則可同時整除3,4,5的數為120;

由於三人最後一步只比每步的台階數少1,則可得台階數X=120-1=119

㈧ 爬樓梯問題--有n階台階,上樓可以一步上1階,2階,3階,計算共有多少種不同的走法

簡單的dp問題:
#include
int step[2048];
int* p = step;
void foo(int n)
{
if(n > 0) {
if(n > 1) {
*p++ = 2;
foo(n-2);
--p;
}
*p++ = 1;
foo(n-1);
--p;
} else {
for(int* k = step; k != p; ++k)
printf("%d ",*k);
putchar('\n');
}
}
int main()
{
foo(5);
}

閱讀全文

與演算法實現走樓梯相關的資料

熱點內容
c語言平均數字編譯錯誤 瀏覽:170
單片機算交流 瀏覽:45
php自適應網站 瀏覽:467
2b2t伺服器怎麼獲得許可權 瀏覽:815
c語言javaphp 瀏覽:804
程序員技術不分高低嗎 瀏覽:619
dos不是內部或外部命令 瀏覽:708
PC機與單片機通訊 瀏覽:675
二級加密圖 瀏覽:113
壓縮機異音影響製冷嗎 瀏覽:711
德斯蘭壓縮機 瀏覽:490
程序員太極拳視頻 瀏覽:531
網上購買加密鎖 瀏覽:825
安卓為什麼軟體要隱私 瀏覽:83
虛擬主機管理源碼 瀏覽:811
java圖形圖像 瀏覽:230
單片機輸出口電平 瀏覽:486
java配置資料庫連接 瀏覽:479
java多態的體現 瀏覽:555
java的split分隔符 瀏覽:128