结对开发Ⅴ——循环一维数组求和最大的子数组
生活随笔
收集整理的這篇文章主要介紹了
结对开发Ⅴ——循环一维数组求和最大的子数组
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
一、設(shè)計(jì)思路
(1)數(shù)據(jù)的存儲結(jié)構(gòu)是鏈表,最后一個結(jié)點(diǎn)的next指向第一個元素的結(jié)點(diǎn);
(2)數(shù)據(jù)個數(shù)為n,則最多有n*(n+(n-1)+...+1)種情況(包括重復(fù));
(3)剩下的部分與二維數(shù)組的差不多。
二、源代碼
1 // 一維數(shù)組.cpp : Defines the entry point for the console application. 2 // 袁佩佩 于海洋 3 4 #include "stdafx.h" 5 #include<iostream.h> 6 #define num 5 7 /*鏈表數(shù)據(jù)結(jié)構(gòu)*/ 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 /*鏈表數(shù)據(jù)的插入*/ 20 void InsertList(LinkList &L)//建立循環(huán)鏈表 21 { 22 LNode *head,*temp; 23 head=L; 24 cout<<"請輸入"<<num<<"個數(shù)字:"; 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; //首尾相連,建立循環(huán)鏈表 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是字?jǐn)?shù)組的和,max是最大的子數(shù)組的和 46 int ordern=0,orderx=0; 47 LinkList L; 48 LNode *temp,*temp1,*temp2,*head; 49 InitList(L); 50 InsertList(L); //由用戶往鏈表中插入數(shù)據(jù) 51 temp=L->next; 52 max=L->next->data; //max初值是鏈表中第一個數(shù) 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,并且保存當(dāng)時(shí)的序號 67 { 68 max=sum; 69 ordern=j; 70 orderx=k; 71 head=temp; 72 flag=i; //用來求取最大值的時(shí)候的鏈表的情況 73 } 74 } 75 } 76 } 77 temp=L->next; 78 cout<<"最大字?jǐn)?shù)組是:"; 79 for(i=0;i<(flag+ordern);i++) //找出取得最大值的時(shí)候的子數(shù)組的第一個數(shù) 80 { 81 temp=temp->next; 82 } 83 for(int j=0;j<(orderx-ordern+1);j++,temp=temp->next)//將取得最大和的子數(shù)組元素輸出 84 { 85 cout<<temp->data<<" "; 86 } 87 cout<<endl<<"最大子數(shù)組的和是:"<<max<<endl;; 88 89 return 0; 90 }?
三、運(yùn)行截圖
四、心得體會
? 這次最大的感受就是,我們兩個都有各自的思路想法,都想向?qū)Ψ疥U述自己的想法,導(dǎo)致無法很好地接受傾聽對方的聲音。我覺得他的方法浪費(fèi)內(nèi)存空間,他認(rèn)為他的查找過程清晰明了。最終還是于海洋同學(xué)妥協(xié)了,采用的是我的思路,用循環(huán)鏈表。開發(fā)過程還是像上兩次差不多,個人有個人的優(yōu)缺點(diǎn),可以相互彌補(bǔ)。調(diào)試程序時(shí),也是我倆共同協(xié)作排查出了錯誤。
五、無圖無真相
?
轉(zhuǎn)載于:https://www.cnblogs.com/JJJanepp/p/4373412.html
總結(jié)
以上是生活随笔為你收集整理的结对开发Ⅴ——循环一维数组求和最大的子数组的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Snap7 西门子S7系列PLC的通信库
- 下一篇: ab st语言编程手册_罗克韦尔ab p