题目
如何评价暴力做法想了一个小时都做不出来,我是FW
思路分析
抽象题意
题目的意思是给定两串密码,要求求出一串密码,在轮盘上每一位都至少和其中一串的每一位相差不小于2个单位
具体实现
由于数据量不是很大,N在100以内,三位数最多也就1000000,所以可以三层for暴力枚举
现在这里最难的就在于如何将密码序列中每一位至少和其中一串的每一位相差不小于2个单位具体实现刻画出来
一开始是想着枚举五种可能(-2,-1,0-1-2),但是其实正确的做法应该是:
针对轮盘上的两个数a,b,他们如果来回相邻2个单位以内,数学角度刻画应该是:|a-b|<=2 || |a-b|>=n-2
代码实现
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
* @author HuangSir
* @date 2022-05-01 20:11
*/
public class Main
{
static int[] dx = {-2, -1, 0, 1, 2};
static int[] john = new int[3];
static int[] pro = new int[3];
static int res = 0;
public static void main(String[] args)
throws IOException
{
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(reader.readLine());
String[] s1 = reader.readLine().split(" ");
String[] s2 = reader.readLine().split(" ");
for (int i = 0; i < 3; i++) {
john[i] = Integer.parseInt(s1[i]);
pro[i] = Integer.parseInt(s2[i]);
}
for (int i = 1; i <=n; i++) {
for (int j = 1; j <=n; j++) {
for (int k = 1; k <=n; k++) {
check(i,j,k,n);
}
}
}
System.out.println(res);
}
private static void check(int i, int j, int k, int n)
{
if (isClose(i,john[0],n) && isClose(j,john[1],n ) && isClose(k,john[2],n)
||isClose(i,pro[0],n) && isClose(j,pro[1],n ) && isClose(k,pro[2],n)){
res++;
}
}
private static boolean isClose(int a,int b,int n){
return Math.abs(a-b)<=2 || Math.abs(a-b)>=n-2;
}
}