网易互娱在线编程纪要

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/rebornyp/article/details/80113780

网易互娱在线编程纪要

第一题

public class Main {
    private static class Team implements Comparable<Team>{
        String name;
        int score, pureGoal, totalGoals;
        public Team(String name) {
            this.name = name;
        }
        @Override
        public int compareTo(Team t) {
            return -(score != t.score ? score - t.score : pureGoal != t.pureGoal ? pureGoal - t.pureGoal : 
                totalGoals != t.totalGoals ? totalGoals - t.totalGoals : -name.compareTo(t.name));
        }
        @Override
        public String toString() {
            return name;
        }
    }   
    public static void main(String[] args) throws IOException {
        Read.init(System.in);
        String t1, t2;
        Map<String, Team> map = new HashMap<>();
        for (int k=0; k<4; k++) {
            map.clear();
            for (int i=0; i<6; i++) {
                t1 = Read.next();
                String temp = Read.next();
                t2 = Read.next();
                int v1 = temp.charAt(0) - '0', v2 = temp.charAt(2) - '0';
                int s1, s2;
                if (v1 > v2) {
                    s1 = 3;
                    s2 = 0;
                } else if (v1 < v2) {
                    s1 = 0;
                    s2 = 3;
                } else {
                    s1 = s2 = 1;
                }
                if (!map.containsKey(t1)) {
                    Team t = new Team(t1);
                    t.score = s1;
                    t.pureGoal = v1 - v2;
                    t.totalGoals = v1;
                    map.put(t1, t);
                } else {
                    map.get(t1).score += s1;
                    map.get(t1).pureGoal += (v1 - v2);
                    map.get(t1).totalGoals += v1;
                }
                if (!map.containsKey(t2)) {
                    Team t = new Team(t2);
                    t.score = s2;
                    t.pureGoal = v2 - v1;
                    t.totalGoals = v2;
                    map.put(t2, t);
                } else {
                    map.get(t2).score += s2;
                    map.get(t2).pureGoal += (v2 - v1);
                    map.get(t2).totalGoals += v2;
                }               
            }
            List<Team> list = new ArrayList<>(map.values());
            Collections.sort(list);
            for (int i=0; i<4; i++) {
                if (i == 0) System.out.print(list.get(i));
                else System.out.print(" " + list.get(i));
            }
            System.out.println();
        }
    }

}

第二题

public class Main {
    public static void main(String[] args) throws IOException {
        Read.init(System.in);
        int M = Read.nextInt(), N = Read.nextInt();
        int[][] num = new int[M][N];
        for (int i=0; i<M; i++) {
            for (int j=0; j<N; j++) {
                String temp = Read.next();
                if (temp.length() == 4 && temp.charAt(1) == 'i') {
                    if (temp.charAt(0) == '+')
                        num[i][j] = 111;
                    else
                        num[i][j] = -111;
                } else num[i][j] = Integer.parseInt(temp);
            }
        }
        int q = Read.nextInt();
        int x1, x2, y1, y2;
        for (int i=0; i<q; i++) {
            x1 = Read.nextInt() - 1;
            y1 = Read.nextInt() - 1;
            x2 = Read.nextInt() - 1;
            y2 = Read.nextInt() - 1;
            System.out.println(help(num, x1, x2, y1, y2));
        }
    }

    private static String help(int[][] num, int x1, int x2, int y1, int y2) {
        int sum = 0;
        boolean pinf = false, ninf = false;
        for (int i=x1; i<=x2; i++) {
            for (int j=y1; j<=y2; j++) {
                if (num[i][j] == 111) pinf = true;
                else if (num[i][j] == -111) ninf = true;
                else sum += num[i][j];
                if (pinf && ninf) return "NaN";
            }
        }
        if (pinf) return "+inf";
        else if (ninf) return "-inf";
        else return "" + sum;
    }
}

第三题(未完成)

public class Main { 
    private static class Room {
        boolean isFull = false;
        int[] val = new int[6];
        int empty = 6;
    }
    static int t1, t2, t3; 
    public static void main(String[] args) throws IOException {
        Read.init(System.in);
        int N = Read.nextInt();
        t1 = Read.nextInt();
        t2 = Read.nextInt();
        t3 = Read.nextInt();
        List<Room> lr = new ArrayList<>();
        int[] add = new int[N];
        for (int i=0; i<N; i++) {
            add[i] = Read.nextInt();
            int k = Read.nextInt();
            List<Integer> list = new ArrayList<>();
            for (int j=0; j<k; j++) {
                list.add(Read.nextInt());
            }
            if (lr.size() == 0 || needNew(lr, list)) {
                Room r = new Room();
                r.empty = 6 - list.size();
                for (int j=0; j<list.size(); j++)
                    r.val[i] = list.get(i);
                if (r.empty == 0) r.isFull = true;
                lr.add(r);
            } else {
                int pre = -1;
                boolean first = true;
                for (int j=0; j<lr.size(); j++) {
                    if (lr.get(j).isFull) continue;
                    if (first && lr.get(j).empty >= list.size()) {
                        first = false;
                        pre = j;
                        int sum = 0, sum2 = 0;
                        for (int k1=0; k1<6; k1++) sum += lr.get(j).val[k1];
                        for (int k1=0; k1<list.size(); k1++) {
                            sum += list.get(k1);
                            sum2 += list.get(k1);
                        }
                        sum /= (6 - lr.get(j).empty + list.size());
                        sum2 /= list.size();
                        if (Math.abs(sum - sum2) < t1) {
                            lr.get(j).empty -= list.size();
                            for (int k1=6-lr.get(j).empty; k1<6-lr.get(j).empty+list.size(); k1++)
                                lr.get(j).val[k1] = list.get(k1-6-lr.get(j).empty);
                            if (lr.get(j).empty == 0) lr.get(j).isFull = true;
                        } 
                    }
                }
                lr.get(pre).empty -= list.size();
                for (int k1=6-lr.get(pre).empty; k1<6-lr.get(pre).empty+list.size(); k1++)
                    lr.get(pre).val[k1] = list.get(k1-6-lr.get(pre).empty);
                if (lr.get(pre).empty == 0) lr.get(pre).isFull = true;
            }
            /*
            未完待续
            */
        }
    }   
    private static boolean needNew(List<Room> lr, List<Integer> list) {
        int i = 0;
        for (; i<lr.size(); i++) {
            if (!lr.get(i).isFull && lr.get(i).empty >= list.size()) break;
        }
        if (i == lr.size()) return true;
        return false;
    }
}

统一的读入方式

class Read {
    static BufferedReader br;
    static StringTokenizer st;
    static void init(InputStream in) {
        br = new BufferedReader(new InputStreamReader(in));
        st = new StringTokenizer("");
    }
    public static double nextDouble() throws IOException {
        return Double.parseDouble(next());
    }
    static String next() throws IOException {
        while (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
    static int nextInt() throws IOException {
        return Integer.parseInt(next());
    }
}

C版本的第二题

#include <stdio.h>
#include <string>
#include <iostream>
using namespace std;
int M, N;
int a[1005][1005];
void help(int x1, int x2, int y1, int y2) {
    int sum = 0;
    bool b1 = false, b2 = false;
    for (int i=x1; i<=x2; i++) {
        for (int j=y1; j<=y2; j++) {
            if (a[i][j] == 111) b1 = true;
            else if (a[i][j] == -111) b2 = true;
            else sum += a[i][j];
            if (b1 && b2) {
                printf("NaN\n"); 
                return;
            }
        }
    }
    if (b1) printf("+inf");
    else if (b2) printf("-inf");
    else printf("%d", sum);
    printf("\n");
}
int main() {    
    scanf("%d%d", &M, &N);
    string s;
    for (int i=0; i<M; i++) {
        for (int j=0; j<N; j++) {
            cin >> s;
            if (s.length() == 4 && s[1] == 'i') {
                if (s[0] == '+') a[i][j] = 111;
                else a[i][j] = -111;
            } else a[i][j] = stoi(s);
        }
    }
    int q;
    scanf("%d", &q);
    int x1, y1, x2, y2;
    for (int i=0; i<q; i++) {
        cin >> x1 >> y1 >> x2 >> y2;
        help(x1-1, x2-1, y1-1, y2-1);
    }
    return 0;
}

总结

  • 浪费时间想在时间上AC第二题,于是写了个C版本,但还是没通过,结果浪费了大量时间,导致第三题没写完;
  • 大概也就一周没刷题,感觉思路就没那么便捷了,看来还是自己太菜;
  • 第三道题思路不难,主要是很复杂,得细细捋一遍,等有时间再补上完整代码;

猜你喜欢

转载自blog.csdn.net/rebornyp/article/details/80113780