Skip to content

Commit 46990da

Browse files
committed
Refactor
1 parent ec77a0b commit 46990da

File tree

9 files changed

+96
-99
lines changed

9 files changed

+96
-99
lines changed

src/II/_2_Traverce/Solver.java

Lines changed: 0 additions & 82 deletions
This file was deleted.

src/II/_1_Pascal/Solver.java renamed to src/Lab_II/_1_Pascal/Solver.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
package II._1_Pascal;
1+
package Lab_II._1_Pascal;
22

33
import javafx.util.Pair;
44
import java.util.HashMap;
55

66
public class Solver {
7-
8-
private static HashMap<Pair<Integer,Integer>, Integer> combinationsCache = new HashMap<>();
7+
static HashMap<Pair<Integer,Integer>, Integer> combinationsCache = new HashMap<>();
8+
static int n = 6;
99

1010
public static void main() {
11-
for (int i = 1; i <= 6; i++,System.out.println())
11+
for (int i = 1; i <= n; i++,System.out.println())
1212
for (int j = 1; j <= i; j++)
1313
System.out.print(combinations(new Pair(i,j)) + " ");
1414
}

src/Lab_II/_2_Traverse/Solver.java

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package Lab_II._2_Traverse;
2+
3+
public class Solver {
4+
static int n = 5;
5+
static int value = 1;
6+
static int[][] array = new int[n+1][n+1];
7+
8+
public static void main() {
9+
traverseFirstWay(0,1,1);
10+
printMatrix();
11+
12+
traverseSecondWay(1,1);
13+
printMatrix();
14+
15+
traverseThirdWay(1);
16+
printMatrix();
17+
}
18+
19+
public static void printMatrix() {
20+
for (int i = 1; i <= n; i++,System.out.println())
21+
for (int j = 1; j <= n; j++)
22+
System.out.printf("%3d",array[i][j]);
23+
System.out.println();
24+
}
25+
26+
public static void traverseFirstWay(int turns, int row, int col) {
27+
int distance = n - (int)Math.round((float) turns / 2 + 0.25);
28+
if (distance == 0) return;
29+
int irow, icol;
30+
switch (turns % 4) {
31+
case 0: // Right
32+
for (icol = col; icol < col + distance; icol++)
33+
array[row][icol] = value++;
34+
traverseFirstWay(++turns, ++row, --icol); break;
35+
case 1: // Down
36+
for (irow = row; irow < row + distance; irow++)
37+
array[irow][col] = value++;
38+
traverseFirstWay(++turns,--irow,--col); break;
39+
case 2: // Left
40+
for (icol = col; icol > col - distance; icol--)
41+
array[row][icol] = value++;
42+
traverseFirstWay(++turns,--row,++icol); break;
43+
case 3: // Up
44+
for (irow = row; irow > row - distance; irow--)
45+
array[irow][col] = value++;
46+
traverseFirstWay(++turns, ++irow, ++col); break;
47+
}
48+
}
49+
50+
public static void traverseSecondWay(int row, int col) {
51+
array[col][row] = value++;
52+
if (row == n && col == n) return;
53+
54+
if ((row + col) % 2 == 0)
55+
if (row == n)
56+
traverseSecondWay(row, ++col);
57+
else
58+
traverseSecondWay(++row, col == 1 ? col : --col);
59+
else
60+
if (col == n)
61+
traverseSecondWay(++row, col);
62+
else
63+
traverseSecondWay(row == 1 ? row : --row, ++col);
64+
}
65+
66+
public static void traverseThirdWay(int col) {
67+
if (col > n) return;
68+
else
69+
if (col % 2 == 0) {
70+
for (int row = n; row > 0; row--)
71+
array[row][col] = value++;
72+
traverseThirdWay(++col);
73+
}
74+
else {
75+
for (int i = 1; i <= n; i++)
76+
array[i][col] = value++;
77+
traverseThirdWay(++col);
78+
}
79+
}
80+
}

src/III/_1_Collections/Solver.java renamed to src/Lab_III/_1_Collections/Solver.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package III._1_Collections;
1+
package Lab_III._1_Collections;
22

33
import java.util.Arrays;
44
import java.util.Collection;

src/III/_2_Primes/PrimesGenerator.java renamed to src/Lab_III/_2_Primes/PrimesGenerator.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package III._2_Primes;
1+
package Lab_III._2_Primes;
22

33
import java.util.Iterator;
44

@@ -20,7 +20,7 @@ public boolean hasNext() {
2020

2121
public Integer next() {
2222
while (!isPrime(from < to ? current++ : current--));
23-
return current - 1;
23+
return from < to ? current - 1 : current + 1;
2424
}
2525

2626
private boolean isPrime(int number)
@@ -29,7 +29,7 @@ private boolean isPrime(int number)
2929
if (number < 2) return false;
3030
if (number < 4) return true;
3131
if (number % 2 == 0) return false;
32-
for (int i = 3; i < Math.ceil(Math.sqrt(number)); i+=2)
32+
for (int i = 3; i < Math.sqrt(number) + 1; i+=2)
3333
if (number % i == 0) return false;
3434
return true;
3535
}

src/III/_2_Primes/Solver.java renamed to src/Lab_III/_2_Primes/Solver.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package III._2_Primes;
1+
package Lab_III._2_Primes;
22

33
public class Solver {
44
public static void main() {

src/III/_3_Comparator/Human.java renamed to src/Lab_III/_3_Comparator/Human.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package III._3_Comparator;
1+
package Lab_III._3_Comparator;
22

33
import java.util.Comparator;
44

src/III/_3_Comparator/Solver.java renamed to src/Lab_III/_3_Comparator/Solver.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package III._3_Comparator;
1+
package Lab_III._3_Comparator;
22

33
import java.util.Collection;
44
import java.util.HashSet;

src/Main.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
public class Main {
2-
32
public static void main(String[] args) {
4-
II._1_Pascal.Solver.main();
5-
II._2_Traverce.Solver.main();
3+
Lab_II._1_Pascal.Solver.main();
4+
Lab_II._2_Traverse.Solver.main();
65

7-
III._1_Collections.Solver.main();
8-
III._2_Primes.Solver.main();
9-
III._3_Comparator.Solver.main();
6+
Lab_III._1_Collections.Solver.main();
7+
Lab_III._2_Primes.Solver.main();
8+
Lab_III._3_Comparator.Solver.main();
109
}
1110
}

0 commit comments

Comments
 (0)