Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AccordEuro06/370fcb6cd0d6d4d04d02e787508c2583 to your computer and use it in GitHub Desktop.
Save AccordEuro06/370fcb6cd0d6d4d04d02e787508c2583 to your computer and use it in GitHub Desktop.
package com.javarush.task.task08.task0812;
Cамая длинная последовательность
1. Создай список чисел.
2. Добавь в список 10 чисел с клавиатуры.
3. Вывести на экран длину самой длинной последовательности повторяющихся чисел в списке.
Пример для списка 2, 4, 4, 4, 8, 8, 9, 12, 12, 14:
3
Искомое значение равно 3, т.к. самая длинная последовательность повторяющихся чисел состоит из трех четверок.
package com.javarush.task.task08.task0812;
import java.io.*;
import java.util.ArrayList;
/*
Cамая длинная последовательность
*/
public class Solution {
public static void main(String[] args) throws IOException {
//напишите тут ваш код
}
}
@AccordEuro06
Copy link
Author

package com.javarush.task.task08.task0812;

import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;

/*
Cамая длинная последовательность
*/

public class Solution {
    public static void main(String[] args) throws IOException {
        ArrayList<Integer> list = new ArrayList<>();
        ArrayList<Integer> tmp = new ArrayList<>();
        Scanner sc = new Scanner(System.in);
        for (int i = 0; i < 10; i++) {
            list.add(sc.nextInt());
        }
        int count = 1;
        int max = 1;
        for (int i=0; i<list.size()-1;i++)
        {
            if (list.get(i) == list.get(i + 1))
            {
                count++;
                if (max < count)
                    max = count;
            }
            else
                count=1;
        }
        System.out.println(max);
    }
}

@ArmanAbdullin
Copy link

Воооошпе не понял

@IuriyG
Copy link

IuriyG commented Apr 10, 2018

А зачем второй список?

@EugeneOrda
Copy link

второй список не нужен

public class Solution{

public static void main(String[] args) throws IOException {

    BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

    ArrayList<Integer> arr = new ArrayList<>();
    int temp = 1;
    int big = 1;
    for (int i = 0; i < 10; i++) {
        arr.add(i, Integer.parseInt(reader.readLine()));
    }

    for (int j = 0; j < arr.size() - 1; j++) {
        if (arr.get(j).equals(arr.get(j + 1))) {
            temp++;
        }
            if (temp >= big) big = temp;

        else temp = 1;
    }

    System.out.println(big);
}

@alehfedaruk
Copy link

второй список не нужен

public class Solution{

public static void main(String[] args) throws IOException {

    BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

    ArrayList<Integer> arr = new ArrayList<>();
    int temp = 1;
    int big = 1;
    for (int i = 0; i < 10; i++) {
        arr.add(i, Integer.parseInt(reader.readLine()));
    }

    for (int j = 0; j < arr.size() - 1; j++) {
        if (arr.get(j).equals(arr.get(j + 1))) {
            temp++;
        }
            if (temp >= big) big = temp;

        else temp = 1;
    }

    System.out.println(big);
}

temp >= big should be changed to temp > big, otherwise it doesn't count what it has to

@VadymLuboml1
Copy link

VadymLuboml1 commented Dec 14, 2018

`import java.io.*;
import java.util.ArrayList;

/*
Cамая длинная последовательность
*/
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
ArrayList list = new ArrayList();
for(int i = 0;i < 10;i++) list.add(Integer.parseInt(reader.readLine()));
int max = 1;
int c = 1;
for(int i = 1;i < list.size();i++){
if(list.get(i).equals(list.get(i - 1))) c++;
if(!(list.get(i).equals(list.get(i - 1))) && c!=1|| i == list.size()-1){
if(c > max) max = c;
c = 1;
}
}
System.out.print(max);
}
}`

@SoloHandzo
Copy link

 ваш код не работает с числами больше 127

@235illino
Copy link

235illino commented Mar 31, 2021

public class Solution {

public static void main(String[] args) throws IOException {

    ArrayList<Integer> list = new ArrayList<>();
    BufferedReader r = new BufferedReader(new InputStreamReader(System.in));
    
    for (int i = 0; i < 10; i++) {
        list.add(Integer.parseInt(r.readLine()));
    }
    int count = 1;
    int max = 1;
    for (int i=0; i<list.size()-1;i++)
    {
        if (list.get(i).equals(list.get(i + 1)))
        {
            count++;
            if (max < count)
                max = count;
        }
        else
            count=1;
    }
    System.out.println(max);
}

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment