结对开发Ⅴ——循环一维数组求和最大的子数组
生活随笔
收集整理的這篇文章主要介紹了
结对开发Ⅴ——循环一维数组求和最大的子数组
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一、設計思路
(1)數據的存儲結構是鏈表,最后一個結點的next指向第一個元素的結點;
(2)數據個數為n,則最多有n*(n+(n-1)+...+1)種情況(包括重復);
(3)剩下的部分與二維數組的差不多。
二、源代碼
1 // 一維數組.cpp : Defines the entry point for the console application. 2 // 袁佩佩 于海洋 3 4 #include "stdafx.h" 5 #include<iostream.h> 6 #define num 5 7 /*鏈表數據結構*/ 8 typedef struct LNode 9 { 10 int data; 11 struct LNode *next; 12 }LNode,*LinkList; 13 /*鏈表的初始化*/ 14 void InitList(LinkList &L) 15 { 16 L=new LNode; 17 L->next=NULL; 18 } 19 /*鏈表數據的插入*/ 20 void InsertList(LinkList &L)//建立循環鏈表 21 { 22 LNode *head,*temp; 23 head=L; 24 cout<<"請輸入"<<num<<"個數字:"; 25 for(int i=0;i<num;i++) 26 { 27 temp=new LNode; 28 cin>>temp->data; 29 temp->next=NULL; 30 head->next=temp; 31 head=head->next; 32 } 33 head->next=L->next; //首尾相連,建立循環鏈表 34 } 35 void output(LinkList L) 36 { 37 for(int i=0;i<num;i++) 38 { 39 cout<<L->next->data<<" "; 40 L=L->next; 41 } 42 } 43 int main( ) 44 { 45 int max,sum,flag=0; //sum是字數組的和,max是最大的子數組的和 46 int ordern=0,orderx=0; 47 LinkList L; 48 LNode *temp,*temp1,*temp2,*head; 49 InitList(L); 50 InsertList(L); //由用戶往鏈表中插入數據 51 temp=L->next; 52 max=L->next->data; //max初值是鏈表中第一個數 53 for(int i=0;i<num;i++,temp=temp->next) 54 { 55 temp2=temp; 56 for(int j=0;j<num;j++,temp2=temp2->next) 57 { 58 for(int k=j;k<num;k++) 59 { 60 sum=0; 61 temp1=temp2; 62 for(int h=j;h<=k;h++,temp1=temp1->next) 63 { 64 sum=sum+temp1->data; 65 } 66 if(max<sum) //將最大值賦給max,并且保存當時的序號 67 { 68 max=sum; 69 ordern=j; 70 orderx=k; 71 head=temp; 72 flag=i; //用來求取最大值的時候的鏈表的情況 73 } 74 } 75 } 76 } 77 temp=L->next; 78 cout<<"最大字數組是:"; 79 for(i=0;i<(flag+ordern);i++) //找出取得最大值的時候的子數組的第一個數 80 { 81 temp=temp->next; 82 } 83 for(int j=0;j<(orderx-ordern+1);j++,temp=temp->next)//將取得最大和的子數組元素輸出 84 { 85 cout<<temp->data<<" "; 86 } 87 cout<<endl<<"最大子數組的和是:"<<max<<endl;; 88 89 return 0; 90 }?
三、運行截圖
四、心得體會
? 這次最大的感受就是,我們兩個都有各自的思路想法,都想向對方闡述自己的想法,導致無法很好地接受傾聽對方的聲音。我覺得他的方法浪費內存空間,他認為他的查找過程清晰明了。最終還是于海洋同學妥協了,采用的是我的思路,用循環鏈表。開發過程還是像上兩次差不多,個人有個人的優缺點,可以相互彌補。調試程序時,也是我倆共同協作排查出了錯誤。
五、無圖無真相
?
轉載于:https://www.cnblogs.com/JJJanepp/p/4373412.html
總結
以上是生活随笔為你收集整理的结对开发Ⅴ——循环一维数组求和最大的子数组的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Snap7 西门子S7系列PLC的通信库
- 下一篇: ab st语言编程手册_罗克韦尔ab p