// No optimization has been made. You are welcome to provide optimization comments ^. ^
Question:
People in the program country like to play such a game, and write a row of numbers on a board, N in total. Two players take one number from the rightmost or leftmost. At the beginning, each player scored zero. If a player takes the next number, the value of the number is added to the score of the player, and the highest score wins the game. Given the N number (from left to right), assuming that the players are very smart, ask the score of the last two people (assuming that the first person takes the number first ).
Input Format: the first act is N (2 <= n <= 100), and the second act is N. Each number is separated by a space. The output is the score of two players. The first number indicates the score of the first player, and the second number indicates the score of the second player. The two numbers are separated by spaces.
As input
6
4 7 2 9 5 2
Output
18 11
============================Code====================
/* Copyright (c) aprin at Xiamen University
* 2005-04
*/
# Include <stdio. h>
# Define maxn100
Int sum (INT data [], int head, int tail );
Int best (INT data [], int head, int tail ){
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;/* 0 data records */
If (tail = head + 1)
Return (* (Data + head) <* (Data + tail ))? Head: tail;/* take the smaller one */
If (BEST (data, Head, tail) = head)/* change the working pointer and let the other party select 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 <n; I ++)
Scanf ("% d", Data + I );
Total1 = total2 = 0;
Head = 0;
Tail = n-1;
For (I = 0; I <n; I ++ ){
Temp = Best (data, Head, tail );
If (I + 1) % 2! = 0)
Total1 = total1 + * (Data + temp );
Else
Total2 = total2 + * (Data + temp );
If (temp = head)
Head = head + 1;
Else
Tail = tail-1;
}
Printf ("% d/N", total1, total2 );
Getch ();
Return 0;
}