← 返回 apple 的题目列表Multithreaded Sum Calculation
类型:online_judge
apple
Write a program to calculate the sum of an integer array using multiple threads. Requirements:
Divide the array into multiple segments, each thread should calculate the sum of one segment.
Set the number of threads reasonably to minimize computation time.
Combine the results from all threads to get the final sum.
Provide at least three test cases, including:
A regular integer array
An array containing negative numbers
An array of length zero
Data scale: The length of the array should not exceed 1000000.
Example Input
nums = [1, 2, 3, 4, 5]
Example Output
15
Example
Input
5
1 2 3 4 5