欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

一个算法题,先写一下答案,后续补充题目,太困了

程序员文章站 2022-03-27 08:52:33
package com.code.test;import java.util.ArrayList;import java.util.List;public class Test { public static void main(String[] args) { int continueThd = 2; int freqThd = 2; int timeThd = 12; int period = 1; Lis...
package com.code.test;

public class Event {
    private int startTime;
    private int endTime;

    public Event(int startTime, int endTime) {
        this.startTime = startTime;
        this.endTime = endTime;
    }

    public int getStartTime() {
        return startTime;
    }

    public void setStartTime(int startTime) {
        this.startTime = startTime;
    }

    public int getEndTime() {
        return endTime;
    }

    public void setEndTime(int endTime) {
        this.endTime = endTime;
    }
}

package com.code.test;

import java.util.ArrayList;
import java.util.List;

public class Test {
    public static void main(String[] args) {
        int continueThd = 2;
        int freqThd = 2;
        int timeThd = 12;
        int period = 1;
        List<Integer> times = new ArrayList<>();
        times.add(0);
        times.add(1);
        times.add(2);
        times.add(3);
        times.add(20);
        times.add(22);
        times.add(90);
        times.add(91);
        times.add(92);
        times.add(93);
        times.add(94);

        List<List<Integer>> result = new ArrayList<>();
        List<Event> events = new ArrayList<>();

        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < times.size() - 1; i++) {
            if (times.get(i + 1) - times.get(i) < timeThd) {
                list.add(times.get(i));
            } else {
                list.add(times.get(i));
                result.add(list);
                list = new ArrayList<>();
            }
        }

        list.add(times.get(times.size() - 1));
        result.add(list);
        System.out.println(result);

        for (List<Integer> list1 : result) {
            int continueNum = 0;
            for (int j = 0; j < list1.size() - 1; j++) {
                int first = list1.get(j);
                int second = list1.get(j + 1);
                if (second - first == period) {
                    continueNum++;
                }
            }


            if (continueNum >= continueThd) {
                Event event = new Event(list1.get(continueThd - 1), list1.get(list1.size() - 1));
                events.add(event);
                continue;
            }

            if (list1.size() >= freqThd) {
                Event event = new Event(list1.get(freqThd - 1), list1.get(list1.size() - 1));
                events.add(event);
            }
        }

        for (Event event : events) {
            System.out.println(event.getStartTime() + "  " + event.getEndTime());
        }
    }
}

本文地址:https://blog.csdn.net/qxf1374268/article/details/112001305

相关标签: 算法