200 question plan (6)

content

1. Fibonacci sequence

2. Don't Two


1. Fibonacci sequence

Fibonacci Number Sequence_Nioke Question Ba_Niuke.com [Niu Ke Question Ba] collects high-frequency school recruiting interview questions of various companies, with official problem solutions, and conducts online practice for Baidu, Alibaba, Tencent, Netease and other famous Internet companies. Experts discuss classic test questions together and comprehensively improve your technical ability https://www.nowcoder.com/practice/18ecd0ecf5ef4fe9ba3f17f8d00d2d66?tpId=85&&tqId=29846&rp=1&ru=/activity/oj&qru=/ta/2017test/question-ranking

①Example of the topic:

②Method analysis:

a. We can find that in a range, by changing f3, f2, f1, the Fibonacci number has always been in these three numbers

b. After understanding the meaning of the question clearly, we can know that when a number is given, there are two ways to turn it into a Fibonacci number, one is to decrease to the left, the second is to increase to the right, and The minimum number of steps required by the question is obviously to take the smaller of the two.

for example:

 c. We need to judge whether the input n=Fibonacci number belongs to left or right. We can easily know that it belongs to right through the following code (think about it here, it is easy to understand)

 Code:

import java.util.*;
public class Main{
    public static void main(String[]args){
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        int f1=0;
        int f2=1;
        while(n>f2){
           int f3=f2+f1;
            f1=f2;
            f2=f3;
        }//当此处循环退出的时候,n<=f2,这个时候,就说明f2就是右边的项,而F1就是左边的一项
        int min=Math.min(n-f1,f2-n);
        System.out.println(min);
    }
}

2. Don't Two

Don't be two_Nioke Question Ba_Niuke.com [Nioke Question Ba] collects high-frequency school recruiting interview questions of various enterprises, equipped with official problem solutions, and conducts online practice for Baidu, Ali, Tencent, Netease and other famous Internet companies. Experts discuss classic test questions together and improve your technical ability in an all-round way https://www.nowcoder.com/practice/1183548cd48446b38da501e58d5944eb?tpId=85&&tqId=29840&rp=1&ru=/activity/oj&qru=/ta/2017test/question-ranking

①Example of the topic:

②Method analysis:

a. From the meaning of the question, we can first know that this is a two-dimensional array

b. Then we convert the square root into a square:
((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2))=4
and only the following 5 kinds of equations can be satisfied Cases
0+4=4
1+3=4
2+2=4
3+1=4
4+0=4
And since every number is an integer, we rule out 3 cases, leaving only
0+4= 4
4+0=4 These two cases
are when x1=x2, y1=y2+2;
when y1=y2, there is x1=x2+2;

This means that when a cake is placed somewhere, it cannot be placed in these two positions.

c. We initially set the value of the entire array to 0, and then count each cake with a count, and set it to 1 where the cake cannot be placed

Code:

import java.util.*;
public class Main{
public static void main(String[]args){
 Scanner sc=new Scanner(System.in);
    int W=sc.nextInt();
    int H=sc.nextInt();
    int count=0;
    //把二维数组所有的值的大小置为0,当它能存放蛋糕时,count++;不能放蛋糕的位置就置为1
    int [][]tmp=new int[W][H];
    for(int i=0;i<W;i++){
        for(int j=0;j<H;j++){
            if(tmp[i][j]==0){
                count++;
         //当此处已经放了蛋糕,那么距离它的欧几里得距离就不能超过2
            //情况①:同i;j+2,判断j是否越界
                if(j+2<H){
                    tmp[i][j+2]=1;
                }
                //情况②:同j,i+2;判断i是否越界
                if(i+2<W){
                    tmp[i+2][j]=1;
                }
            }
        }
    }
    System.out.println(count);
}
}

Guess you like

Origin blog.csdn.net/weixin_58850105/article/details/123992753