A recursive implementation

xiaoxiao2021-03-05  27

// has not been optimized, welcome to propose optimization opinions ^. ^ Title: People in the Program country, like to play such a game, write a line on a plate, a total of n. Two players, take turns from the right or longer left. Just start, each player's score is zero. If a player removes a number, the value of the number is added to the player's score, and the last person's score is the highest to win the game. Give this N number (from left to right), assume that the player is very smart, ask the last two people's score (assuming the first person first taking). Input format: The first line is n (2 <= n <= 100), the second behavior n number, and each number is spaced apart from the space. The output is the score of two players. The first number represents the first player's score, the second number is the second player's score, and the two numbers are separated from spaces. Enter 64 7 2 9 5 2 output 18 11

============================================= / * CopyRight (c) Aprin at xiamen university * 2005-04 * / #include #define MaxN 100int Sum (int Data [], int head, int tail; int {int Temp1, temp2; if (head == Tail) Return Head; Temp1 = * (Data Head) SUM (Data, Head 1, Tail); Temp2 = * (Data Tail) SUM (Data, Head, Tail-1); Return (Temp1> Temp2 )? Head: tail; / * Return pointer * /} int sum (int data [], int head, int tail) {int Temp; if (head == tail) return 0; / * has 0 data * / if (Tail == HEAD 1) RETURN (* (Data Tail))? Head: tail; / * Take a small * / if (Best (Data, Head, Tail) == HEAD / * Change the work pointer, let the other party choose one of the best value * / head ; else tail--; Temp = BEST (Data, Head, Tail); if (Temp == HEAD) Return * (Data Temp) SUM (Data, Head 1, Tail); Else Return * (Data Temp) SUM (Data, Head, Tail-1);} int main (void) {Int Data [MAXN], Head, Tail, N, I, Total1, Total2, Temp; Scanf ("% D", & n); while ((n <2) || (N> 100)) {Printf ("N is Wrong! please input n (2 <= n < = 100) Again! / N "); scanf ("% d ", & n);} for (i = 0; I

转载请注明原文地址:https://www.9cbs.com/read-33541.html

New Post(0)