300 likes | 418 Views
第三次小考. #include<iostream> 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<<"<br>"; v=aaa(ib,m+1,a2); cout<<"v="<<v<<"<br>"; if(u>v)return u;else return v; } int main() {
E N D
#include<iostream> • 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; • }
傳入參數 a1=0,a2=9 m=(0+9)/2 …… 因為m屬於int,所以m等於整數部份 m = 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 沒有return 傳入aaa(ib,0,4)
傳入參數 a1=0,a2=4 m=(0+4)/2 …… 因為m屬於int,所以m等於整數部份 m = 2 • 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沒有return 傳入aaa(ib,0,2)
傳入參數 a1=0,a2=2 m=(0+2)/2 …… 因為m屬於int,所以m等於整數部份 m = 1 • 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沒有return 傳入aaa(ib,0,1)
傳入參數 a1=0,a2=1 m=(0+1)/2 …… 因為m屬於int,所以m等於整數部份 m = 0 • 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沒有return 傳入aaa(ib,0,0)
傳入參數 a1=0,a2=0 m=(0+2)/2 …… 因為m屬於int,所以m等於整數部份 m = 1 • 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=0return ib[0]
傳入參數 a1=0,a2=1 m=(0+1)/2 …… 因為m屬於int,所以m等於整數部份 m = 0 • 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沒有return aaa(ib,0,0) return ib[0] 所以 u=ib[0] (u=0) u=0 傳入aaa(ib,1,1)
傳入參數 a1=1,a2=1 m=(1+1)/2 …… 因為m屬於int,所以m等於整數部份 m = 1 • 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=a2=1return ib[1]
傳入參數 a1=0,a2=1 m=(0+1)/2 …… 因為m屬於int,所以m等於整數部份 m = 0 • 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沒有return 傳入aaa(ib,0,0) u = 0 v = aaa(ib,1,1) v = 2 u=0 ,v = 2 v>u return v (v=2)
v>u return v (v=2)
傳入參數 a1=0,a2=2 m=(0+2)/2 …… 因為m屬於int,所以m等於整數部份 m = 1 • 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沒有return u=aaa(ib,0,1)=2 u=2 傳入aaa(ib,2,2)
傳入參數 a1=2,a2=2 m=(2+2)/2 …… 因為m屬於int,所以m等於整數部份 m = 2 • 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=a2=2return ib[2] (ib[2]=3)
傳入參數 a1=0,a2=2 m=(0+2)/2 …… 因為m屬於int,所以m等於整數部份 m = 1 • 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沒有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)
傳入參數 a1=0,a2=4 m=(0+4)/2 …… 因為m屬於int,所以m等於整數部份 m = 2 • 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沒有return 傳入aaa(ib,0,2) aaa(ib,0,2) = 3 u = 3 傳入 aaa(ib,3,4)
加油~~!! 繼續做下去一定會是 以下結果