[Exercise 09] Simple and dynamic planning of 1002 Max sum

Source: Internet
Author: User

The Code submitted for the first time uses recursion. Due to the large number of recursive layers of data, stack overflow occurs ".

Code submitted for the first time:

// Template start # include <string> # include <vector> # include <algorithm> # include <iostream> # include <sstream> # include <fstream> # include <map> # include <set> # include <cstdio> # include <cmath> # include <cstdlib> # include <ctime> # include <iomanip> # define SZ (X) (INT (X. size () using namespace STD; int toint (string s) {istringstream sin (s); int t; sin> T; return t ;} template <class T> string tostring (t x) {ostrings Tream sout; sout <X; return sout. STR ();} typedef long int64; int64 toint64 (string s) {istringstream sin (s); int64 t; sin> T; return t ;} template <class T> T gcd (t a, t B) {if (a <0) return gcd (-a, B); If (B <0) return gcd (A,-B); Return (B = 0 )? A: gcd (B, A % B);} // template end (General Part) // [exercise 09] Simple Dynamic Planning 1002 Max sum int calculate_add_max (int * datas, int * add_max, Int J, int N) {If (j = n-1) {return datas [n-1];} else {add_max [J + 1] = calculate_add_max (datas, add_max, J + 1, n); If (add_max [J + 1]> 0) {return datas [J] + add_max [J + 1];} else {return datas [J] ;}} int main () {ifstream ifs ("shuju.txt", IOS:: In); INT cases; int N; int datas [100005]; int add_max [1000 05]; // ifs> cases; CIN> cases; for (INT I = 0; I <cases; I ++) {// ifs> N; cin> N; For (Int J = 0; j <n; j ++) {// ifs> datas [J]; CIN> datas [J];} add_max [0] = calculate_add_max (datas, add_max, 0, n); int max = add_max [0]; int max_index = 0; For (Int J = 0; j <N; j ++) {If (add_max [J]> MAX) {max = add_max [J]; max_index = J ;}} Int J; For (j = max_index + 1; j <n; j ++) {If (add_max [J] <= 0) {cout <"case" <I + 1 <":" <Endl; c Out <add_max [max_index] <"" <max_index + 1 <"" <j <Endl; if (I! = Cases-1) {cout <Endl;} break;} else {continue;} If (j = N) {cout <"case" <I + 1 <":" <Endl; cout <add_max [max_index] <"" <max_index + 1 <"" <n <Endl; if (I! = Cases-1) {cout <Endl ;}} return 0 ;}

 

Instead of recursion, the AC is successfully switched to recursion.

The second code submission:

// Template start # include <string> # include <vector> # include <algorithm> # include <iostream> # include <sstream> # include <fstream> # include <map> # include <set> # include <cstdio> # include <cmath> # include <cstdlib> # include <ctime> # include <iomanip> # define SZ (X) (INT (X. size () using namespace STD; int toint (string s) {istringstream sin (s); int t; sin> T; return t ;} template <class T> string tostring (t x) {ostrings Tream sout; sout <X; return sout. STR ();} typedef long int64; int64 toint64 (string s) {istringstream sin (s); int64 t; sin> T; return t ;} template <class T> T gcd (t a, t B) {if (a <0) return gcd (-a, B); If (B <0) return gcd (A,-B); Return (B = 0 )? A: gcd (B, A % B);} // template end (General Part) // [exercise 09] Simple Dynamic Planning 1002 Max sum int calculate_add_max (int * datas, int * add_max, Int J) {return add_max [J + 1]> 0? Datas [J] + add_max [J + 1]: datas [J];} int main () {ifstream ifs ("shuju.txt", IOS: In); INT cases; int N; int datas [100005]; int add_max [100005]; // ifs> cases; CIN> cases; for (INT I = 0; I <cases; I ++) {// ifs> N; CIN> N; For (Int J = 0; j <n; j ++) {// ifs> datas [J]; CIN> datas [J];} add_max [n-1] = datas [n-1]; for (Int J = N-2; j> = 0; j --) {add_max [J] = calculate_add_max (datas, add_max, J );} int max = add_max [0]; int Max_index = 0; For (Int J = 0; j <n; j ++) {If (add_max [J]> MAX) {max = add_max [J]; max_index = J ;}} Int J; For (j = max_index + 1; j <n; j ++) {If (add_max [J] <= 0) {cout <"case" <I + 1 <":" <Endl; cout <add_max [max_index] <"" <max_index + 1 <"" <j <Endl; if (I! = Cases-1) {cout <Endl;} break;} else {continue;} If (j = N) {cout <"case" <I + 1 <":" <Endl; cout <add_max [max_index] <"" <max_index + 1 <"" <n <Endl; if (I! = Cases-1) {cout <Endl ;}} return 0 ;}

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.