C++3個漢諾塔遞歸問題

3個漢諾塔問題A—>C

移動次數2^n-1

  1. hannoni(int n,char a,char b,char c)把n個盤子借助b,從a移動到c
  2. move(int n,char a,char c)把第n個盤子,從a移動到c
#include<iostream> 
#include<cmath>
using namespace std;
//漢諾塔問題A--->C
//2^n-1次移動次數 //把第n個盤子從a移動到c 
void move(int n,char a,char c)
{cout  <<"把"<<n<<"號盤子從"<<a<<"移到"<<c<<endl; 
}
//把n個盤子a借助b移到c 
void hannoni(int n,char a,char b,char c)
{if(n==1)//只需要移動一次 move(1,a,c);else{//a-->b   n-1hannoni(n-1,a,c,b);move(n,a,c);//b--->c n-1hannoni(n-1,b,a,c);
}
} 
int main()
{int n;cout <<"請輸入要移動的盤子數"<<endl; cin>>n;hannoni(n,'A','B','C');return 0;
}

要點:A—>C
1.當n==1時直接從A移動到C
2.當n>1時,
先把n-1個盤子借助C,從A移動到B
再把第n個盤子從A移動到C
最后,借助A,把n-1個盤子從B移動到C

漢諾塔移動次數

要點:A—>C
1.當n==1時直接從A移動到C 移動一次
2.當n>1時,
先把n-1個盤子借助C,從A移動到B 移動n-1次
再把第n個盤子從A移動到C 移動一次
最后,借助A,把n-1個盤子從B移動到C 移動n-1次
所以,
n=1,T(N)=1
n>1,T(N)=2T(N-1)+1

代碼示例

#include<iostream>
#include<cmath>
using namespace std;int count(int n){int number=0;if(n<=0)return -1;if(n==1)number=1;elsenumber=2*count(n-1)+1;return number;}int main()
{int n;cout <<"輸入要移動的盤子個數"<<endl; cin>>n;cout<<count(n)<<endl;return 0;} 

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/446818.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/446818.shtml
英文地址,請注明出處:http://en.pswp.cn/news/446818.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

clion編譯器解決undefined reference to symbol ‘shm_open@@GLIBC_2.2.5‘

修改CMakelists文件 cmake_minimum_required(VERSION 3.17) project(mutex_learn)set(CMAKE_CXX_STANDARD 14)set(BOOST_ROOT "/usr/local/include/boost") #添加頭文件搜索路徑 include_directories(/usr/local/include) #添加庫文件搜索路徑 link_directories(/us…

Android 攔截或屏蔽返回鍵

在Android開發中我們常常會遇到需要攔截或屏蔽返回鍵的需求&#xff0c;對攔截后的返回鍵進行特殊操作。 監聽返回鍵有兩種方式 1、重寫OnBackPressed方法 Overridepublic void onBackPressed() {// 完全由自己控制返回鍵邏輯&#xff0c;系統不再控制&#xff0c;但是有個前…

C++遞歸斐波那契數列

第一種 //斐波那契數列 // 0 1 1 … //從第1個開始 代碼 #include<iostream> #include<cmath> using namespace std; //斐波那契數列 // 0 1 1 ... //從第1個開始 int f(int n) {int m; if(n1)return 0;if(n2)return 1;elsemf(n-1)f(n-2);return m;} int mai…

Android onActivityResult中requestCode與resultCode區別

想要了解requestCode與resultCode的區別&#xff0c;我們需要先了解以下三個方法的用法&#xff1a; startActivityForResult(Intent intent, Int requestCode) setResut(int resultCode, Intent intent) onActivityResult(int requestCode, int resultCode, Intent intent) …

使用named_mutex和named_condition配合實現讀寫鎖

代碼 代碼的名稱是read_write_mutex.h初步優化如果涉及到進程掛掉了&#xff0c;造成進程堵塞&#xff0c;如何解決&#xff1f;還未涉及 #include <boost/interprocess/sync/named_condition.hpp> #include <boost/interprocess/sync/named_mutex.hpp>namespace …

C++的輸入與輸出

輸入與輸出 輸入:從外部輸入設備(鍵盤)向計算機輸入數據 輸出:從計算機向外部輸出設備(顯示屏)輸出數據 C使用流對象實現 使用流對象cin與cout,將標準輸入輸出流庫的頭文件iostream包含到源文件 #include<iostream>//標準輸入輸出庫 using namespace std;//使用標準命…

Android 動態計算ListView的高度

目錄一、簡介二、效果圖三、代碼實現一、簡介 在Android開發的過程中有的時候我們需要手動計算ListView的高度&#xff0c;比如說&#xff0c;ScrollView中嵌套ListView的時候&#xff0c;我們就需要手動精確計算ListView的高度了。 如果ListView的Item高度是固定的話還好計算…

DjangoRestFramework(drf實現五個接口)

安裝&#xff1a;pip install djangorestframework 在使用drf之前&#xff0c;先 使用原生Django實現5個接口 models.py from django.db import modelsclass Book(models.Model):namemodels.CharField(max_length53)pricemodels.IntegerField() views.py from app01 impor…

linux使用共享內存進行進程通信

一、什么是共享內存 共享內存就是允許兩個不相關的進程訪問同一個邏輯內存。共享內存是在兩個正在運行的進程之間共享和傳遞數據的一種非常有效的方式。不同進程之間共享的內存通常為同一段物理內存。使用共享內存進行通信的進程都需要同一段共享內存連接到它們自己的地址空間…

安卓TextView文本框與自定義邊框

常用屬性 自定義邊框 基本使用 <?xml version"1.0" encoding"utf-8"?> <shape xmlns:android"http://schemas.android.com/apk/res/android"android:shape"rectangle矩形/ring圓環/oval橢圓/line直線"當為圓環時android:s…

Android RecyclerView實現九宮格效果

RecyclerView更加優化的復用機制和方便實現UI效果&#xff0c;幾乎替代Listview和GridView的使用。但是分割線的實現&#xff0c;需要自己繼承ItemDecoration來繪制。 完整代碼已上傳至Github&#xff1a;RecyclerView實現九宮格效果 效果圖 item的布局文件 <?xml versi…

如何讀取指針指向的地址空間呢?

方法 使用%p 接收指針返回的地址空間 代碼 #include <stdio.h> #include <stdlib.h>int main() {int a 100;int *a_p &a;printf("%p\n",&a);//輸出&#xff1a;002AF744 輸出的是a變量的地址printf("%p\n",a_p);//輸出&#xff1…

科學究研明表,漢字序順并不一定影閱響讀

有個很有意思的現象&#xff1a; 不信你就來試試 中文打亂小工具 github地址&#xff1a;在線打亂文字順序

安卓EditText

常用屬性 android:textAllCaps"false"去除大寫狀態 inputType 常用 textpassword密碼 number數字 phone撥號鍵盤 設置光標位置 editText.setSelection(2);從1開始 editText.setSelection(1,3);從1開始,1–3中間部分,一個范圍

完善博文 共享內存一寫多讀無鎖實現的代碼邏輯部分

使用共享內存(內存映射)實現發布訂閱模式 多進程實現PubSub發布訂閱模式&#xff0c;從而實現進程間的通信。通信方式可以是TCP/UDP&#xff0c;管道Pipe/消息隊列&#xff0c;共享內存shared memory等等。其中TCP/UDP的方式是可以用作局域網以及跨平臺的通信&#xff0c;Pipe…

想對你說的話,就在這里!

甜(Tu)言(Wei)蜜(Qing)語(Hua)最近在github上看到了一個朋友開發的 土味情話在線生成器 &#xff0c;感覺還不錯&#xff0c;在這里推薦一下。 github地址&#xff1a;在線生成土味情話

linux讀寫文件 簡單版

代碼 //write void write_file(const std::string file_name){FILE *fp nullptr;fp fopen(file_name.c_str(),"w");fprintf(fp,"This is testing for mutex\n");fclose(fp); } //read void read_file(const std::string file_name){std::ifstream fp(fi…

具有中國風的傳統顏色(炫酷)

一個小小的中國風的傳統顏色&#xff0c;你覺得應該是什么樣子的呢&#xff1f; 看了下面這個&#xff0c;我一個搞移動開發的都想去搞前端開發了。 廢話不多說了&#xff0c;直接看效果&#xff1a; 訪問地址&#xff1a;中國傳統顏色手冊 github地址&#xff1a;Chinese…

Android Studio安裝問題及填坑

安裝過程 安裝Android Studio 其他問題 1.Android Studio出現Error:Unable to tunnel through proxy. Proxy returns “HTTP/1.1 400 Bad Request” 2.Could not resolve all artifacts for configuration :classpath 3.!No cached version of com.android.tools.build:gr…

Linux strtol將十六進制轉化為十進制

代碼 #include <iostream> #include "crypto_util.h"int get_file(const std::string file_name){size_t get_file_id 0;std::cout << hsm::common::get_md5_digest_hex(file_name) << std::endl;get_file_id strtol(reinterpret_cast<const…