1 / 30

第三次小考

第三次小考. #include&lt;iostream&gt; 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&lt;&lt;&quot;u=&quot;&lt;&lt;u&lt;&lt;&quot;<br>&quot;; v=aaa(ib,m+1,a2); cout&lt;&lt;&quot;v=&quot;&lt;&lt;v&lt;&lt;&quot;<br>&quot;; if(u&gt;v)return u;else return v; } int main() {

cyrus-ray
Download Presentation

第三次小考

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 第三次小考

  2. #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; • }

  3. 傳入參數 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)

  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)

  5. 傳入參數 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)

  6. 傳入參數 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)

  7. 傳入參數 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=0return ib[0]

  8. u=aaa(ib,0,0)=0

  9. 傳入參數 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)

  10. 傳入參數 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=1return ib[1]

  11. v=aaa(ib,1,1)=2

  12. 傳入參數 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)

  13. v>u return v (v=2)

  14. 傳入參數 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)

  15. 傳入參數 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=2return ib[2] (ib[2]=3)

  16. v=aaa(ib,2,2)=3

  17. 傳入參數 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)

  18. 傳入參數 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)

  19. 加油~~!! 繼續做下去一定會是 以下結果

More Related