Java筛子问题
Java筛子问题
灵感来源:原神 稻妻 筛子机关
筛子问题:
1. 有四个筛子,每个筛子有四个面,每一个筛子只有一个面有图案
2. 按顺时针转,每次旋转只能旋转90度
3. 第一个筛子旋转,第二个筛子也旋转;
4. 第二个筛子旋转,第一个筛子和第三个筛子也旋转;
5. 第三个筛子旋转,第二个筛子和第四个筛子也旋转;
6. 第四个筛子旋转,第三个筛子也旋转;
问:计算出每个筛子转多少次,使每个筛子有图案的在正面。
/*
筛子问题
有四个筛子,每个筛子有四个面,每一个筛子只有一个面有图案
按顺时针转,每次旋转只能旋转90度
第一个筛子旋转,第二个筛子也旋转;
第二个筛子旋转,第一个筛子和第三个筛子也旋转;
第三个筛子旋转,第二个筛子和第四个筛子也旋转;
第四个筛子旋转,第三个筛子也旋转;
计算出每个筛子转多少次,使每个筛子有图案的在正面。
*/
public class SifterMain {
static boolean isf=false;
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (!isf) {
System.out.println("---------------------");
System.out.println("输入不是(前后左右)则退出");
System.out.println("---------------------");
System.out.println("输入第一个筛子有图案的方向(前后左右):");
String one = scanner.next();
System.out.println("输入第二个筛子有图案的方向(前后左右):");
String two = scanner.next();
System.out.println("输入第三个筛子有图案的方向(前后左右):");
String three = scanner.next();
System.out.println("输入第四个筛子有图案的方向(前后左右):");
String four = scanner.next();
initSifter(one,two,three,four);
}
}
public static void initSifter(String one,String two,String three,String four){
int a,b,c,d;
int sum;//c+b
int one1 = judge(one);
int two1 = judge(two);
int three1 = judge(three);
int four1 = judge(four);
if (one1<0 || two1<0 || three1<0 || four1<0){
System.out.println("输入错误!");
isf=true;
}else {
c=-two1-(-one1);
b=-three1-(-four1);
sum=(-two1-(-one1))+(-three1-(-four1));
a=-two1-(sum);
d=-three1-(sum);
if (a<0 || b<0 || c<0 || d<0){
a=a+4;
b=b+4;
c=c+4;
d=d+4;
System.out.println("第一个筛子转"+a+"次");
System.out.println("第二个筛子转"+b+"次");
System.out.println("第三个筛子转"+c+"次");
System.out.println("第四个筛子转"+d+"次");
}else {
System.out.println("第一个筛子转"+a+"次");
System.out.println("第二个筛子转"+b+"次");
System.out.println("第三个筛子转"+c+"次");
System.out.println("第四个筛子转"+d+"次");
}
}
}
//判断
public static int judge(String s){
if (s.equals("前")){
return 0;
}else if (s.equals("左")){
return 1;
}else if (s.equals("后")){
return 2;
}else if (s.equals("右")){
return 3;
}else {
return -1;
}
}
}
思路:
设:Δ为4的倍数
|2| |1| |1| |0| |0| |Δ|
|1| |1| |1| |1| |0| |Δ|
+ a | | + b | | + c | | + d | | = | |
|1| |0| |1| |1| |1| |Δ|
|2| |0| |0| |1| |1| |Δ|
--------------------
2+a+b+0+0=Δ
1+a+b+c+0=Δ
1+0+b+c+d=Δ
2+0+0+c+d=Δ
--------------------
a+b=Δ-2
a+b+c=Δ-1
b+c+d=Δ-1
c+d=Δ-2
--------------------
令Δ=0
a=-3,=1,c=1,d=-3
因为有负数,Δ为4的倍数
所有得的值+4
a=1,b=5,c=5,d=1
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!