import java.util.Scanner;
public class b {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[][] nums = new int[n][n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
nums[i][j] = sc.nextInt();
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n - 1; j++) {
if (nums[i][j] == nums[i][j + 1]) {
nums[i][j] *= 2;
nums[i][j + 1] = 0;
}
}
}
for (int i = 0; i < n; i++) {
int start = 0;
for (int j = 0; j < n; j++) {
if(j==start&&nums[i][j] != 0) {
start++;
continue;
}
if (nums[i][j] != 0) {
nums[i][start++] = nums[i][j];
nums[i][j] = 0;
}
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (j != 0)
System.out.print(" " + nums[i][j]);
else
System.out.print(nums[i][j]);
}
System.out.println();
}
}
}
代码