-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathP120.java
More file actions
62 lines (50 loc) · 1.27 KB
/
P120.java
File metadata and controls
62 lines (50 loc) · 1.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package p100_199;
import java.util.Arrays;
import java.util.Scanner;
//accepted
public class P120 {
static int[][] crearMatriz(int lado, int n){
int x = lado/2;
int y = 0;
int xAnterior;
int yAnterior;
int [][] matriz = new int[lado][lado];
for (int[] row : matriz) {
Arrays.fill(row, -1);
}
for (int i = 0; i < lado*lado; i++) {
matriz[x][y]=n;
xAnterior=x;
yAnterior=y;
n++;
y--;
if (y<0){
y=lado-1;
}
x++;
if (x>=lado){
x=0;
}
if (matriz[x][y]!=-1){
x=xAnterior;
y=yAnterior+1;
}
}
return matriz;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int k = sc.nextInt();
while (n!=0 || k!=0){
int acumulador = 0;
int [][] matriz = crearMatriz(n,k);
for (int i = 0; i < n; i++) {
acumulador+=matriz[i][i];
}
System.out.println(acumulador);
n = sc.nextInt();
k = sc.nextInt();
}
}
}