Blue Bridge Cup: Practice (Question Analysis)

topic

[Problem description]
   Xiaoming practices qigong every day, and the most important item in qigong practice is the plum blossom pile.
   Xiao Ming’s plum blossom piles are arranged in n rows and m columns. The distance between two adjacent rows is 1, and the distance between two adjacent columns is also 1.
   Xiaoming is standing on the first row and the first column, and he wants to go to the nth row and the mth column. Xiao Ming has been practicing for a while, and he can now move a distance of no more than d (straight line distance) in one step.
   Xiao Ming wanted to know how many steps he would need to move to the target without falling off the plum blossom pile.
[Input format]
   The first line of input contains two integers n, m, which represent the number of rows and columns of quincunx piles.
   The second line contains a real number d (contains one decimal at most), indicating the distance Xiao Ming can move in one step.
[Output format]
   Output an integer, indicating how many steps Xiao Ming can reach the goal at least.
[Sample input]
3 4
1.5
[Sample output]
   3
[Evaluation use case scale and conventions]
   For 30% of evaluation use cases, 2 <= n, m <= 20, 1 <= d <= 20.
   For 60% of the evaluation use cases, 2 <= n, m <= 100, 1 <= d <= 100.
   For all evaluation cases, 2 <= n, m <= 1000, 1 <= d <= 100.

Code

import java.util.Scanner;

public class Main {
    
    //蓝桥杯要求class命名为Main,且无package
    public static void main(String[] args) {
    
    
        Scanner scanner = new Scanner(System.in);
        int n=scanner.nextInt();
        int m=scanner.nextInt();
        float d = scanner.nextFloat();
        float distance = (float)Math.sqrt(((n-1)*(n-1)+(m-1)*(m-1)));//计算距离
        int step = (int) Math.ceil(distance/d);//向上取整
        System.out.println(step);
    }
}


Problem analysis

   According to the style input and output of the question, it can be calculated that the calculated distance is the shortest distance between two points. For example, n=3, m=4, and the distance distance is approximately 3.6 instead of 5.

Guess you like

Origin blog.csdn.net/qq_47168235/article/details/108917774