网易游戏互娱 笔试题2021.8.7 Java版

1、身份证

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

/**
 * @author Jefft
 * @date 2021/8/7 19:46
 */
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        sc.nextLine();

        String[] strs = new String[n];

        for (int i = 0;i < n;i++) {
            strs[i] = sc.nextLine();
        }

        Map<Integer, Integer> numMap = new HashMap<>();
        numMap.put(0,7);
        numMap.put(1,9);
        numMap.put(2,10);
        numMap.put(3,5);
        numMap.put(4,8);
        numMap.put(5,4);
        numMap.put(6,2);
        numMap.put(7,1);
        numMap.put(8,6);
        numMap.put(9,3);
        numMap.put(10,7);
        numMap.put(11,9);
        numMap.put(12,10);
        numMap.put(13,5);
        numMap.put(14,8);
        numMap.put(15,4);
        numMap.put(16,2);

        Map<String, Integer> map2 = new HashMap<>();
        map2.put("0",1);
        map2.put("1",0);
        map2.put("2",10);
        map2.put("3",9);
        map2.put("4",8);
        map2.put("5",7);
        map2.put("6",6);
        map2.put("7",5);
        map2.put("8",4);
        map2.put("9",3);
        map2.put("X",2);



        //遍历处理
        for (String s:strs) {
            int result = 0;
            //未知个数
            Map<Integer, Integer> map = new HashMap<>();
            int index = 0;//索引

            int num = 0;
            for (int i = 0;i < 17;i++) {
                if (s.charAt(i) == '*') {
                    map.put(index++, numMap.get(i));
                } else {
                    num += numMap.get(i) * Integer.parseInt(s.charAt(i)+"");
                }
            }

            //有多少位未知数,1 or 2 or 3
            int size = map.size();

            int xxx = map2.get(s.charAt(17)+"");
            if (size == 1) {
                for (int x = 0;x < 10;x++) {
                    if ((x * map.get(0) + num) % 11 == xxx) {
                        result++;
                    }
                }
            } else if (size == 2) {
                for (int x = 0;x < 10;x++) {
                    for (int y = 0;y < 10;y++) {
                        if ((x * map.get(0) + y * map.get(1) + num) % 11 == xxx) {
                            result++;
                        }
                    }
                }
            } else if (size == 3) {
                for (int x = 0;x < 10;x++) {
                    for (int y = 0;y < 10;y++) {
                        for (int z = 0;z < 10;z++) {
                            if ((x * map.get(0) + y * map.get(1) + z * map.get(2) + num) % 11 == xxx) {
                                result++;
                            }
                        }
                    }
                }
            }
            //输出结果
            System.out.println(result);
        }

    }
}

2、比赛排名

import java.util.*;

/**
 * @author Jefft
 * @date 2021/8/7 20:41
 */
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        //t组数据
        int t = sc.nextInt();

        for (int tt = 0;tt < t;tt++) {
            //初始化
            int n = sc.nextInt();
            int m = sc.nextInt();
            int[] nums = new int[n];
            boolean ifCan = false;

            //处理数据
            List<int[]> list = new ArrayList<>();
            for (int mm = 0;mm < m;mm++) {
                int c = sc.nextInt();
                int[] cNum = new int[c];
                for (int cc = 0;cc < c;cc++) {
                    cNum[cc] = sc.nextInt();
                }
                list.add(cNum);
            }
            //对list进行处理
            Map<Integer,Node> map = new HashMap<>();


            Node pre = new Node(0);
            for (int i = 0;i < list.size();i++) {
                Node cur = pre;
                int[] ints = list.get(i);
                for (int j = 0;j < ints.length;j++) {
                    if (j != ints.length - 1) {
                        if (!map.containsKey(ints[j])) {
                            //当前加进去
                            map.put(ints[j], new Node(ints[j]));
                        }
                        if (!map.containsKey(ints[j + 1])) {
                            List<Node> nodes = new ArrayList<>();
                            nodes.add(new Node(ints[j + 1]));
                            map.get(ints[j]).next = nodes;
                        } else {
                            List<Node> nodes = new ArrayList<>();
                            nodes.add(map.get(ints[j + 1]));
                            map.get(ints[j]).next = nodes;
                        }
                    }
                    else {
                        if (!map.containsKey(ints[j])) {
                            //当前加进去
                            map.put(ints[j], new Node(ints[j]));
                        }
                    }
                }
            }

            if (map.size() == n) {
                //遍历每个节点
                Set<Integer> keys = map.keySet();

                for (Integer key : keys) {
                    Node node = map.get(key);
                    if (getNum(map, node, n, 1)) {
                        ifCan = true;
                        break;
                    }
                }
            }

            //结果
            if (ifCan) {
                for (int i = 0;i < nums.length;i++) {
                    if (i == nums.length - 1) {
                        int iii = i + 1;
                        System.out.println(iii);
                    } else {
                        int ii = i + 1;
                        System.out.print(ii + " ");
                    }
                }
                System.out.println();
            } else {
                System.out.println("NO");
            }
        }

    }

	//如果有某条路径能够覆盖所有节点,则为true
    public static boolean getNum(Map<Integer,Node> map, Node node, int n, int cur) {
        if (node == null) {
            return false;
        }
        if (cur == n) {
            return true;
        }
        if (node.next == null) {
            return false;
        }
        List<Node> next = node.next;

        for (Node nod : next) {
            if (getNum(map, map.get(nod.val), n, cur+1)) {
                return true;
            }
        }
        return false;
    }
}



class Node {
    int val;
    List<Node> next;

    public Node(int val) {
        this.val = val;
    }
}

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值