網路磁帶機

HOME

資料結構使用C :用遞迴方程式寫出abc之排列

格式要遵照下面這兩行

 void permute(const string &str);
 void permute(const string &str, int low, int high); // 使用遞迴

第一個程序是一個驅動程式用來呼叫第二個程序並且在string str印出所有排列字元
例如如果str="abc",就要輸出 abcacb、bac、bca、cab、cba

此問題的 Base case 和 Design rule 我想好久...
但是因為這是初次碰到遞迴問題...
(教科書範例看的懂,但是自己寫寫不出來)

P.S.
附上原題目給大家參考:
The first routine is a driver that calls the second and prints all the permutations of the characters in string str.
If str is “abc”, then the strings that are output are abc, acb, bac, bca, cab, and cba.
Use 〝recursion〞 for the second routine.
名詞解釋
Base case:solve without recursion
Design rule:Assume that all the recursive calls work
liwe
permute.cpp


/** Webcpp v0.7.0+ compatible StyleSheet http://webcpp.sf.net **/
/** Theme: ide-devcpp **/

body
{
background-color: #ffffff
}

a:link {color:#ff0000}
a:visited {color:#000080}
a:active {color:#000000}
a:hover {color:#008000}

pre
{
color: #000000
}

font
{
font-size:100%
}

font.preproc
{
color: #008000
}

font.numbers
{
color: #0000ff
}

font.strings
{
color: #ff0000
}

font.keyword
{
color: #000000;
font-weight: bold
}

font.comment
{
color: #000080;
font-style: italic
}






#include <iostream>
#include <string>


using std::string;

void permute(const string &);
void permute(const string &,int,int);


using std::cout;

int main()
{

string str("abc");

permute(str);

return 0;
}


void permute(const string &str)
{
permute(str,0,str.length());
}


void permute(const string &str,int low,int high)
{

string strx = str;

if (low == high -1)
cout << str << " ";
else {
for(int i=low;i < high ;++i) {

char temp = strx[i];
strx[i] = strx[low];
strx[low] = temp;

permute(strx,low+1,high);

strx[low] = strx[i];
strx[i] = temp;
}
}
}
UP TO DATE BLOG
資料結構使用C :用遞迴方程式寫出abc之排列
請問台灣重要五條?河川?是哪幾條??
請問有人去過長春藤身心健康管理中心嗎?
金露花的修剪~
可以隨時幫女兒寫寶寶日記的電子產品?
哪裡有賣串珠珠的東西?
聽說過年的旅遊團機位搶都搶不到???
請問要怎麼去史帝夫厄文開的澳洲動物園
?什麼吃太多精緻食品不好?! EX: 精緻米...
AIO達人嚴選人氣人類人氣日記
問三太子遊戲歌曲
關於寒假短期遊學
埃及的紙草是用什麼植物做的?
有關於The Rose這首歌的歷史
撘公車悠遊卡發出的嗶聲,司機要如何分辨?
可樂旅遊韓國飯店吃早餐的問題
請問國家公園警察、林務局、要如何進去
請給我長恨歌的詩的內容
蔣公誕辰紀念日(10/31)在週休二日實行前是國定假日嗎?
靜脈區長治療方式
LINK BLOG


Comment
Title:
Url:
Validate:
Validate
 
Powered by 網路磁帶機© 2005-2008