




版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
1、類型標識符類型標識符 函數(shù)名(形式參數(shù)表)函數(shù)名(形式參數(shù)表) 變量定義部分變量定義部分 語句部分語句部分 return 返回值;返回值; 或或 return(返回值);(返回值);eg. int max(int a, int b) if (ab) return(a) else return(b); 函數(shù)體函數(shù)體 * * * * *void printstar() cout “ *n”; cout “ *n”; cout “ *n”; cout “ *n”; cout “*n”;void printstar(int numOfLine) int i , j; for (i = 1; i = n
2、umOfLine; +i) cout endl; for (j = 1; j = numOfLine - i; +j) cout ; for (j = 1; j = 2 * i - 1; +j) cout “*”; int p(int n) int s=1, i; if (n 0) return(0); for (i = 1; i = n; +i) s *= i; return(s); bool IsLeapYear(int year) bool leapyear; leapyear = (year %4 = 0) &(year % 100 != 0) | (year % 400 = 0);
3、return (leapyear);#include int max(int a, int b); main() int x, y; cin x y; cout b) return(a); else return(b); 函數(shù)原型說明函數(shù)原型說明函數(shù)調用函數(shù)調用函數(shù)實現(xiàn)函數(shù)實現(xiàn)#include int max(int a, int b) if (a b) return(a); else return(b); main() int x, y; cin x y; cout x y; cout max(x, y);int p( int n ) int s =1, i; if (n 0) return
4、(0); for (i=1;in2? n1: n2); mainx(2)y(3)mainx(2)y(3)maxa(2)b(3)n1n2mainx(2)y(3)maxa(2)b(3)n1n2pn(2)simainx(2)y(3)maxa(2)b(3)n1(2)n2pn(3)simainx(2)y(3)maxa(2)b(3)n1(2)n2(6)mainx(2)y(3)注意:形式參數(shù)是注意:形式參數(shù)是數(shù)組,實際參數(shù)也數(shù)組,實際參數(shù)也是一個數(shù)組是一個數(shù)組Int main(void) int a = 2, b = 3; cout a b; int a = 4; cout a b; cout a b;in
5、t p = 1, q = 5, r=3;int f1() int p = 3, r = 2; q=p+q+r; cout “f1: p,q,r=“ p q r; int f2() p=p+q+r; cout “f2: p,q,r=“ p q r“ p q r; int f3() int q; r = 2*r; q=r+p; cout “f3: p,q,r=“ p q r; main()f3(); cout “after f3: p,q,r=” p q r” p q r; f1(); cout “after f1: p,q,r=“ p q r; f2(); cout “after f2: p,q
6、,r=” p q r” p q r; 結果:結果: f3: p,q,r=1 7 6 after f3: p,q,r=1 5 6 f1: p,q,r=3 10 2 after f1:p,q,r=1 10 6 f2: p,q,r=17 10 6 after f2: p,q,r=17 10 6 /file1.cpp#include using namespace std;void f();extern int x; /外部變量的聲明外部變量的聲明int main() f(); cout in main(): x= “ x endl; return 0;/file2.cpp#include using
7、 namespace std;int x; /全局變量的定義全局變量的定義void f() cout in f(): x= “ x endl; eg. int f(int a) int b=0; static int c=3; b=b+1; c=c+1; return(a+b+c); void main() int a=2,i; for (i=0; i3; +i) cout f(a); 運行結果為:運行結果為:7 8 9 void a();void b();void c();void d();main() int x=6; cout “x in main is “ x endl; a(); b
8、(); c(); d(x); x+; a(); b(); c(); d(x); cout “x in main is ” x endl; void a()int x=25; cout “x in a is ” x endl; void b()static int x=50; cout “x in b is ” x+ endl; void c() extern int x; x*=10; cout “x in c is ” x endl; int x=2; void d(int x) cout “x in d is ” +x endl; 結果:結果: x in main is 6 x in a
9、is 25 x in b is 50 x in c is 20 x in d is 7 x in a is 25 x in b is 51 x in c 200 x in d is 8 x in main is 7 基本情況基本情況n!=1*2*3*4*(n-1)*n(n-1)!遞歸形式:遞歸形式:其他)!1(*01!nnnn遞歸終止條件遞歸終止條件long p(int n) if (n = 0) return 1; else return n * p(n-1); 00112132435568其他)2() 1(1100)(nFnFnnnFint f(int n)if (n=0) return
10、0; elseif (n=1) return 1; else return (f(n-1)+f(n-2); ) 1()!1() 1 , 0(1!nnnnn求求p(4) 1)0(*1)1(*2)2(*3)3(*4)4(ppppp遞歸過程回溯消耗的時間:執(zhí)消耗的時間:執(zhí)行行n n次加法和次加法和3n3n次次賦值!賦值!120191817221323130322333629423343528524252627678910Void fill( int number, int begin, int size) int i, row = begin, col = begin; if (size = 0)
11、return; if (size = 1) pbeginbegin = number; return; prowcol = number; +number; for (i=0; isize-1; +i) +row; prowcol = number; +number; for (i=0; isize-1; +i) +col; prowcol = number; +number; for (i=0; isize-1; +i) -row; prowcol = number; +number; for (i=0; isize-2; +i) -col; prowcol = number; +numbe
12、r; fill( number, begin+1, size-2 );目標:將A上的盤子全部移到B上規(guī)則:每次只能移動一個盤子不允許大盤子放在小盤子上 A B CVoid PermuteWithFixedPrefix (char str , int k) int i; if ( k = strlen(str) ) cout str endl; else for (i=k; istrlen(str); +i) swap(str, k, i); PermuteWithFixedPrefix (str, k+1); swap(str, k, i); void queen_a11(int k) /在在
13、8x8棋盤的第棋盤的第k列上找合理的配置列上找合理的配置int i, j; char awn; for(i = 1; i 9; i+) / 依次在依次在l至至8行上配置行上配置k列的皇后列的皇后 if ( ai & bk+i-1 & c8+k-i) /可行位置可行位置 colk = i; ai = bk+i-1 = c8+k-i = false; /置對應位置有皇后置對應位置有皇后 if (k = 8) / 找到一個可行解找到一個可行解 for (j = 1; j = 8; j+) cout j “:”colj t ; cout awn; if (awn=Q | awn=q) exit(0);
14、 else queen_a11(k+1);/遞歸至第遞歸至第k十十1列列 ai = bk+i-1 = c8+k-i = true; /恢復對應位置無皇后恢復對應位置無皇后 NAAA,.,2112,.,NA AA12,.,NA AAjikkAInt maxSum(int a , int left, int right ) int maxLeft, maxRight, center; int leftSum = 0, rightSum = 0; int maxLeftTmp = NEGMAX, maxRightTmp = NEGMAX; center = ( left + right ) / 2;
15、 if ( left = right ) return aleft 0 ? aleft : 0; maxLeft = maxSum(a, left, center); maxRight = maxSum(a, center + 1, right); for (int i = center; i = left; -i) leftSum += ai; if (leftSum maxLeftTmp) maxLeftTmp = leftSum; for (int i = center + 1; i maxRightTmp) maxRightTmp = rightSum; return max3(max
16、Left, maxRight, maxLeftTmp + maxRightTmp)owhigh123045768901324576801234576890123456785730421968lowhighK=5730421968lowhighK=5730421968lowhigh173042968lowhigh130427968lowhigh123047968lowhighint coin(int k) int i, tmp, int coinNum = k; if (能用一個硬幣找零)(能用一個硬幣找零) return 1; for (i=1; ik; +i) if (tmp = coin(i) + coin(k-i) coinNum) coinNum = tmp; return coinNum;void makechange( int coins , int differentCoins, int maxChange, int coinUsed ) coinUsed0 = 0; for (int cents = 1; cents = m
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 三年級數(shù)學故事解讀
- 小王子書中純真之愛讀后感
- 自然資源開發(fā)與保護合作協(xié)議
- 智能家電銷售與保修協(xié)議
- 初中生歷史故事解讀
- 運輸合同運輸補充協(xié)議
- 辦公區(qū)域布局調研報告
- 環(huán)保咨詢服務協(xié)議
- 電子設備銷售及安裝維護合同
- 物流行業(yè)運輸損壞物品賠償協(xié)議
- 北京電子科技職業(yè)學院招聘考試題庫2024
- 貸款的培訓課件
- 無人系統(tǒng)自主控制
- 化工原理陳敏恒課件
- 景區(qū)保安投標方案(技術方案)
- 中建辦公、生活區(qū)臨時設施施工方案
- 中國金融書法家協(xié)會入會申請表
- 地下室頂板支撐回頂方案
- 痛經教學講解課件
- 基于康耐視相機的視覺識別實驗指導書
- 水務集團有限公司人事管理制度
評論
0/150
提交評論