C 課後題 遞迴

2021-09-05 08:58:44 字數 2396 閱讀 9740

#include #include using namespace std;

void reverstnumber(int num)

//cout << endl;

}int main(int argc, char *argv)

#include #include using namespace std;

void reverstnumber(int num)

//cout << endl;

}// 這裡考慮寫乙個recursive helper function

void reversestring(const string& ss)

}int main(int argc, char *argv)

執行結果:

該問題可以分解為:

m(i)=i/(2i+1)+m(i-1)

停止條件為i>0

**:

#include #include using namespace std;

void reverstnumber(int num)

//cout << endl;

}// 這裡考慮寫乙個recursive helper function

void reversestring(const string& ss)

}double sumcascade(int n) // 求級數的前n項和

int main(int argc, char *argv)

執行結果:

-----------------------------------分割線----------------------------------------

又來更新啦

用遞迴的方式實現選擇排序:

**:

#include #include using namespace std;

// 遞迴的方法實現選擇排序

templatevoid selectsort(t list, int n)

} // 找到最大值與最大值的index

list[max_index] = list[n-1];

list[n-1] = max_number;

selectsort(list, n-1);

}}

int main(int argc, char *argv)

; selectsort(a, 8);

for(int i=0; i<8; i++)

cout << endl;

return 0;

}

執行結果:

用遞迴的方式寫氣泡排序:

一次冒泡的過程,數列的最大元素一定會移動到數列的尾部,這與選擇排序中尋找序列最大值並將其放到隊尾的操作一致,接著對前n-1個元素進行相同的操作,所以可以用遞迴的方式實現這一過程。

#include #include using namespace std;

templatevoid bulbsort(t list, int n)

}bulbsort(list, n-1);

}}int main(int argc, char *argv)

; // selectsort(a, 8);

bulbsort(a, 8);

for(int i=0; i<8; i++)

cout << endl;

return 0;

}

實現插入排序的非遞迴版本:

#include #include #include using namespace std;

templatevoid insertionsort(t list, int n) // 注意選擇排序的思路

list[j+1] = temp; // list[j+1] }}

int main(int argc, char *argv)

; insertionsort(a, 7);

for(int i=0; i<7; i++)

cout << endl;

return 0;

}

C 第七章課後題7 13

唯願你,所得皆所望,所失皆無礙 慢慢來,會好的 include include using namespace std struct sales data sales data const string s,unsigned n,double p bookno s units sold n reve...

c 第八章課後題

題目 定義乙個學生成績類 score,描述學生的私有資料成員為學號 no 姓名 name 8 數學 math 物理 phi 英語 eng 定義能輸入學生成績的公有成員函式 input 能計算學生總分的公有成員函式 sum 能顯示輸出學生成績的公有成員函式 show 原始碼 include inclu...

C語言 有關遞迴 非遞迴的題

include include pragma warning disable 4996 intmul int n,int k 函式實現 intmain 遞迴 include include pragma warning disable 4996 abcd 1 my strlen bcd bcd 1 ...