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

骰子问题旋转(java)

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

骰子问题旋转(java)

问题描述

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

【输入形式】输入只包含LRFBAC的字母序列,最大长度为50,可重复

【输出形式】输出经过一系列操作后的序列 注:按左右前后上下顺序输出

【样例输入】LB

【样例输出】5 6 1 2 3 4

【样例说明】
【评分标准】

题解思路

这是一道模拟题目,需要一定想象能力,读者可以自己画一个骰子来进行代码编写

代码
package homework;

import java.util.ArrayList;
import java.util.Scanner;

public class DiceRotate {
    private static ArrayList dice = new ArrayList<>();
    public static void main(String[]args){

        Scanner scanner = new Scanner(System.in);
        while(scanner.hasNext()){
            for(int i = 1 ; i <= 6 ; i ++){
                dice.add(i);
            }
            String operations = scanner.next();
            for(int i = 0 ; i < operations.length() ; i ++ ){
                operate(operations.charAt(i));
            }
            //System.out.println(dice);
            for(int i = 0 ; i < 6 ; i ++){
                System.out.print(dice.get(i) + " ");
            }
        }

    }
    public static void operate(char x){
        switch (x){
        //六种情况 l r f b a c
            case 'L':{
                int l = dice.get(0);
                int r = dice.get(1);
                int u = dice.get(4);
                int d = dice.get(5);
                dice.set(0,u);
                dice.set(1,d);
                dice.set(4,r);
                dice.set(5,l);
                break;
            }
            case 'R': {
                int l = dice.get(0);
                int r = dice.get(1);
                int u = dice.get(4);
                int d = dice.get(5);
                dice.set(0, d);
                dice.set(1, u);
                dice.set(4, l);
                dice.set(5, r);
                break;
            }
            case 'F':{
                int f = dice.get(2);
                int b = dice.get(3);
                int u = dice.get(4);
                int d = dice.get(5);
                dice.set(2,u);
                dice.set(3,d);
                dice.set(4,b);
                dice.set(5,f);
                break;
            }
            case 'B':{
                int f = dice.get(2);
                int b = dice.get(3);
                int u = dice.get(4);
                int d = dice.get(5);
                dice.set(2,d);
                dice.set(3,u);
                dice.set(4,f);
                dice.set(5,b);
                break;
            }
            case 'A':{
                int l = dice.get(0);
                int r = dice.get(1);
                int f = dice.get(2);
                int b = dice.get(3);
                dice.set(0,b);
                dice.set(1,f);
                dice.set(2,l);
                dice.set(3,r);
                break;
            }
            case 'C':{
                int l = dice.get(0);
                int r = dice.get(1);
                int f = dice.get(2);
                int b = dice.get(3);
                dice.set(0,f);
                dice.set(1,b);
                dice.set(2,r);
                dice.set(3,l);
                break;
            }
        }
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/345023.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

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

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