import java.util.Scanner; public class Main{ static final int N = 100010; static int[] a = new int[N]; static int[] b = new int[N]; public static void insert(int l, int r, int c){ b[l]+=c; b[r+1]-=c; } public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); int m = scanner.nextInt(); for(int i = 1;i<=n;i++) a[i] = scanner.nextInt(); for(int i = 1;i<=n;i++) insert(i,i,a[i]); while(m!=0){ m--; int l = scanner.nextInt(); int r = scanner.nextInt(); int c = scanner.nextInt(); insert(l,r,c); } for(int i = 1;i<=n;i++) b[i]+=b[i-1]; for(int i = 1;i<=n;i++) System.out.print(b[i]+" "); System.out.println(); } } 差分矩阵 import java.util.Scanner; public class Main{ static int N = 1010; static int a[][] = new int[N][N]; static int b[][] = new int[N][N]; static void insert(int x1,int y1,int x2,int y2,int c){ b[x1][y1]+=c; b[x2+1][y1]-=c; b[x1][y2+1]-=c; b[x2+1][y2+1]+=c; } public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); int m = scanner.nextInt(); int q = scanner.nextInt(); for(int i = 1;i<=n;i++){ for(int j = 1;j<=m;j++){ a[i][j] = scanner.nextInt(); } } for (int i = 1; i <=n; i++) { for (int j = 1; j <=m ; j++) { insert(i,j,i,j,a[i][j]); } } while(q!=0){ q--; int x1 = scanner.nextInt(); int y1 = scanner.nextInt(); int x2 = scanner.nextInt(); int y2 = scanner.nextInt(); int c = scanner.nextInt(); insert(x1,y1,x2,y2,c); } for (int i = 1; i <=n; i++) { for (int j = 1; j <=m; j++) { b[i][j] +=b[i-1][j]+b[i][j-1]-b[i-1][j-1]; } } for(int i = 1;i<=n;i++){ for(int j = 1;j<=m;j++){ System.out.print(b[i][j]+" "); } System.out.println(); } } }
标签:scanner,int,补交,周五,11.24,static,new,public,Scanner From: https://www.cnblogs.com/muzhaodi/p/17856335.html