2022-4-16 荣耀笔试

第一题 80% 问题在哪?
package org.rongyao;

import java.util.Scanner;

public class Main {
    static int[][] day={
            {0,31,28,31,30,31,30,31,31,30,31,30,31},{0,31,29,31,30,31,30,31,31,30,31,30,31}
    };
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        String[] s=sc.nextLine().split(" ");
        int year=Integer.parseInt(s[0]);int month=Integer.parseInt(s[1]);
        int week=Integer.parseInt(s[2]);int we=Integer.parseInt(s[3]);

        if(week>6){
            System.out.println(0);
            return ;
        }

        int sum=0;
        for(int i=2000;i<year;i++){
            if(isLuna(i)){
                sum+=366;
            }else{
                sum+=365;
            }
        }
        int flag=isLuna(year)? 1:0;
        for(int i=1;i<month;i++){
            sum+=day[flag][i];
        }

        int d=(sum-2)%7;
        int dd=(sum+day[flag][month]-2)%7;
        //28 4 3
        System.out.println("flag"+day[flag][month]);
        System.out.println("d"+d+"==="+"dd"+dd);
        int start=d==0? 1:d+1;
        int end=dd==0? 7:dd;
        int curweek=1;
        int ss=day[flag][month];
        ss-=(7-start+1);
        curweek+=ss/7;
        if(ss%7!=0){
            curweek+=1;
        }

        if((week==1&&we<start)||(week==curweek&&we>end)){
            System.out.println(0);
            return;
        }
        System.out.println(start+"===="+end);
        int res=0;
        if(week==1){
            res=we-start+1;
        }else{
            for(int i=1;i<=week;i++){
                if(i==1){
                    res+=(7-start+1);
                }else if(i==week){
                    res+=we;
                }else{
                    res+=7;
                }
            }

        }
        System.out.println(s[0]+"-"+s[1]+"-"+String.valueOf(res));
    }
    public static boolean isLuna(int year){
        if((year%4==0&&year%100!=0)||year%400==0){
            return true;
        }
        return false;
    }
}
第二题  编译器

第三题 奥特曼打怪兽 50%
package org.rongyao;

import java.util.*;

public class Solution {
    static int[] dx={0,1,0,-1};
    static int[] dy={1,0,-1,0};
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        List<String> nums=new ArrayList<>();
        String s1 = sc.nextLine();
        nums.add(s1);
        String[] s2 = s1.split(" ");

        int m=s2.length;

        for(int i=0;i<m-1;i++){
            nums.add(sc.nextLine());
        }

        PriorityQueue<int[]> queue=new PriorityQueue<>((a,b)->a[0]-b[0]);
        int[][] matrix=new int[m][m];
        for(int i=0;i<m;i++){
            String[] s=nums.get(i).split(" ");
            for(int j=0;j<m;j++){
                matrix[i][j]=Integer.parseInt(s[j]);
                if(matrix[i][j]>=2){
                    queue.offer(new int[]{matrix[i][j],i,j});
                }
            }
        }

//        for(int i=0;i<m;i++){
//            for(int j=0;j<m;j++){
//                System.out.print(matrix[i][j]+" ");
//            }
//            System.out.println();
//        }

        int res=0;int re=0;
        int startx=0;int starty=0;
        while(!queue.isEmpty()){
            int[] node=queue.poll();
            re=bfs(matrix,startx,starty,node[1],node[2]);
//            System.out.println("startx"+startx+"=="+starty+"=="+node[1]+"=="+node[2]);
//            System.out.println("re"+re);
            if(re==-1){
                break;
            }else{
                res+=re;
            }
            startx=node[1];starty=node[2];
        }

        if(re==-1){
            System.out.println(-1);
        }else{
            System.out.println(res);
        }

    }
    public static int bfs(int[][] matrix,int start,int end,int targetx,int targety){
        int m=matrix.length;
        boolean[][] vis=new boolean[m][m];
        Queue<int[]> queue=new LinkedList<>();
        queue.offer(new int[]{start,end});
        vis[start][end]=true;
        int cnt=0;
        while (!queue.isEmpty()) {
            int size=queue.size();
            for(int i=0;i<size;i++){
                int[] node = queue.poll();
                if(matrix[node[0]][node[1]]>=2){
                    continue;
                }
                if(node[0]==targetx&&node[1]==targety){
                    matrix[targetx][targety]=1;
                    return cnt;
                }
                for(int k=0;k<4;k++){
                    int next_x=node[0]+dx[k];
                    int next_y=node[1]+dy[k];
                    if(next_x<0||next_x>=m||next_y<0||next_y>=m||matrix[next_x][next_y]==0||vis[next_x][next_y]){
                        continue;
                    }
                    queue.offer(new int[]{next_x,next_y});
                    vis[next_x][next_y]=true;
                }
            }
            cnt++;
        }
        return -1;
    }
}


#荣耀笔试#
全部评论
第一题个位数res前补0
点赞 回复 分享
发布于 2022-04-16 21:21
啊?,醉了
点赞 回复 分享
发布于 2022-04-17 09:58

相关推荐

赏个offer求你了:友塔HR还专门加我告诉我初筛不通过😂
点赞 评论 收藏
分享
3 5 评论
分享
牛客网
牛客企业服务