Huawei OD machine test real questions - Alibaba looking for a golden treasure chest (I) - 2023 OD unified test (B paper)

Description of the topic : Ali Baba, a poor woodcutter, stumbled upon a treasure trove of a bandit group on his way to cut firewood. There are boxes numbered from 0 to N in the treasure trove, and each box has a number on it. There may be a golden treasure box in the box.

The golden treasure box satisfies the sum of the numbers of all the boxes before it equal to the sum of the numbers of all the boxes after it; the sum of the numbers on the left part of the first box is defined as 0; the sum of the numbers on the right part of the last treasure box is defined as 0.

Please help Alibaba find the golden treasure chest, and output the number of the first golden treasure chest that meets the conditions. If there is no golden treasure chest, please return -1.

Enter a description:

A list of numbers to stick on the box, separated by commas, eg 1, -1, 0.

The number of treasure chests is not less than 1 and not more than 10,000

The value range posted on the treasure chest is not less than -1000 and not more than 1000

Output description:

Number of the first golden chest

Supplementary note:

Example 1

enter:

2,5,-1,8,6

output:

3

illustrate:

The sum of the numbers before the subscript 3 is: 2 + 5 + -1 = 6

The sum of the numbers after the subscript 3 is: 6 = 6

Example 2

enter:

8,9

output:

-1

illustrate:

No matching location exists

Example 3

enter:

11

output:

0

illustrate:

Guess you like

Origin blog.csdn.net/2301_76848549/article/details/131387117