c++ 趣味算法题 渐开螺旋矩阵

渐开螺旋矩阵(c++)

题目说明

简单来说,一个骰子游戏,3在上,2在前,1在右,4在下,5在后,6在右,这样一个骰子沿着图示的矩阵,按照编号顺序转动,在1的位置是原点,1向右滚动到2,2向下滚动到3,3向左滚动到4,4向左滚动到5,5向上滚动到6…

解题思路:

一个类,number是矩阵中的编号,六个变量代表骰子的六个面

class pivoting
{
public:
    pivoting():number(""),top(3),left(6),right(1),front(2),back(5),down(4){}
    void move();
    void in_put();
    void show();
private:
    string number;
    int top;
    int left;
    int right;
    int front;
    int back;
    int down;
};

类中有输入输出移动函数
其中移动函数,骰子只能向四个方向滚动,方向n初始1为向右,2向下,3向左,4向上
刚开始向右count走1步,最大max_count为1步,相等时,count清零,改变方向n,同一步数改变方向次数max_num加1
当同一步数改变两次方向的时候,max_num清零,方向上最大步数max_count加1

void pivoting::move(){
    long long num = std::stoll(number) - 1;  //编号
    int n = 1;  //方向
    int count = 0;  //一个方向走了几步
    int max_count = 1;  //一个方向的最大步数,初始为1步
    int max_num = 0;  //第几次同一步数改变方向

    for(num;num!=0; --num){
        if (n==1){
            int temp = top;
            top = left;
            left = down;
            down = right;
            right = temp;
        }

        else if (n==2){
            int temp = top;
            top = back;
            back = down;
            down = front;
            front = temp;
        }

        else if (n==3){
            int temp = top;
            top = right;
            right = down;
            down = left;
            left = temp;
        }

        else if (n==4){
            int temp = top;
            top = front;
            front = down;
            down = back;
            back = temp;
        }

        ++count;  //走一步,加1
        if (count == max_count)  //走到了最大步数
        {
            count = 0;  //方向步数清零
            ++n;  //改方向
            if (n > 4)  { n = 1; }

            ++max_num;
            if (max_num == 2) //第二次同一步数改变方向
            {
                max_num = 0;
                ++max_count;  //最大步数++
            }
        }
    }
}

完整代码如下

#include<iostream>

using namespace std;

class pivoting
{
public:
    pivoting():number(""),top(3),left(6),right(1),front(2),back(5),down(4){}
    void move();
    void in_put();
    void show();
private:
    string number;
    int top;
    int left;
    int right;
    int front;
    int back;
    int down;
};

void pivoting::in_put(){
    cout << "Enter the desired goal call number:";

    std::getline(cin, number);

    if(number.empty()){
        cout << "Incorrect value, try again " << endl;
        in_put();
    }else{
        string::iterator i = number.begin();
        bool flag = true;
        for(i;i!=number.end();++i){
            int asc = (int) *i;
            if(asc >= 48 && asc <= 57){
                continue;
            }else{
                flag = false;
                break;
            }
        }

        if(flag){
            cout<<"On cell "<<number;
        }else{
            cout<<"Incorrect value, try again "<<endl;
            in_put();
        }
    }
}

void pivoting::move(){
    long long num = std::stoll(number) - 1;  //编号
    int n = 1;  //方向
    int count = 0;  //一个方向走了几步
    int max_count = 1;  //一个方向的最大步数,初始为1步
    int max_num = 0;  //第几次同一步数改变方向

    for(num;num!=0; --num){
        if (n==1){
            int temp = top;
            top = left;
            left = down;
            down = right;
            right = temp;
        }

        else if (n==2){
            int temp = top;
            top = back;
            back = down;
            down = front;
            front = temp;
        }

        else if (n==3){
            int temp = top;
            top = right;
            right = down;
            down = left;
            left = temp;
        }

        else if (n==4){
            int temp = top;
            top = front;
            front = down;
            down = back;
            back = temp;
        }

        ++count;  //走一步,加1
        if (count == max_count)  //走到了最大步数
        {
            count = 0;  //方向步数清零
            ++n;  //改方向
            if (n > 4)  { n = 1; }

            ++max_num;
            if (max_num == 2) //第二次同一步数改变方向
            {
                max_num = 0;
                ++max_count;  //最大步数++
            }
        }
    }
}

void pivoting::show(){
    cout<<", "<<top <<" is at the top, "<<front <<" at the front, and "<<right<<" on the right."<<endl;
}

int main(){
    pivoting pi;
    pi.in_put();
    pi.move();
    pi.show();
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值