算法与数据结构题目 4.3 不知道取什么好

不知道取什么好

算法与数据结构 题目4.3



题目

原题


实验规则

  • 长度n的序列(1-n)
  • 判断 序列第一个数==当前最大值
    ,输出该数
    ,放入末尾
  • 要求:计算某个编号的数据在第几次被输出

设计思路

  • 环状链表来模拟序列,每个节点当作一个数据。
  • 链表: 序号,数据,下一个节点的指针
  • 判断当前数据==最大值数组的最大值
    – 真–>输出节点,计数器 + 1
    – 假–>跳过节点(环状链表,跳过后直接在序列末尾)
  • 判断该节点==所求节点?
    –真–>打印计数器的值(既该节点为第几次输出)
    –假–>进入下一个节点

流程图

Created with Raphaël 2.1.0 Start 获取序列长度 n 和所求数字序号 m 建立对应的数据类 NumData 建立相应的链表 _preNum 存储序列 建立相应的数组 _maxNum 存储并排序序列 开始模拟输出 获得下一个数据节点 该节点的数字==当前最大值? Yes or No? 输出,删除该节点 计数器 count += 1 该节点的序号==所求数字序号? Yes or No? 打印所求的次数 count End yes no yes no

代码实现


#include<iostream>

//Node :store the number of the array
typedef struct Node {
    int _num; // store the serial number of each number in the array
    int _data;  //store the number in the array
    Node * _next; //point the next number
}NumNode;
class Num_Data {
public:
    Num_Data();    
    Num_Data(const int &n,const int &m);
    ~Num_Data();
    void DataDel();     //delete a number node  from the array
    void DataShow();    //show a number node from the  array
    void DataSort(int left, int right);    //to sort all numbers in the array (QuickSort)
    void DataStare();    //begin show
private:
    NumNode * _preNum; //point a number node in this array
    int * _maxNum;   //save a oderly array
    int _len;    //the length of the array
    int _num;    //the  No what you want to know 
    int _count;  //a counter
};
Num_Data::Num_Data() {
    _preNum = NULL;
    _maxNum = NULL;
    _len = 0;
    _num = 0;
    _count = 0;
}
Num_Data::Num_Data(const int &n,const int &m) {
    if (n <= 0) return;

    // get the length and number of this Array
    _len = n;
    _num = m;
    _count = 0;

    _maxNum = new int[n];

    NumNode * temp;

    _preNum = temp = new NumNode;
    for (int i = 1; i < n; i += 1) {
        temp->_next = new NumNode;
        temp = temp->_next;
        std::cin >> _maxNum[i - 1];
        temp->_num = i;
        temp->_data = _maxNum[i - 1];
    }

    temp->_next = _preNum;

    _preNum->_num = n;
    std::cin >> _maxNum[n - 1];
    _preNum ->_data = _maxNum[n - 1];

    DataSort(0, _len - 1);
}
Num_Data::~Num_Data() {
    delete[] _maxNum;
    if (_preNum) {
        NumNode *pre, *las;
        pre = _preNum;
        las = pre->_next;
        while (las != _preNum) {
            delete pre;
            pre = las;
            las = pre->_next;
        }
        delete pre;
    }
}
void Num_Data::DataDel() {
    NumNode * temp = _preNum->_next;
    _preNum->_next = temp->_next;
    delete temp;
}
void Num_Data::DataShow() { 
    this->_count += 1;
    DataDel();
}
void Num_Data::DataSort(int left,int right) {
    if (left < right) {
        int key = _maxNum[left];
        int low = left;
        int high = right;
        while (low < high) {
            while (low < high && _maxNum[high] <= key) {
                high -= 1;
            }
            _maxNum[low] = _maxNum[high];
            while (low < high && _maxNum[low] >= key) {
                low += 1;
            }
            _maxNum[high] = _maxNum[low];
        }
        _maxNum[low] = key;
        DataSort(left, low - 1);
        DataSort(low + 1, right);
    }
}
void Num_Data::DataStare() {
    while (1) {
        if (_preNum->_next->_data == _maxNum[_count]) {
            if (_preNum->_next->_num == this->_num) {
                std::cout << _count + 1;
                break;
            }
            DataShow();
        }
        else
            _preNum = _preNum->_next;
    }
}
int main(void) {
    int n, m;
    std::cin >> n >> m;
    Num_Data data(n, m);
    data.DataStare();
    return 0;
}

个人觉得

觉得自己可能写多了。
首先觉得自己代码可能写长了,简单的问题复杂化了。
接着快速排序部分没写好,没理解透快速排序。

最后希望各位给点意见。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值