Skip to content

Commit

Permalink
Lesson 6 Complete
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikita-boop committed Jan 17, 2023
1 parent b1009de commit 3e4f3eb
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 16 deletions.
18 changes: 15 additions & 3 deletions src/by/it/group151051/nebolsin/lesson06/A_LIS.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,25 @@ int getSeqSize(InputStream stream) throws FileNotFoundException {
m[i] = scanner.nextInt();
}
//тут реализуйте логику задачи методами динамического программирования (!!!)
int result = 0;


int result = dynamic_lis_count(m, n);
//!!!!!!!!!!!!!!!!!!!!!!!!! КОНЕЦ ЗАДАЧИ !!!!!!!!!!!!!!!!!!!!!!!!!
return result;
}

static int dynamic_lis_count(int[] a, int n) {
int[] lis = new int[n];
int i, j, max = 0;

for (i = 0; i < n; i++) lis[i] = 1;
for (i = 1; i < n; i++)
for (j = 0; j < i; j++)
if (a[i] > a[j] && lis[i] < lis[j] + 1)
lis[i] = lis[j] + 1;
for (i = 0; i < n; i++)
if (max < lis[i])
max = lis[i];
return max;
}

public static void main(String[] args) throws FileNotFoundException {
String root = System.getProperty("user.dir") + "/src/";
Expand Down
25 changes: 21 additions & 4 deletions src/by/it/group151051/nebolsin/lesson06/B_LongDivComSubSeq.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,20 +43,37 @@ int getDivSeqSize(InputStream stream) throws FileNotFoundException {
m[i] = scanner.nextInt();
}
//тут реализуйте логику задачи методами динамического программирования (!!!)
int result = 0;

int result = dynamic_lms_count(m,n);

//!!!!!!!!!!!!!!!!!!!!!!!!! КОНЕЦ ЗАДАЧИ !!!!!!!!!!!!!!!!!!!!!!!!!
return result;
}

static int dynamic_lms_count(int[] a, int n) {
int[] lms = new int[n];
int i, j, max = 0;

for (i = 0; i < n; i++) lms[i] = 1;
for (i = 1; i < n; i++)
for (j = 0; j < i; j++)
if(a[i] >= 0 && a[j] > 0 && a[i] != a[j]) {
if (a[i]%a[j] == 0 && lms[i] < lms[j] + 1)
lms[i] = lms[j] + 1;
}
for (i = 0; i < n; i++)
if (max < lms[i])
max = lms[i];

System.out.println(max);
return max;
}


public static void main(String[] args) throws FileNotFoundException {
String root = System.getProperty("user.dir") + "/src/";
InputStream stream = new FileInputStream(root + "by/it/a_khmelev/lesson06/dataB.txt");
InputStream stream = new FileInputStream(root + "by/it/group151051/voronko/lesson06/dataB.txt");
B_LongDivComSubSeq instance = new B_LongDivComSubSeq();
int result = instance.getDivSeqSize(stream);
System.out.print(result);
}

}
18 changes: 15 additions & 3 deletions src/by/it/group151051/nebolsin/lesson06/C_LongNotUpSubSeq.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,26 @@ int getNotUpSeqSize(InputStream stream) throws FileNotFoundException {
m[i] = scanner.nextInt();
}
//тут реализуйте логику задачи методами динамического программирования (!!!)
int result = 0;

int result = dynamic_lds_count(m, n);

//!!!!!!!!!!!!!!!!!!!!!!!!! КОНЕЦ ЗАДАЧИ !!!!!!!!!!!!!!!!!!!!!!!!!
return result;
}

static int dynamic_lds_count(int[] a, int n) {
int[] lds = new int[n];
int i, j, max = 0;

for (i = 0; i < n; i++) lds[i] = 1;
for (i = 1; i < n; i++)
for (j = 0; j < i; j++)
if (a[i] <= a[j] && lds[i] < lds[j] + 1)
lds[i] = lds[j] + 1;
for (i = 0; i < n; i++)
if (max < lds[i])
max = lds[i];
return max;
}

public static void main(String[] args) throws FileNotFoundException {
String root = System.getProperty("user.dir") + "/src/";
Expand All @@ -65,5 +78,4 @@ public static void main(String[] args) throws FileNotFoundException {
int result = instance.getNotUpSeqSize(stream);
System.out.print(result);
}

}
12 changes: 6 additions & 6 deletions src/by/it/group151051/nebolsin/lesson06/Lesson6Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
public class Lesson6Test {
@Test
public void A() throws Exception {
String root = System.getProperty("user.dir") + "/src/";
InputStream stream = new FileInputStream(root + "by/it/a_khmelev/lesson06/dataA.txt");
String root = System.getProperty("user.dir") + "/Prog/src/";
InputStream stream = new FileInputStream(root + "by/it/group151051/nebolsin/lesson06/dataA.txt");
A_LIS instance = new A_LIS();
int result=instance.getSeqSize(stream);
boolean ok=(result==3);
Expand All @@ -21,8 +21,8 @@ public void A() throws Exception {

@Test
public void B() throws Exception {
String root = System.getProperty("user.dir") + "/src/";
InputStream stream = new FileInputStream(root + "by/it/a_khmelev/lesson06/dataB.txt");
String root = System.getProperty("user.dir") + "/Prog/src/";
InputStream stream = new FileInputStream(root + "by/it/group151051/nebolsin/lesson06/dataB.txt");
B_LongDivComSubSeq instance=new B_LongDivComSubSeq();
int result=instance.getDivSeqSize(stream);
boolean ok=(result==3);
Expand All @@ -31,8 +31,8 @@ public void B() throws Exception {

@Test(timeout = 1000)
public void C() throws Exception {
String root = System.getProperty("user.dir") + "/src/";
InputStream stream = new FileInputStream(root + "by/it/a_khmelev/lesson06/dataC.txt");
String root = System.getProperty("user.dir") + "/Prog/src/";
InputStream stream = new FileInputStream(root + "by/it/group151051/nebolsin/lesson06/dataC.txt");
C_LongNotUpSubSeq instance = new C_LongNotUpSubSeq();
int result=instance.getNotUpSeqSize(stream);
boolean ok=(result==4);
Expand Down

0 comments on commit 3e4f3eb

Please sign in to comment.