当前位置:网站首页>Manhattan distance sum - print diamond
Manhattan distance sum - print diamond
2022-07-06 06:08:00 【starnight531】

Manhattan distance and
Food Guide :
Leetcode The column opens , Because the blogger is closed at the end of the period , So only one question per day Try to solve more than one problem , First say the train of thought. , Then the code realizes , Necessary comments will be added Grammar or STL The content will be highlighted at the attention point , Novice friendly Welcome to the blogger's magic tricks column , Detailed explanation of connotation algorithm foundation and code template
Title Description :
Enter an odd number n, Output a by * Composed of n Step solid diamond .
Input format
An odd number n.Output format
Output a by * Composed of n Step solid diamond .Refer to the output sample for specific format .
Data range
1≤n≤99
sample input :
5
sample output :
Title source :https://www.acwing.com/problem/content/729/
Topic analysis :
Law 1 : Manhattan distance and :
Manhattan distance :x / y The maximum value of the direction projection distance
Manhattan distance and :x Direction projection distance + y Direction projection distance
The diamond :
The nature of the circle itself is that the distance from each point to the center of the circle is equal , This distance is under the root sign ((Δx)2+(Δy)2)
In the array , Because of the accuracy , Reduce the distance to Manhattan distance and
The same Manhattan distance and the collection of points form a diamond , The diamond is already very close
But because of its different mathematical expressions , The resulting figure is also a diamond
Algorithm template :
Code implementation :
Law 1 :
- Enter only odd numbers :
import java.util.Scanner;
import java.lang.Math;
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
for(int i=0; i<n; i++){
for(int j=0; j<n; j++){
if (Math.abs(i - n/2)+Math.abs(j - n/2) <= n/2){
System.out.print("*");
}else System.out.print(" ");
}
System.out.println();
}
}
}
- Odd even mixed input :
import java.util.Scanner;
import java.lang.Math;
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = n;
if (n % 2 == 0) m++;
for(int i=0; i<m; i++){
for(int j=0; j<m; j++){
if (Math.abs(i - m/2)+Math.abs(j - m/2) <= m/2){
System.out.print("*");
}else System.out.print(" ");
}
System.out.println();
}
}
}
Be careful :
For odd row diamonds :
Input n It is the number of rows and columns in a diamond , It's diamond in the middle again * Number
The number of lines of the square on the outside of the diamond / The number of columns is odd , Midpoint is (n/2 , n/2),mahattan = max(abs(i - n/2), abs(j - n/2))
For even rows of diamonds :
Input n For the even , The number of diamond rows and columns is n+1, Middle row * The number is also n+1
The number of lines of the square on the outside of the diamond / The number of columns is odd , Midpoint is ((n+1/2), (n+1)/2),mahattan = max(abs(i - (n+1)/2), abs(j - (n+1)/2))
Can we force the use of even Manhattan distance formula to solve ?
The answer is that this question cannot , After all, drawing a diamond can only have odd rows and odd sequences (2*n + 1),
The midpoint compulsorily solved by the even Manhattan distance formula is not the midpoint , Distance is even more wrong .demonstration : Enter the following code 4 The diamond drawn is
import java.util.Scanner;
import java.lang.Math;
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = n;
if (n % 2 == 0) m++;
for(int i=0; i<m; i++){
for(int j=0; j<m; j++){
// Even numbers are wrong to use Manhattan distance formula , The midpoint position and distance are wrong
if(n % 2 == 1){
if (Math.abs(i - n/2)+Math.abs(j - n/2) <= n/2){
System.out.print("*");
}else System.out.print(" ");
}else{
if ((int)Math.abs(i - (n-1)/2.0)+(int)Math.abs(j - (n-1)/2.0) <= n/2){
System.out.print("*");
}else System.out.print(" ");
}
}
System.out.println();
}
}
}

边栏推荐
- Expose the serial fraudster Liu Qing in the currency circle, and default hundreds of millions of Cheng Laolai
- C language bubble sort
- Configuring OSPF GR features for Huawei devices
- The difference and usage between continue and break
- wib3.0 跨越,在跨越(ง •̀_•́)ง
- Amazon Engineer: eight important experiences I learned in my career
- Huawei BFD configuration specification
- Clock in during winter vacation
- Li Chuang EDA learning notes 12: common PCB board layout constraint principles
- Baidu online AI competition - image processing challenge: the 8th program of handwriting erasure
猜你喜欢
![Buuctf-[gxyctf2019] no dolls (xiaoyute detailed explanation)](/img/0a/054b994b29d4c50ede8b23514cf4ee.jpg)
Buuctf-[gxyctf2019] no dolls (xiaoyute detailed explanation)

Hongliao Technology: how to quickly improve Tiktok store

How to use the container reflection method encapsulated by thinkphp5.1 in business code

MPLS test report

Sqlmap tutorial (III) practical skills II

CoDeSys note 2: set coil and reset coil

Significance of unit testing

IP day 16 VLAN MPLS configuration

异常检测方法总结

The latest 2022 review of "graph classification research"
随机推荐
H3C V7 switch configuration IRF
Caused by:org.gradle.api.internal.plugins . PluginApplicationException: Failed to apply plugin
Detailed explanation of BF and KMP
Some easy-to-use tools make your essay style more elegant
How Huawei routers configure static routes
局域网同一个网段通信过程
Idea new UI usage
Zoom through the mouse wheel
Is it difficult for an information system project manager?
【Postman】动态变量(也称Mock函数)
【Postman】Collections配置运行过程
Eigen稀疏矩阵操作
【LeetCode】Day96-第一个唯一字符&赎金信&字母异位词
查询生产订单中某个(些)工作中心对应的标准文本码
C language bubble sort
Clock in during winter vacation
MIT6.s081-2020 Lab2 System Calls
How to recover Huawei router's forgotten password
Expose the serial fraudster Liu Qing in the currency circle, and default hundreds of millions of Cheng Laolai
Practice sharing: how to safely and quickly migrate from CentOS to openeuler