(C卷,200分)- 转骰子(Java & JS & Python)

题目描述

骰子是一个立方体,每个面一个数字,初始为左1,右2,前3(观察者方向),后4,上5,下6,用123456表示这个状态,放置在平面上,

  • 可以向左翻转(用L表示向左翻转1次),
  • 可以向右翻转(用R表示向右翻转1次),
  • 可以向前翻转(用F表示向前翻转1次),
  • 可以向后翻转(用B表示向后翻转1次),
  • 可以逆时针旋转(用A表示逆时针旋转90度),
  • 可以顺时针旋转(用C表示顺时针旋转90度),

现从123456这个初始状态开始,根据输入的动作序列,计算得到最终的状态。

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

输入描述

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

输出描述

输出最终状态

用例

输入 L R
输出 123456
说明
输入 F C R
输出 342156
说明

题目解析

本题感觉就是一道耗时题,考察细心程度的。具体的逻辑反而不是很难。

具体逻辑请看源码。

JavaScript算法源码

/* JavaScript Node ACM模式 控制台输入获取 */
const readline = require("readline");
 
const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});
 
rl.on("line", (line) => {
  const directives = line.split(" ");
  turnDice(directives);
});
 
function turnDice(directives) {
  const dice = new Dice();
 
  directives.forEach((directive) => {
    switch (directive) {
      case "L":
        dice.turnL();
        break;
      case "R":
        dice.turnR();
        break;
      case "F":
        dice.turnF();
        break;
      case "B":
        dice.turnB();
        break;
      case "A":
        dice.turnA();
        break;
      case "C":
        dice.turnC();
        break;
    }
  });
 
  dice.print();
}
 
class Dice {
  constructor() {
    this.left = 1;
    this.right = 2;
    this.front = 3;
    this.back = 4;
    this.top = 5;
    this.bottom = 6;
  }
 
  turnL() {
    // 前后不变,上变左,左变下,下变右,右变上
    let tmp = this.right;
    this.right = this.bottom;
    this.bottom = this.left;
    this.left = this.top;
    this.top = tmp;
  }
 
  turnR() {
    // 前后不变,上变右,右变下,下变左,左变上
    let tmp = this.left;
    this.left = this.bottom;
    this.bottom = this.right;
    this.right = this.top;
    this.top = tmp;
  }
 
  turnF() {
    // 左右不变,上变前,前变下,下变后,后变上
    let tmp = this.front;
    this.front = this.top;
    this.top = this.back;
    this.back = this.bottom;
    this.bottom = tmp;
  }
 
  turnB() {
    // 左右不变,前变上,上变后,后变下,下边前
    let tmp = this.top;
    this.top = this.front;
    this.front = this.bottom;
    this.bottom = this.back;
    this.back = tmp;
  }
 
  turnA() {
    // 上下不变, 前变右,右变后,后变左,左变前
    let tmp = this.right;
    this.right = this.front;
    this.front = this.left;
    this.left = this.back;
    this.back = tmp;
  }
 
  turnC() {
    // 上下不变, 右变前,前变左,左变后,后变右
    let tmp = this.front;
    this.front = this.right;
    this.right = this.back;
    this.back = this.left;
    this.left = tmp;
  }
 
  print() {
    let { left, right, front, back, top, bottom } = this;
    console.log(`${left}${right}${front}${back}${top}${bottom}`);
  }
}

Java算法源码

import java.util.Scanner;

public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    String[] directives = sc.nextLine().split(" ");
    turnDice(directives);
  }

  public static void turnDice(String[] directives) {
    Dice dice = new Dice();

    for (String directive : directives) {
      switch (directive) {
        case "L":
          dice.turnL();
          break;
        case "R":
          dice.turnR();
          break;
        case "F":
          dice.turnF();
          break;
        case "B":
          dice.turnB();
          break;
        case "A":
          dice.turnA();
          break;
        case "C":
          dice.turnC();
          break;
      }
    }

    dice.print();
  }
}

class Dice {
  int left = 1;
  int right = 2;
  int front = 3;
  int back = 4;
  int top = 5;
  int bottom = 6;

  public void turnL() {
    // 前后不变,上变左,左变下,下变右,右变上
    int tmp = this.right;
    this.right = this.bottom;
    this.bottom = this.left;
    this.left = this.top;
    this.top = tmp;
  }

  public void turnR() {
    // 前后不变,上变右,右变下,下变左,左变上
    int tmp = this.left;
    this.left = this.bottom;
    this.bottom = this.right;
    this.right = this.top;
    this.top = tmp;
  }

  public void turnF() {
    // 左右不变,上变前,前变下,下变后,后变上
    int tmp = this.front;
    this.front = this.top;
    this.top = this.back;
    this.back = this.bottom;
    this.bottom = tmp;
  }

  public void turnB() {
    // 左右不变,前变上,上变后,后变下,下边前
    int tmp = this.top;
    this.top = this.front;
    this.front = this.bottom;
    this.bottom = this.back;
    this.back = tmp;
  }

  public void turnA() {
    // 上下不变, 前变右,右变后,后变左,左变前
    int tmp = this.right;
    this.right = this.front;
    this.front = this.left;
    this.left = this.back;
    this.back = tmp;
  }

  public void turnC() {
    // 上下不变, 右变前,前变左,左变后,后变右
    int tmp = this.front;
    this.front = this.right;
    this.right = this.back;
    this.back = this.left;
    this.left = tmp;
  }

  public void print() {
    String sb =
        String.valueOf(this.left) + this.right + this.front + this.back + this.top + this.bottom;
    System.out.println(sb);
  }
}

Python算法源码

# 算法入口
def turnDice(directives):
    dice = Dice()

    for directive in directives:
        if directive == "L":
            dice.turnL()
        elif directive == "R":
            dice.turnR()
        elif directive == "F":
            dice.turnF()
        elif directive == "B":
            dice.turnB()
        elif directive == "A":
            dice.turnA()
        elif directive == "C":
            dice.turnC()

    return str(dice)

# 筛子实现类
class Dice:
    def __init__(self):
        self.left = 1
        self.right = 2
        self.front = 3
        self.back = 4
        self.top = 5
        self.bottom = 6

    def turnL(self):
        # 前后不变,上变左,左变下,下变右,右变上
        self.right, self.bottom, self.left, self.top = self.bottom, self.left, self.top, self.right

    def turnR(self):
        # 前后不变,上变右,右变下,下变左,左变上
        self.left, self.bottom, self.right, self.top = self.bottom, self.right, self.top, self.left

    def turnF(self):
        # 左右不变,上变前,前变下,下变后,后变上
        self.front, self.top, self.back, self.bottom = self.top, self.back, self.bottom, self.front

    def turnB(self):
        # 左右不变,前变上,上变后,后变下,下边前
        self.top, self.front, self.bottom, self.back = self.front, self.bottom, self.back, self.top

    def turnA(self):
        # 上下不变, 前变右,右变后,后变左,左变前
        self.right, self.front, self.left, self.back = self.front, self.left, self.back, self.right

    def turnC(self):
        # 上下不变, 右变前,前变左,左变后,后变右
        self.front, self.right, self.back, self.left = self.right, self.back, self.left, self.front

    def __str__(self):
        return f"{self.left}{self.right}{self.front}{self.back}{self.top}{self.bottom}"


# 输入获取
directives = input().split()

# 算法调用
print(turnDice(directives))

免责声明:

1、IT资源小站为非营利性网站,全站所有资料仅供网友个人学习使用,禁止商用
2、本站所有文档、视频、书籍等资料均由网友分享,本站只负责收集不承担任何技术及版权问题
3、如本帖侵犯到任何版权问题,请立即告知本站,本站将及时予与删除下载链接并致以最深的歉意
4、本帖部分内容转载自其它媒体,但并不代表本站赞同其观点和对其真实性负责
5、一经注册为本站会员,一律视为同意网站规定,本站管理员及版主有权禁止违规用户
6、其他单位或个人使用、转载或引用本文时必须同时征得该帖子作者和IT资源小站的同意
7、IT资源小站管理员和版主有权不事先通知发贴者而删除本文

0

评论0

站点公告

没有账号?注册  忘记密码?