栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java筛子问题

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java筛子问题

Java筛子问题
灵感来源:原神 稻妻 筛子机关

筛子问题:
1. 有四个筛子,每个筛子有四个面,每一个筛子只有一个面有图案
2. 按顺时针转,每次旋转只能旋转90度
3. 第一个筛子旋转,第二个筛子也旋转;
4. 第二个筛子旋转,第一个筛子和第三个筛子也旋转;
5. 第三个筛子旋转,第二个筛子和第四个筛子也旋转;
6. 第四个筛子旋转,第三个筛子也旋转;
问:计算出每个筛子转多少次,使每个筛子有图案的在正面。

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
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/781177.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号