9.13微众银行笔试

题目不记得了,贴个代码吧

第一题

package one;

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[] a = new int[n];
        for (int i = 0; i < n; i++) {
            a[i] = sc.nextInt();
        }
        LinkedHashSet<Integer> set = new LinkedHashSet<>();
        for (int i = n - 1; i >= 0; i--) {
            if(set.contains(a[i])){
                continue;
            }
            set.add(a[i]);
        }
        ArrayList<Integer> ans = new ArrayList<>(set);
//        System.out.println(ans);
        for (int i = ans.size() - 1;  i >= 0; i--) {
            System.out.print(ans.get(i) + " ");
        }
    }
}

第二题

package two;

import java.util.LinkedList;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        LinkedList<Integer> list = new LinkedList<>();
        int n = sc.nextInt();
        for (int i = 0; i < n; i++) {
            list.addLast(sc.nextInt());
        }
        while (!list.isEmpty()){
            Integer first = list.removeFirst();
            System.out.print(first + " ");
            if(list.isEmpty()){
                break;
            }
            list.addLast(list.removeFirst());
        }
    }
}

第三题

思路:两个 set,一个保存起点能到的点,一个人保存终点能到的点。然后取交集,若交集不为空,求组合数 Cn2,若交集为空答案就是两个 set.size() 的乘积

package three;

import java.util.*;
/*
3 2 1 3
1 2
2 3
 */
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();
        int s = sc.nextInt();
        int t = sc.nextInt();
        HashMap<Integer, LinkedList<Integer>> graph = new HashMap<>();
        for (int i = 0; i < m; i++) {
            int x = sc.nextInt();
            int y = sc.nextInt();
            graph.putIfAbsent(x, new LinkedList<>());
            graph.get(x).add(y);
            graph.putIfAbsent(y, new LinkedList<>());
            graph.get(y).add(x);
        }
        HashSet<Integer> start = new HashSet<>();
        
        HashSet<Integer> end = new HashSet<>();
        
        HashSet<Integer> visit = new HashSet<>();
        ArrayDeque<Integer> queue = new ArrayDeque<>();
        start.add(s);
        queue.add(s);
        while (!queue.isEmpty()) {
            Integer head = queue.pollFirst();
            visit.add(head);
            start.add(head);
            LinkedList<Integer> nexts = graph.get(head);
            if(nexts== null){
                break;
            }
            for (Integer next : nexts) {
                if (visit.contains(next)) {
                    continue;
                }
                queue.addLast(next);
                visit.add(next);
            }

        }
        queue = new ArrayDeque<>();
        visit = new HashSet<>();
        end.add(t);
        queue.add(t);
        while (!queue.isEmpty()) {
            Integer head = queue.pollFirst();
            visit.add(head);
            end.add(head);
            LinkedList<Integer> nexts = graph.get(head);
            if(nexts== null){
                break;
            }
            for (Integer next : nexts) {
                if (visit.contains(next)) {
                    continue;
                }
                queue.addLast(next);
                visit.add(next);
            }

        }
//        System.out.println(start);
//        System.out.println(end);
        HashSet<Integer> tmp = new HashSet<>(start);
        boolean flag = tmp.retainAll(end);
        int ans = 0;
        if(!flag){
            //组合数Cn2
            ans = n * (n - 1) / 2;

        }else {
            ans = start.size() * end.size();
        }
        System.out.println(ans);

    }
}

#微众银行#
全部评论

相关推荐

02-17 20:43
西北大学 Java
在做测评的猫头鹰很紧张:他问你,你问deep seek
点赞 评论 收藏
分享
评论
1
6
分享

创作者周榜

更多
牛客网
牛客企业服务