題目:見(jiàn)啊哈算法p58頁(yè),一開(kāi)始用暴力枚舉的方式寫(xiě)的,后p79頁(yè)介紹如何使用DFS做。很適合熟悉DFS的寫(xiě)法。 分析:按照套路來(lái)
void dfs(int step){ 判斷邊界; 嘗試每一種可能for(int i = 1 ; i <= n ; i++){ 繼續(xù)下一步dfs(step+1); } 返回; }代碼:
#include<iostream>#include<cstdio>using namespace std ;int total = 0 ;int a[10],book[10];void dfs(int step){//step表示現(xiàn)在站在第幾個(gè)盒子 if(step == 10){//如果站在第10個(gè)盒子面前,則表示前9個(gè)盒子已經(jīng)放好了撲克牌 //判斷是否滿足等式 if(a[1]*100+a[2]*10+a[3]+a[4]*100+a[5]*10+a[6]==a[7]*100+a[8]*10+a[9]){新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注