go語言實現尋找最大子數組

來源:互聯網
上載者:User
這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。

題目:給定一個數字序列,尋找其中各元素相加和最大的子數組

  1 /*  2 尋找最大子數組go語言實現  3 */  4   5 package main  6   7 import fmt "fmt"  8   9 func main() { 10     /* 11     需要尋找的數組 12     */ 13     A:=[]int{-1,5,-3,-1,3,1,3} 14     low:=0 15     high:=len(A)-1 16     sum:=0 17     low,high,sum=find_max_array(A,0,len(A)-1) 18     /* 19     返回數組邊界 20     */ 21     fmt.Println(low," ",high," ",sum) 22 } 23 /* 24 返回越過中間點的最大數組 25 */ 26 func Find_max_acrossing(Array []int,low int,mid int,high int)(int,int,int) { 27     /* 28     現設定左最小值,尋找左邊最小數組邊界 29     */ 30     left_sum:=-1000 31     sum:=0 32     max_left:=mid 33     for i := mid; i>=low; i-- { 34         sum+=Array[i] 35         if sum>left_sum { 36             left_sum=sum 37             max_left=i 38         } 39     } 40  41     /* 42     現設定右最小值,尋找右邊最小數組邊界 43     */ 44     right_sum:=-1000 45     sum=0 46     max_right:=mid+1 47     for i := mid+1; i <=high; i++ { 48         sum+=Array[i] 49         if sum>right_sum { 50             right_sum=sum 51             max_right=i 52         } 53     } 54     return max_left,max_right,left_sum+right_sum 55 } 56  57 func find_max_array(Array []int,low int,high int)(int,int,int){ 58     max_left:=low 59     max_right:=high 60     sum:=0 61     if low==high { 62         max_left=low 63         max_right=high 64         sum=Array[high] 65     }else{ 66         mid:=0 67         if (high-low+1)%2==0 { 68             mid=(high+low-1)/2 69         }else{ 70             mid=(high+low)/2 71         } 72  73         max_left_low:=low 74         max_left_high:=mid 75         left_sum:=0 76         max_left_low,max_left_high,left_sum=find_max_array(Array,low,mid) 77  78         max_right_low:=mid+1 79         max_right_high:=high 80         right_sum:=0 81         max_right_low,max_right_high,right_sum=find_max_array(Array,mid+1,high) 82  83         max_across_low:=low 84         max_across_high:=high 85         across_sum:=0 86         max_across_low,max_across_high,across_sum=Find_max_acrossing(Array,low,mid,high) 87  88         if left_sum>right_sum && left_sum>across_sum{ 89             max_left=max_left_low 90             max_right=max_left_high 91             sum=left_sum 92         }else if right_sum>left_sum && right_sum>across_sum { 93             max_left=max_right_low 94             max_right=max_right_high 95             sum=right_sum 96         }else if across_sum>left_sum && across_sum>right_sum { 97             max_left=max_across_low 98             max_right=max_across_high 99             sum=across_sum100         }101     }102     return max_left,max_right,sum103 }

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.