标题:java 代码在eclipse上能运行,在oj系统就有错误,代码的bug在哪里,怎么修改 ...
只看楼主
java小白兔
Rank: 1
等 级:新手上路
帖 子:29
专家分:3
注 册:2018-11-7
结帖率:100%
已结贴  问题点数:20 回复次数:3 
java 代码在eclipse上能运行,在oj系统就有错误,代码的bug在哪里,怎么修改?

import java.util.*;

class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            int N = sc.nextInt();
            MultiValueMap<String, String> stringMultiValueMap = new LinkedMultiValueMap<>();
            while (N-- > 0) {
                String id = sc.next();
                String time = sc.next();
                stringMultiValueMap.add(id, time);

            }
            Set<String> keySet = stringMultiValueMap.keySet();
            for (String key : keySet) {
                List<String> values = stringMultiValueMap.getValues(key);
                if (values.size() < 6)
                    continue;
                ArrayList<Integer> total_times = new ArrayList<>();
                for (String value : values) {
                    int h = Integer.parseInt(value.substring(0, 2));
                    int d = Integer.parseInt(value.substring(3, 5));
                    int s = Integer.parseInt(value.substring(6, 8));
                    int total_time = h * 3600 + d * 60 + s;
                    total_times.add(total_time);
                }
                Collections.sort(total_times);
                if (check(total_times))
                    System.out.println(key);
            }
        }
    }

    private static boolean check(ArrayList<Integer> list) {
        for (int i = 0; i <= list.size() - 6; i++) {
            for (int j = i + 1; j < i + 6; j++) {
                if (Math.abs(list.get(i) - list.get(j)) >= 3600)
                    return false;
            }
        }
        return true;
    }

}

interface MultiValueMap<K, V> {
    void add(K key, V value);

    void add(K key, List<V> values);

    void set(K key, V value);

    void set(K key, List<V> values);

    void set(Map<K, List<V>> values);

    List<V> remove(K key);

    void clear();

    Set<K> keySet();

    List<V> values();

    V getValue(K key, int index);

    List<V> getValues(K key);

    int size();

    boolean isEmpty();

    boolean containsKey(K key);

}

class LinkedMultiValueMap<K, V> implements MultiValueMap<K, V> {
    protected Map<K, List<V>> mSource = new LinkedHashMap<K, List<V>>();

    public LinkedMultiValueMap() {
    }

    @Override
    public void add(K key, V value) {
        if (key != null) {
            if (!mSource.containsKey(key))
                mSource.put(key, new ArrayList<V>(2));
            mSource.get(key).add(value);
        }
    }

    @Override
    public void add(K key, List<V> values) {
        for (V value : values) {
            add(key, value);
        }
    }

    @Override
    public void set(K key, V value) {
        mSource.remove(key);
        add(key, value);
    }

    @Override
    public void set(K key, List<V> values) {
        mSource.remove(key);
        add(key, values);
    }

    @Override
    public void set(Map<K, List<V>> map) {
        mSource.clear();
        mSource.putAll(map);
    }

    @Override
    public List<V> remove(K key) {
        return mSource.remove(key);
    }

    @Override
    public void clear() {
        mSource.clear();
    }

    @Override
    public Set<K> keySet() {
        return mSource.keySet();
    }

    @Override
    public List<V> values() {
        List<V> allValues = new ArrayList<V>();
        Set<K> keySet = mSource.keySet();
        for (K key : keySet) {
            allValues.addAll(mSource.get(key));
        }
        return allValues;
    }

    @Override
    public List<V> getValues(K key) {
        return mSource.get(key);
    }

    @Override
    public V getValue(K key, int index) {
        List<V> values = mSource.get(key);
        if (values != null && index < values.size())
            return values.get(index);
        return null;
    }

    @Override
    public int size() {
        return mSource.size();
    }

    @Override
    public boolean isEmpty() {
        return mSource.isEmpty();
    }

    @Override
    public boolean containsKey(K key) {
        return mSource.containsKey(key);
    }
}


搜索更多相关主题的帖子: public key List values Override 
2018-11-07 14:02
幻紫灵心
Rank: 11Rank: 11Rank: 11Rank: 11
来 自:山咔咔里面
等 级:贵宾
威 望:53
帖 子:395
专家分:2640
注 册:2018-3-30
得分:10 
有题目地址吗?

saber,别哭.
2018-11-07 22:25
java小白兔
Rank: 1
等 级:新手上路
帖 子:29
专家分:3
注 册:2018-11-7
得分:0 
回复 2楼 幻紫灵心
这是要学校内部网才能进的
2018-11-08 08:26
纵横阳仔
Rank: 7Rank: 7Rank: 7
来 自:自学成才的火星人儿
等 级:贵宾
威 望:26
帖 子:147
专家分:698
注 册:2016-10-20
得分:10 
while循环不需要吧.

check逻辑错误了
你应该要查找一组满足条件的数据,而不是找出一组不满足的条件;如下面的数据,而且根据前面的逻辑不需要两层循环,应为已经排过序了.
7
s1 00:00:00
s1 02:00:01
s1 02:00:02
s1 02:00:03
s1 02:00:04
s1 02:00:05
s1 02:00:06
正确逻辑:
 private static boolean check(ArrayList<Integer> list) {
        for (int i = 0; i <= list.size() - 6; i++) {
                if (Math.abs(list.get(i) - list.get(i+5)) <= 3600)
                    return true;
        }
        return false;
    }


[此贴子已经被作者于2018-11-8 15:13编辑过]

2018-11-08 15:06



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-491055-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.248176 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved