Skip to content

Instantly share code, notes, and snippets.

@gmakc-094423
Created September 7, 2022 21:08
Show Gist options
  • Save gmakc-094423/51496619bb1cfc983f886b7a6116131e to your computer and use it in GitHub Desktop.
Save gmakc-094423/51496619bb1cfc983f886b7a6116131e to your computer and use it in GitHub Desktop.
Домашнее задание к 1 семинару
public class dz_01 {
static int sum(int[] arr, int start, int end) {
int sum = 0;
for (int i = start; i < end; i++) {
sum += arr[i];
}
return sum;
}
public static void main(String[] args) {
int[] nums = { 1, 7, 3, 6, 5, 6 }; // 3
// int[] nums = { 1, 7, 3, 6, 5, 7, 10, 13 }; // no result
// int[] nums = { 29, 1, 7, 3, 6, 5, 7, 1 }; // 1
int middle = nums.length / 2;
int count = middle;
boolean flag = true;
while (flag) {
if (count < 1) { // выход из цикла если решения не существует
System.out.printf("NO RESULT");
flag = false;
}
count = count / 2;
if (sum(nums, 0, middle) == sum(nums, middle + 1, nums.length)) {
System.out.printf("Index = %d", middle);
flag = false;
} else if (sum(nums, 0, middle) < sum(nums, middle + 1, nums.length)) {
middle += count;
} else {
middle -= count;
}
}
}
}
public class dz_01_02 {
static boolean check(String[] str, String substr) {
boolean res = false;
for (String i : str) {
if (i.contains(substr)) {
res = true;
} else
return false;
}
return res;
}
public static void main(String[] args) {
String[] str = new String[] { "aabb", "aabbb", "aaabb" };
String pref = str[0];
boolean flag = false;
int endIndex = str[0].length();
while (endIndex > 1 && !flag) {
pref = str[0].substring(0, endIndex);
flag = check(str, pref);
endIndex--;
}
if (flag) {
System.out.printf("Search prefix = %s\n", pref);
} else {
System.out.printf("NONE");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment