[Governance] Introduction to computing (a)/Recursive Function exercises (1) polygon games

Source: Internet
Author: User
1 # include <cstdio> 2 # include <algorithm> 3 using namespace STD; 4 int N, ANS, V [41], C [41], s [2]; 5 Int work (int l, int R) // grouping 6 {7 if (L = r) return V [l]; 8 int res = 0; 9 For (INT I = L; I <r; I ++) 10 if (C [I] = '+') RES = max (Res, work (L, i) + work (I + 1, R); 11 else if (C [I] = '*') RES = max (Res, work (L, I) * Work (I + 1, R); 12 Return res; 13} 14 int main () 15 {16 scanf ("% d", & N ); 17 For (INT I = 1; I <= N; I ++) // expand the loop into N chains 18 {19 scanf ("% d % s ", & V [I], S); 20 V [n + I] = V [I]; 21 C [I] = C [n + I] = s [0]; 22} 23 for (INT I = 1; I <= N; I ++) 24 ans = max (ANS, work (I, I + N-1 )); 25 printf ("% d \ n", ANS); 26 return 0; 27}

 

Total time limit:
1000 ms

Memory limit: 65536kb

Description
A polygon starts with n vertices. Each vertex is assigned a positive integer value, and each edge is assigned an operator "+" or "*". All edges are numbered from 1 to n in sequence. Now I want to play a game. There are n steps in this game: Step 2. Select an edge and delete it from step n-1. Each step is operated in the following way: (1) select one edge e and two vertices V1 and V2 connected by E. (2) replace edge e with a new vertex and the two vertices V1 and V2 connected by E, assign the result value obtained from the operation on edge e to the new vertex through the integer of vertex V1 and V2.
Finally, all edges are deleted, with only one vertex left, and the game ends. The game score is the integer on the remaining vertices. So what is the maximum integer value?
Input
The number of vertices of the first polygon is n (n ≤ 20), followed by N rows. Each behavior contains an integer and a character. The integer is the positive integer value on the vertex, the character is the "+" or "*" Operator between the vertex and the next vertex (the last character is the operator between the last vertex and the first vertex ).
Output
Only one integer is output, that is, the maximum value calculated by the game.
Sample Input
44 *5 +5 +3 +
Sample output
70
Prompt
Small-scale problems can be solved only by recursion without the need to use the dynamic programming method. The calculation result does not need to be considered to be out of the integer expression range. The data given can ensure the validity of the calculation result. In the given example, the calculation process is (3 + 4) * (5 + 5) = 70.

Orz szy

Divide the idea of governance, first turn the cycle into N chains, and Recursive Divide processing during processing.

 

[Governance] Introduction to computing (a)/Recursive Function exercises (1) polygon games

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.