牛客--不要二(大厂面试题并没有那么难)

📜个人简介

⭐️个人主页:摸鱼の文酱博客主页🙋‍♂️
🍑博客领域:java编程基础,mysql
🍅写作风格:干货,干货,还是tmd的干货
🌸精选专栏:【Java】【mysql】 【算法刷题笔记】
🎯博主的码云gitee,平常博主写的程序代码都在里面。
🚀支持博主:点赞👍、收藏⭐、留言💬
🍭作者水平很有限,如果发现错误,一定要及时告知作者哦!感谢感谢!

📃不要二

🎯1.原题链接

不要二

🎯2.题目要求

  二货小易有一个W*H的网格盒子,网格的行编号为0-H-1,网格的列编号为0-W-1。每个格子至多可以放一块蛋糕,任意两块蛋糕的欧几里得距离不能等于2。
  对于两个格子坐标(x1,y1),(x2,y2)的欧几里得距离为: ( ( x 1 − x 2 ) ∗ ( x 1 − x 2 ) + ( y 1 − y 2 ) ∗ ( y 1 − y 2 ) ) ( (x1-x2) * (x1-x2) + (y1-y2) * (y1-y2) ) ((x1x2)(x1x2)+(y1y2)(y1y2)) 的算术平方根
  小易想知道最多可以放多少块蛋糕在网格盒子里。


  样例输入: 3 2

  样例输出: 4

🎯3.基础框架

java版本的基础框架代码如下:

import java.util.*
public class Main(){
	
}

🎯4.解题思路

思路一:
在这里插入图片描述

思路二(贪心):

  1.4个一组蛋糕方阵,相邻4个一组的空白方阵,交替摆放即可
  2.首先,左上角一定是可以放置蛋糕的
  3.后因为题目要求任意两个蛋糕之间的欧几里得距离不能为2,而对于整数坐标位置来说,欧几里得距离为2的情况下只有垂直或者水平距离为2,像斜对角的距离不可能为2的
  4.就此先简单推断第一行的蛋糕位置,假设1为可放置,0为不可放置,则第一行应该为:110011001100…,因此可以看出是以4为一个周期,而纵方向也同样如此:110011001100…因此需要对横纵坐标依次遍历通过周期4进行判断

思路三:反向思维

  1.定义一个二维数组,resize开空间并初始化,每个位置初始化为0
  2.如果在a[i][j]位置放蛋糕,则可以标记处a[i][j+2]a[i+2][j]位置不能放蛋糕,
  3.遍历一遍二维数组,标记处不能放蛋糕的位置,统计也就统计出了当蛋糕的位置数。

🎯5.完整代码

思路一代码(暴力):

//暴力法
import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNext()) {
            int length = in.nextInt();
            int width = in.nextInt();
            int[][] array = new int[length][width];
            for (int i = 0; i < length; i++) {
                for (int j = 0; j < width; j++) {
                    if ((i % 4 == 0 || i % 4 == 1) 
                            && (j % 4 == 0 || j % 4 == 1) )
                        array[i][j] = 1;
                    if ((i % 4 == 2 || i % 4 == 3) 
                            && (j % 4 == 2 || j % 4 == 3) )
                        array[i][j] = 1;
                }
            }
            int count = 0;
            for (int i = 0; i < length; i++) {
                for (int j = 0; j < width; j++)
                    if (array[i][j] == 1) count++;
            }
            System.out.println(count);
        }
    }
}
---------------------------------------------------------------------------
//最优解
import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNext()) {
            int length = in.nextInt();
            int width = in.nextInt();
            int res = 0;
            if (length % 4 == 0 || width % 4 == 0)
                res = length * width / 2;
            else if (length % 4 == 2 && width % 4 == 2)
                res = length * width / 2 + 2;
            else
                res = length * width / 2 + 1;
            System.out.println(res);
        }
    }
}

import java.util.Scanner;

public class Main {
	
	//不要2
	public static void main(String[] args) {
		
		Scanner in = new Scanner(System.in);
		int row = in.nextInt();
		int col = in.nextInt();
		
		int[][] grid = new int[row][col];
		//横向判断蛋糕位置
		for(int i=0; i<row; i++){
			if(i%4 == 0 || i%4 == 1){
				for(int j=0; j<col; j++){
					if(j%4 == 0 || j%4 == 1){
						grid[i][j] = 1;
					}
				}
			}
			//纵向判断蛋糕位置
			else{
				for(int j=0; j<col; j++){
					if(j%4 ==2 || j%4 == 3){
						grid[i][j] = 1;
					}
				}
			}
		}
		int sum = 0;
		for(int i=0; i<row; i++){
			for(int j=0; j<col; j++){
				if(grid[i][j] == 1){
					sum++;
				}
			}
		}
		System.out.println(sum);
	}
}

思路三代码:

import java.util.*;
public class Main {
public static void main(String[] args) {
	Scanner in = new Scanner(System.in);
	int col = in.nextInt();
	int row = in.nextInt();
	int count = 0;
	int[][] array = new int[row][col];
	for(int i = 0;i < row;i++) {
		for(int j = 0;j < col;j++) {
			if(array[i][j] == 0) {
				count++;
				if(i+2 < row) {
					array[i+2][j] = 1;
				} 
				if(j+2 < col) {
					array[i][j+2] = 1;
				}
			}
		}
	} 
	System.out.println(count);
	}
}

🎯6.涉及算法&总结

根据题给信息找到规律,然后模拟即可
据说这道题是网易的一道面试题,与它类似的面试题还有很多
在这里插入图片描述
可以看得出来,公司对于这种类似的题考察也不少,并且难度不一定很大,只要多做,多总结,就有机会进大厂!!!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值