Skip to content

Instantly share code, notes, and snippets.

@geakstr
Created February 19, 2016 12:25
Show Gist options
  • Save geakstr/257c3728db44ff87f87e to your computer and use it in GitHub Desktop.
Save geakstr/257c3728db44ff87f87e to your computer and use it in GitHub Desktop.
import java.util.*;
public class Main {
public static void main(String[] args) {
int N = 10, M = 54;
int probability = 90;
int[][] table = new int[N][M];
/// Генерим таблицу ///
Random rnd = new Random();
for (int i = 0; i < N; i++) {
for (int j = 0; j < M / 2 - 3; j++) {
int x = rnd.nextInt(100) + 1;
table[i][j] = x >= probability ? rnd.nextInt(5) : 0;
}
}
for (int i = 0; i < N; i++) {
for (int j = M / 2 + 3; j < M; j++) {
int x = rnd.nextInt(100) + 1;
table[i][j] = x >= probability ? rnd.nextInt(5) : 0;
}
}
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
System.out.print(" " + (table[i][j] == 0 ? "-" : table[i][j]) + " ");
}
System.out.println();
}
/// Находим недели, разделяющие семестры ///
int globalMin = 0, globalMax = 0;
outer: for (int localMin = 0; localMin < M;) {
for (int localMax = localMin; localMax < M; localMax++) {
int row = 0;
while (row < N && table[row][localMax] == 0) {
row++;
}
if (row == N) {
if (localMax - localMin >= globalMax - globalMin) {
globalMin = localMin;
globalMax = localMax;
}
} else {
localMin = localMax + 1;
continue outer;
}
}
localMin++;
}
System.out.println("\nКаникулы c " + globalMin + " по " + globalMax + " нед.");
/// Ищем сумму в столбце ///
int sem = 1; // Интересующий семестр, 1 - первый, 2 - второй
int rangeMin = sem == 1 ? 0 : globalMax; int rangeMax = sem == 1 ? globalMin : M - 1;
for (int j = rangeMin; j < rangeMax; j++) {
int sum = 0;
for (int i = 0; i < N; i++) {
sum += table[i][j];
}
if (sum > 0) {
System.out.println("Сумма " + sum + " для " + j + " недели");
break;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment