Today, in the Software engineering class, the teacher gave an example, the biggest continuous sub-sequence of the problem, himself in the brain, think of the ACM did in the topic,
briefly talk about the idea : Dynamic programming, to find the state transfer equation is the key. Define two arrays a[],b[], one to save your own input array, one to store continuous and the value. State transfer equation: B[i]=max{b[i-1]+a[i],a[i]}, find the largest b[i].
Pseudo code:
for (Loop)
if (a[i]>0),
B[i]=b[i-1]+a[i],
Else
B[i]=a[i]
I choose to use the choice of sorting, compare the round to know which is the largest,
MAX=B[0]
if (Max<b[i])
Max=b[i]
Enter the number of test data groups: Test, type: The size of the array, and then enter: the individual elements of the array.
Output: maximum continuous sequence and
Source:
1#include <iostream>2#include <cstdio>3#include <cstdlib>4 using namespacestd;5 6 #defineMax 10057 intA[max],b[max];8 9 intMain ()Ten { One inti,n,test,max=0; Ascanf"%d",&test); - while(test--) - { thescanf"%d",&n); - for(i=0; i<n;i++) -scanf"%d",&a[i]); -b[1]=a[1]; + for(i=2; i<=n;i++) - { + if(a[i]>0) Ab[i]=b[i-1]+A[i]; at Else -b[i]=A[i]; - } -max=b[1]; - for(i=2; i<=n;i++) - { in if(max<B[i]) -max=B[i]; to + } -printf"%d", max); the } * return 0; $}
Then the maximum sequential sequence is found, and the output from the number of digits to the number of feet is also required.
Topics in Hdu oj:http://acm.hdu.edu.cn/game/entry/problem/show.php?chapterid=3§ionid=2&problemid=1
is the output of the abnormal point, in fact, the idea of mastering, on the line.
My Code:
1#include <iostream>2 #defineN 1000103 using namespacestd;4 intA[n],d[n];5 intMain ()6 {7 inttest,n,i,max,k,f,e;8Cin>>test;9k=1;Ten while(test--) One { ACin>>N; - for(i=1; i<=n;i++) -Cin>>A[i]; thed[1]=a[1]; - for(i=2; i<=n;i++) - { - if(d[i-1]<0) d[i]=A[i]; + Elsed[i]=d[i-1]+A[i]; - } +max=d[1];e=1; A for(i=2; i<=n;i++) at { - if(max<D[i]) - { -Max=d[i];e=i; - } - } in intt=0; -f=e; to for(i=e;i>0; i--) + { -t=t+A[i]; the if(T==max) f=i; * } $cout<<" Case"<<k++<<":"<<endl<<max<<" "<<f<<" "<<e<<Endl;Panax Notoginseng if(test) cout<<Endl; - } the return 0; +}
Software engineering maximum continuous sequence and