分治思想
import java.util.*; public class Main{ static int[] res; public static void main(String[] args){ Scanner sc=new Scanner(System.in); String[] s=sc.nextLine().split(" "); int n=s.length; res=new int[n]; int[] nums=new int[n]; for(int i=0;i<n;i++){ nums[i]=sc.nextInt(); } traverse(nums,0,n-1); StringBuilder sb=new StringBuilder(); for(int i=0;i<n;i++){ sb.append(res[i]).append(" "); } System.out.print(sb.toString().trim()); } public static int traverse(int[] nums,int i,int j){ int mid=(i+j)/2; if(i==j) return nums[i]; res[mid]=traverse(nums,i,mid-1)+traverse(nums,mid+1,j); return res[mid]+nums[mid]; } }