华为OD机试:转骰子

标题:转骰子 | 时间限制:1 秒 | 内存限制:262144K | 语言限制:不限

骰子是一个立方体,每个面一个数字,初始为左 1,右 2,前 3(观察者方向),后 4,上 5, 下 6,用 123456 表示这个状态,放置到平面上,可以向左翻转(用 L 表示向左翻转 1 次), 可以向右翻转(用 R 表示向右翻转 1 次),可以向前翻转(用 F 表示向前翻转 1 次),可以向 后翻转(用 B 表示向后翻转 1 次),可以逆时针旋转(用 A 表示逆时针旋转 90 度),可以顺 时针旋转(用 C 表示顺时针旋转 90 度),现从 123456 这个初始状态开始,根据输入的动作 序列,计算得到最终的状态。

骰子的初始状态和初始状态转动后的状态如图所示

输入描述:

输入一行,为只包含 LRFBAC 的字母序列,最大长度 50,字母可重复

输出描述:

输出最终状态

示例 1

输入 LR

输出 123456

说明

骰子先向左翻转,再向右翻转回来,故还是原来的状态 123456

示例 2

输入 FCR

输出 342156

说明

骰子向前翻转,状态变为 125643,再顺时针旋转,状态变为 651243,最后向右翻转,状态 变为 342156

解题思路:原始状态123456,根据规则分类处理LRFBAC之后的状态,根据输入调用对应方法即可

public class Test3 {

    public static void main(String[] args) {
        String str = "FCR";

        int[] dice = {1,2,3,4,5,6};
        for (int i=0;i<str.length();i++){
            if (str.charAt(i) == 'L'){
                dice = turnL(dice);
            }else if (str.charAt(i) == 'R'){
                dice = turnR(dice);
            }else if (str.charAt(i) == 'F'){
                dice = turnF(dice);
            }else if (str.charAt(i) == 'B'){
                dice = turnB(dice);
            }else if (str.charAt(i) == 'A'){
                dice = turnA(dice);
            }else if (str.charAt(i) == 'C'){
                dice = turnC(dice);
            }
        }

        for (int i=0;i<dice.length;i++){
            System.out.print(dice[i]);
        }
    }

    //123456->563421
    public static int[] turnL(int[] dice){
        int[] trun = new int[6];
        trun[0] = dice[4];
        trun[1] = dice[5];
        trun[2] = dice[2];
        trun[3] = dice[3];
        trun[4] = dice[1];
        trun[5] = dice[0];
        return trun;
    }
    //123456->653412
    public static int[] turnR(int[] dice){
        int[] trun = new int[6];
        trun[0] = dice[5];
        trun[1] = dice[4];
        trun[2] = dice[2];
        trun[3] = dice[3];
        trun[4] = dice[0];
        trun[5] = dice[1];
        return trun;
    }
    //123456->125643
    public static int[] turnF(int[] dice){
        int[] trun = new int[6];
        trun[0] = dice[0];
        trun[1] = dice[1];
        trun[2] = dice[4];
        trun[3] = dice[5];
        trun[4] = dice[3];
        trun[5] = dice[2];
        return trun;
    }
    //123456->126534
    public static int[] turnB(int[] dice){
        int[] trun = new int[6];
        trun[0] = dice[0];
        trun[1] = dice[1];
        trun[2] = dice[5];
        trun[3] = dice[4];
        trun[4] = dice[2];
        trun[5] = dice[3];
        return trun;
    }
    //123456->431256
    public static int[] turnA(int[] dice){
        int[] trun = new int[6];
        trun[0] = dice[3];
        trun[1] = dice[2];
        trun[2] = dice[0];
        trun[3] = dice[1];
        trun[4] = dice[4];
        trun[5] = dice[5];
        return trun;
    }
    //123456->342156
    public static int[] turnC(int[] dice){
        int[] trun = new int[6];
        trun[0] = dice[2];
        trun[1] = dice[3];
        trun[2] = dice[1];
        trun[3] = dice[0];
        trun[4] = dice[4];
        trun[5] = dice[5];
        return trun;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值