C語言實(shí)現(xiàn)分治法實(shí)例
本文為大家分享了C語言實(shí)現(xiàn)分治法實(shí)例代碼,供大家參考,具體內(nèi)容如下
使用分治法求最大值
這個函數(shù)將數(shù)組a[l]...a[r]分成a[l],...,a[m]和a[m+1],...a[r]兩部分,分別求出每一部分的最大元素(遞歸地),并返回較大的那一個作為整個數(shù)組的最大元素.如果數(shù)組大小是偶數(shù),則兩部分大小相等;如果是奇數(shù),第一部分比第二部分的大小大1.
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <malloc.h>
using namespace std;
#define OK 1
#define ERROR -1
#define TRUE 1
#define FALSE 0
typedef int Status;
int Max(int a[], int l, int r)
{
int u, v, m = (l + r) / 2;
//當(dāng)區(qū)間中只有一個元素,遞歸終止,并將該元素返回
if(l == r)
return a[l];
//遞歸原區(qū)域的左邊
u = Max(a, l, m);
//遞歸原區(qū)域的右邊
v = Max(a, m+1, r);
//返回最大值
return (u>v)?u:v;
}
int main()
{
//舉例驗(yàn)證
int a[7] = {6, 5, 3, 4, 7, 2, 1};
int maxx = Max(a, 0, 6);
printf("%d\n", maxx);
return 0;
}
漢諾塔的解
我們把盤子(遞歸地)移動到c上的方案是,將除了最下面的盤子之外的所有盤子移到b上,然后將做下面的盤子移到c上,然后(遞歸地)再將其他盤子移回到最下面的盤子上面.
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <malloc.h>
using namespace std;
#define OK 1
#define ERROR -1
#define TRUE 1
#define FALSE 0
typedef int Status;
//輸出盤子的移動
void shift(int n, char x, char y)
{
printf("Move %d disk: %c ---------> %c\n", n, x, y);
}
void hanoi(int n, char a, char b, char c)
{
//遞歸終止的條件
if(n == 1)
{
//將a上最下面的盤子移到c上
shift(n, a, c);
return;
}
//以c為中間軸,將a上的盤子移動到b上
hanoi(n-1, a, c, b);
shift(n, a, c);
//以a為中間軸,將b上的盤子移動到c上
hanoi(n-1, b, a, c);
}
int main()
{
//舉例驗(yàn)證
hanoi(4, 'a', 'b', 'c');
return 0;
}
使用分治法在尺子上畫刻度
要在尺子上畫刻度線,我們首先在左半邊畫刻度線,然后在中間畫一條最長的刻度線,最后在右半邊畫刻度線.
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <malloc.h>
using namespace std;
#define OK 1
#define ERROR -1
#define TRUE 1
#define FALSE 0
typedef int Status;
//畫線
void mark(int m, int h)
{
//由于無法實(shí)際表示刻度線之間的高度差,故用實(shí)際數(shù)字來體現(xiàn)
printf("%d ", h);
}
//劃分該區(qū)域內(nèi)的刻度
void rule(int l, int r, int h)
{
//找到該區(qū)域的中間
int m = (l + r) / 2;
//當(dāng)高度大于0
if(h)
{
//劃分小區(qū)域
rule(l, m, h-1);
//畫線
mark(m, h);
//劃分小區(qū)域
rule(m+1, r, h-1);
}
}
int main()
{
//舉例驗(yàn)證
rule(0, 14, 4);
return 0;
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。
上一篇:深入淺析c/c++ 中的static關(guān)鍵字
欄 目:C語言
下一篇:C++使用異或運(yùn)算實(shí)現(xiàn)交換兩個數(shù)的值
本文標(biāo)題:C語言實(shí)現(xiàn)分治法實(shí)例
本文地址:http://www.jygsgssxh.com/a1/Cyuyan/681.html
您可能感興趣的文章
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用函數(shù)刪除字符
- 04-02c語言的正則匹配函數(shù) c語言正則表達(dá)式函數(shù)庫
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言中對數(shù)函數(shù)的表達(dá)式 c語言中對數(shù)怎么表達(dá)
- 04-02c語言用函數(shù)寫分段 用c語言表示分段函數(shù)
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排序法函數(shù)
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段函數(shù)
- 04-02C語言中怎么打出三角函數(shù) c語言中怎么打出三角函數(shù)的值
- 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求階乘


閱讀排行
本欄相關(guān)
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言的正則匹配函數(shù) c語言正則表達(dá)
- 04-02c語言用函數(shù)寫分段 用c語言表示分段
- 04-02c語言中對數(shù)函數(shù)的表達(dá)式 c語言中對
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段
- 04-02C語言中怎么打出三角函數(shù) c語言中怎
- 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求
隨機(jī)閱讀
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 04-02jquery與jsp,用jquery
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 01-10C#中split用法實(shí)例總結(jié)
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 01-10delphi制作wav文件的方法
- 01-11ajax實(shí)現(xiàn)頁面的局部加載
- 08-05DEDE織夢data目錄下的sessions文件夾有什


