Download presentation
Presentation is loading. Please wait.
Published byKaylee Fogg Modified over 9 years ago
1
第三次小考
2
#include using namespace std; int aaa(int *ib,int a1,int a2) { int u,v; int m=(a1+a2)/2; if(a1==a2)return ib[a1]; u=aaa(ib,a1,m); cout<<"u="<<u<<"\n"; v=aaa(ib,m+1,a2); cout<<"v="<<v<<"\n"; if(u>v)return u;else return v; } int main() { int ia[10]={0,2,3,6,8,6,12,28,16,11}; cout<<aaa(ia,0,9)<<"\n"; return 0; }
3
aaa(ib,0,9)
4
int aaa(int *ib,int a1,int a2) { int u,v; int m=(a1+a2)/2; if(a1==a2)return ib[a1]; u=aaa(ib,a1,m); cout<<"u="<<u<<"\n"; v=aaa(ib,m+1,a2); cout<<"v="<<v<<"\n"; if(u>v)return u;else return v; } 傳入參數 a1=0 , a2=9 m=(0+9)/2 …… 因為 m 屬於 int ,所以 m 等於整數部份 m = 4 a1=0 , a2=9 沒有 return 傳入 aaa(ib,0,4)
5
aaa(ib,0,9) aaa(ib,0,4)
6
int aaa(int *ib,int a1,int a2) { int u,v; int m=(a1+a2)/2; if(a1==a2)return ib[a1]; u=aaa(ib,a1,m); cout<<"u="<<u<<"\n"; v=aaa(ib,m+1,a2); cout<<"v="<<v<<"\n"; if(u>v)return u;else return v; } 傳入參數 a1=0 , a2=4 m=(0+4)/2 …… 因為 m 屬於 int ,所以 m 等於整數部份 m = 2 a1=0 , a2=4 沒有 return 傳入 aaa(ib,0,2)
7
aaa(ib,0, 9) aaa(ib,0, 4) aaa(ib,0, 2)
8
int aaa(int *ib,int a1,int a2) { int u,v; int m=(a1+a2)/2; if(a1==a2)return ib[a1]; u=aaa(ib,a1,m); cout<<"u="<<u<<"\n"; v=aaa(ib,m+1,a2); cout<<"v="<<v<<"\n"; if(u>v)return u;else return v; } 傳入參數 a1=0 , a2=2 m=(0+2)/2 …… 因為 m 屬於 int ,所以 m 等於整數部份 m = 1 a1=0 , a2=2 沒有 return 傳入 aaa(ib,0,1)
9
aaa(ib,0,9) aaa(ib,0,4) aaa(ib,0,2) aaa(ib,0,1)
10
int aaa(int *ib,int a1,int a2) { int u,v; int m=(a1+a2)/2; if(a1==a2)return ib[a1]; u=aaa(ib,a1,m); cout<<"u="<<u<<"\n"; v=aaa(ib,m+1,a2); cout<<"v="<<v<<"\n"; if(u>v)return u;else return v; } 傳入參數 a1=0 , a2=1 m=(0+1)/2 …… 因為 m 屬於 int ,所以 m 等於整數部份 m = 0 a1=0 , a2=1 沒有 return 傳入 aaa(ib,0,0)
11
aaa(ib,0,9) aaa(ib,0,4) aaa(ib,0,2) aaa(ib,0,1) aaa(ib,0,0)
12
int aaa(int *ib,int a1,int a2) { int u,v; int m=(a1+a2)/2; if(a1==a2)return ib[a1]; u=aaa(ib,a1,m); cout<<"u="<<u<<"\n"; v=aaa(ib,m+1,a2); cout<<"v="<<v<<"\n"; if(u>v)return u;else return v; } 傳入參數 a1=0 , a2=0 m=(0+2)/2 …… 因為 m 屬於 int ,所以 m 等於整數部份 m = 1 a1=0 , a2=0 return ib[0]
13
aaa(ib,0,9) aaa(ib,0,4) aaa(ib,0,2) aaa(ib,0,1) aaa(ib,0,0) return ib[0] (ib[0]=0) u=aaa(ib,0,0)=0
14
int aaa(int *ib,int a1,int a2) { int u,v; int m=(a1+a2)/2; if(a1==a2)return ib[a1]; u=aaa(ib,a1,m); cout<<"u="<<u<<"\n"; v=aaa(ib,m+1,a2); cout<<"v="<<v<<"\n"; if(u>v)return u;else return v; } 傳入參數 a1=0 , a2=1 m=(0+1)/2 …… 因為 m 屬於 int ,所以 m 等於整數部份 m = 0 a1=0 , a2=1 沒有 return aaa(ib,0,0) return ib[0] 所以 u=ib[0] (u=0) u=0 傳入 aaa(ib,1,1)
15
aaa(ib,0,9) aaa(ib,0,4) aaa(ib,0,2) aaa(ib,0,1) aaa(ib,0,0) u=0 return ib[0] (ib[0]=0) aaa(ib,1,1)
16
int aaa(int *ib,int a1,int a2) { int u,v; int m=(a1+a2)/2; if(a1==a2)return ib[a1]; u=aaa(ib,a1,m); cout<<"u="<<u<<"\n"; v=aaa(ib,m+1,a2); cout<<"v="<<v<<"\n"; if(u>v)return u;else return v; } 傳入參數 a1=1 , a2=1 m=(1+1)/2 …… 因為 m 屬於 int ,所以 m 等於整數部份 m = 1 a1=a2=1 return ib[1]
17
aaa(ib,0,9) aaa(ib,0,4) aaa(ib,0,2) aaa(ib,0,1) aaa(ib,0,0) u=0 return ib[0] (ib[0]=0) aaa(ib,1,1) v=2 return ib[1] (ib[1]=2) v=aaa(ib,1,1)=2
18
int aaa(int *ib,int a1,int a2) { int u,v; int m=(a1+a2)/2; if(a1==a2)return ib[a1]; u=aaa(ib,a1,m); cout<<"u="<<u<<"\n"; v=aaa(ib,m+1,a2); cout<<"v="<<v<<"\n"; if(u>v)return u;else return v; } 傳入參數 a1=0 , a2=1 m=(0+1)/2 …… 因為 m 屬於 int ,所以 m 等於整數部份 m = 0 a1=0 , a2=1 沒有 return 傳入 aaa(ib,0,0) u = 0 v = aaa(ib,1,1) v = 2 u=0,v = 2 v>u return v (v=2)
19
v>u return v (v=2) aaa(ib,0,9) aaa(ib,0,4) aaa(ib,0,2) aaa(ib,0,1) u=2 aaa(ib,0,0) u=0 return ib[0] (ib[0]=0) aaa(ib,1,1) v=2 return ib[1] (ib[1]=2)
20
int aaa(int *ib,int a1,int a2) { int u,v; int m=(a1+a2)/2; if(a1==a2)return ib[a1]; u=aaa(ib,a1,m); cout<<"u="<<u<<"\n"; v=aaa(ib,m+1,a2); cout<<"v="<<v<<"\n"; if(u>v)return u;else return v; } 傳入參數 a1=0 , a2=2 m=(0+2)/2 …… 因為 m 屬於 int ,所以 m 等於整數部份 m = 1 a1=0 , a2=2 沒有 return u=aaa(ib,0,1)=2 u=2 傳入 aaa(ib,2,2)
21
aaa(ib,0,9) aaa(ib,0,4) aaa(ib,0,2) aaa(ib,0,1) u=2 aaa(ib,0,0) u=0 return ib[0] (ib[0]=0) aaa(ib,1,1) v=2 return ib[1] (ib[1]=2) aaa(ib,2,2)
22
int aaa(int *ib,int a1,int a2) { int u,v; int m=(a1+a2)/2; if(a1==a2)return ib[a1]; u=aaa(ib,a1,m); cout<<"u="<<u<<"\n"; v=aaa(ib,m+1,a2); cout<<"v="<<v<<"\n"; if(u>v)return u;else return v; } 傳入參數 a1=2 , a2=2 m=(2+2)/2 …… 因為 m 屬於 int ,所以 m 等於整數部份 m = 2 a1=a2=2 return ib[2] (ib[2]=3)
23
aaa(ib,0,9) aaa(ib,0,4) aaa(ib,0,2) aaa(ib,0,1) u=2 aaa(ib,0,0) u=0 return ib[0] (ib[0]=0) aaa(ib,1,1) v=2 return ib[1] (ib[1]=2) aaa(ib,2,2) v=3 return ib[2] (ib[2]=3) v=aaa(ib,2,2)=3
24
int aaa(int *ib,int a1,int a2) { int u,v; int m=(a1+a2)/2; if(a1==a2)return ib[a1]; u=aaa(ib,a1,m); cout<<"u="<<u<<"\n"; v=aaa(ib,m+1,a2); cout<<"v="<<v<<"\n"; if(u>v)return u;else return v; } 傳入參數 a1=0 , a2=2 m=(0+2)/2 …… 因為 m 屬於 int ,所以 m 等於整數部份 m = 1 a1=0 , a2=2 沒有 return u=aaa(ib,0,1)=2 u=2 傳入 aaa(ib,2,2) v=3 u=2,v = 3 v>u return v (v=3)
25
v>u return v (v=3)
26
int aaa(int *ib,int a1,int a2) { int u,v; int m=(a1+a2)/2; if(a1==a2)return ib[a1]; u=aaa(ib,a1,m); cout<<"u="<<u<<"\n"; v=aaa(ib,m+1,a2); cout<<"v="<<v<<"\n"; if(u>v)return u;else return v; } 傳入參數 a1=0 , a2=4 m=(0+4)/2 …… 因為 m 屬於 int ,所以 m 等於整數部份 m = 2 a1=0 , a2=4 沒有 return 傳入 aaa(ib,0,2) aaa(ib,0,2) = 3 u = 3 傳入 aaa(ib,3,4)
27
v>u return v (v=3)
28
加油 ~~!! 繼續做下去一定會是 以下結果
29
aaa(ib,0,9) 28 aaa(ib,0,4) u=8 aaa(ib,0,2) u=3 aaa(ib,0,1) u=2 aaa(ib,0,0) u=0 return ib[0] aaa(ib,1,1) v=2 return ib[1] aaa(ib,2,2) v=3 return ib[2] aaa(ib,3,4) v=8 aaa(ib,3,3) u=6 return ib[3] aaa(ib,4,4) v=8 return ib[4] aaa(ib.5,9) v=28
30
aaa(ib,0,9) 28 aaa(ib,0,4) u=8 aaa(ib.5,9) v=28 aaa(ib,5,7) u=28 aaa(ib,5,6) u=12 aaa(ib,5,5) u=6 return ib[5] aaa(ib,6,6) v=12 return ib[6] aaa(ib,6,7) v=28 aaa(ib,6,6) u=12 return ib[6] aaa(ib,7,7) v=28 return ib[7] aaa{ib,8,9) v=16 aaa(ib,8,8) u=16 return ib[8] Aaa(ib,9,9) v=11 return ib[9]
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.