首页 > 20200815 美团 java
头像
池凹
编辑于 2020-08-15 21:41
+ 关注

20200815 美团 java

不保证ac,仅一和三

第一题

package meituan;

import java.util.ArrayList;
import java.util.Scanner;
public class Main1 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        scanner.close();

        ArrayList arrayList = new ArrayList();

        for(int i=1; i<n ;i++){
            int finalI = i;
            int fourFinalI = 4*i;
            if(fourFinalI > n) break;
            if(fun(finalI, fourFinalI)) arrayList.add(finalI);
        }
        System.out.println(arrayList.size());

        for (int i = 0; i < arrayList.size(); i++) {
            int fourI = 4*Integer.valueOf((Integer) arrayList.get(i));
            System.out.println(arrayList.get(i)+" "+fourI);
        }
    }
    static boolean fun(int finalI, int fourFinalI){
        int tmp=0;
        while (true){
            if(finalI / 10 > 0){
                tmp = tmp*10 + finalI % 10;
            } else {
                tmp = tmp*10 + finalI;
                break;
            }
            finalI = finalI / 10;
        }
        return tmp == fourFinalI;
    }
}

第三题

package meituan;

import java.util.*;

public class Main4 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int m = scanner.nextInt();

        Map<Integer, Set<Integer>> map = new HashMap<>();

        // 初始化Map
        for (int i = 1; i < n+1; i++) {
            Set<Integer> set = new HashSet<>();
            set.add(i);
            map.put(i,set);
        }

        // 填充map
        for (int i = 0; i < m; i++) {
            int a = scanner.nextInt();
            int b = scanner.nextInt();
            map.get(a).add(b);
            map.get(b).add(a);
        }
        scanner.close();

        // body
        int[] nums = new int[n+1];
        for (int i = 1; i < n+1; i++) {
            if(nums[i]!=0) continue;
            // mark
            int finalI = i;
            mark(finalI, i, nums, map);
        }

        int temp = 1;
        for (int j = 1; j < n+1; j++) {
            if(nums[j]!=temp){
                System.out.println();
                temp = nums[j];
            }
            System.out.print(j +" ");
        }
    }

    private static void mark(int finalI, int i, int[] nums, Map<Integer, Set<Integer>> map) {
        for (Integer temp : map.get(i)) {
            if(nums[temp]==finalI) continue;
            nums[temp] = finalI;
            mark(finalI, temp, nums, map);
        }
    }
}

全部评论

(1) 回帖
加载中...
话题 回帖

推荐话题

相关热帖

近期热帖

历年真题 真题热练榜 24小时
技术(软件)/信息技术类
查看全部

近期精华帖

热门推荐