先看结果:
代码实现:
import java.util.*;
public class Test {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int n = input.nextInt();//个数
int[][] point = new int[n+1][n+1];//点
int[][] sum = new int[n+1][n+1];//二维前缀和 //输入点
for(int i =1;i<=n;i++){
for(int j =1;j<=n;j++) {//按行输入
point[i][j] = input.nextInt();
}
}
//求和
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
sum[i][j] = sum[i-1][j]+sum[i][j-1] - sum[i-1][j-1]+point[i][j];
}
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
System.out.print(sum[i][j]+" ");
}
System.out.println();
}
}
}