zoukankan      html  css  js  c++  java
  • 第03次作业-栈和队列

    1.学习总结

    2.PTA实验作业

    题目一:队列7-1 jmu-报数游戏

    报数游戏是这样的:有n个人围成一圈,按顺序从1到n编好号。从第一个人开始报数,报到m(m<n)的人退出圈子;下一个人从1开始报数,报到m的人退出圈子。如此下去,直到留下最后一个人。其中n是初始人数;m是游戏规定的退出位次(保证为小于n的正整数)。要求用队列结构完成。输出数字间以空格分隔,但结尾不能有多余空格。
    

    1.设计思路

    1至num-1依次入队
    if 总人数<退出位次 或 总人数<1
         输出“error!"
    end
    while 队列非空
         k=m-1
         位次数-1个元素出队再入队
         出队并输出一个元素
    end
    

    2.代码截图


    3. PTA提交列表


    编译错误:再C编译器下使用C++头文件
    改正方法:将编译器改为C++编译器

    题目二:队列 7-2 银行业务队列简单模拟

    设某银行有A、B两个业务窗口,且处理业务的速度不一样,其中A窗口处理速度是B窗口的2倍 —— 即当A窗口每处理完2个顾客时,B窗口处理完1个顾客。给定到达银行的顾客序列,请按业务完成的顺序输出顾客序列。假定不考虑顾客先后到达的时间间隔,并且当不同窗口同时处理完2个顾客时,A窗口顾客优先输出。
    输入格式:
    输入为一行正整数,其中第1个数字N(≤1000)为顾客总数,后面跟着N位顾客的编号。编号为奇数的顾客需要到A窗口办理业务,为偶数的顾客则去B窗口。数字间以空格分隔。
    输出格式:
    按业务处理完成的顺序输出顾客的编号。数字间以空格分隔,但最后一个编号后不能有多余的空格。
    输入样例:
    8 2 1 3 9 4 11 13 15
    输出样例:
    1 3 2 9 11 4 13 15
    

    1.设计思路

    for i=0 to n-1
        if 客户编号是奇数
           then 入A队
           else 入B队
    end
    for i=0 to n-1
          if A队非空
             then 取出2个A队元素并输出
          if B队非空
              then 取出1个B队元素并输出
    end
    

    2.代码截图


    3. PTA提交列表

    题目三:队列 7-5 堆栈模拟队列

    设已知有两个堆栈S1和S2,请用这两个堆栈模拟出一个队列Q。
    所谓用堆栈模拟队列,实际上就是通过调用堆栈的下列操作函数:
    int IsFull(Stack S):判断堆栈S是否已满,返回1或0;
    int IsEmpty (Stack S ):判断堆栈S是否为空,返回1或0;
    void Push(Stack S, ElementType item ):将元素item压入堆栈S;
    ElementType Pop(Stack S ):删除并返回S的栈顶元素。
    实现队列的操作,即入队void AddQ(ElementType item)和出队ElementType DeleteQ()。
    输入格式:
    输入首先给出两个正整数N1和N2,表示堆栈S1和S2的最大容量。随后给出一系列的队列操作:A item表示将item入列(这里假设item为整型数字);D表示出队操作;T表示输入结束。
    输出格式:
    对输入中的每个D操作,输出相应出队的数字,或者错误信息ERROR:Empty。如果入队操作无法执行,也需要输出ERROR:Full。每个输出占1行。
    输入样例:
    3 2
    A 1 A 2 A 3 A 4 A 5 D A 6 D A 7 D A 8 D D D D T
    输出样例:
    ERROR:Full
    1
    ERROR:Full
    2
    3
    4
    7
    8
    ERROR:Empty
    

    1.设计思路

    while 输入字符不是‘T’
              if 输入‘A’
                 then  输入待操作元素
                 if S1中元素个数达S2栈容且S2非空 或 错误信息
                    then 输出"ERROR:Full"
                 if S1中元素个数达S2栈容且S2为空
                    then 依次取出S1元素进栈S2
                 if S1中元素个数未达S2栈容
                    元素入S1
               end          
               else if 输入‘D'
                 if S2为空且S1为空
                    then 输出"ERROR:Empty"
                 if S2为空且S1非空
                    then 依次取出S1元素进栈S2
                 if S2非空
                    then 取出S2栈顶元素并输出
               end
    end
    
      
    

    2.代码截图



    3. PTA提交列表

    3.本周题目集的PTA最后排名

    1. 栈PTA排名

    2. 队列PTA排名

    3.我的总分

    36+121=157分(必做、选做做了部分)
    1.5分

    4. 阅读代码

    #include <iostream>
    #include <algorithm>
    #include <cstring>
    #include <stack>
    #include <cmath>
    
    using namespace std;
    
    char s[1000];
    int  i;  
    
    double Translation(int & i)
    {
        double integer = 0.0;  
        double remainder = 0.0;  
    
        while (s[i] >= '0' && s[i] <= '9')
        {
            integer *= 10;
            integer += (s[i] - '0');
            i++;
        }
    
        if (s[i] == '.')
        {
            i++;
            int c = 1;
            while (s[i] >= '0' && s[i] <= '9')
            {
                double t = s[i] - '0';
                t *= pow(0.1, c);
                c++;
                remainder += t;
                i++;
            }
        }
    
        return integer + remainder;
    }
    
    
    int GetLevel(char ch)
    {
        switch (ch)
        {
        case '+':
        case '-':
            return 1;
        case '*':
        case '/':
            return 2;
        case '(':
            return 0;
        case '#':
            return -1;
        };
    }
    
    
    double Operate(double a1, char op, double a2)
    {
        switch (op)
        {
        case '+':
            return a1 + a2;
        case '-':
            return a1 - a2;
        case '*':
            return a1 * a2;
        case '/':
            return a1 / a2;
        };
    }
    
    
    double Compute()
    {
        stack<char> optr;    
        stack<double> opnd;  
    
        optr.push('#');
        int len = strlen(s);
        bool is_minus = true;  
    
        for (i = 0; i < len;)
        {
            
            if (s[i] == '-' && is_minus)  
            {
                opnd.push(0);
                optr.push('-');
                i++;
            }
           
            else if (s[i] == ')')
            {
                is_minus = false;
                i++;
    
                while (optr.top() != '(')
                {
                    double a2 = opnd.top();
                    opnd.pop();
                    double a1 = opnd.top();
                    opnd.pop();
                    char op = optr.top();
                    optr.pop();
    
                    double result = Operate(a1, op, a2);
                    opnd.push(result);
                }
    
                optr.pop(); 
            }
            
            else if (s[i] >= '0' && s[i] <= '9')
            {
                is_minus = false;
                opnd.push(Translation(i));
            }
            
            else if (s[i] == '(')
            {
                is_minus = true;
                optr.push(s[i]);
                i++;
            }
            
            else
            {
                while (GetLevel(s[i]) <= GetLevel(optr.top()))
                {
                    double a2 = opnd.top();
                    opnd.pop();
                    double a1 = opnd.top();
                    opnd.pop();
                    char op = optr.top();
                    optr.pop();
    
                    double result = Operate(a1, op, a2);
                    opnd.push(result);
                }
    
                optr.push(s[i]);
                i++;
            }
        }
    
        while (optr.top() != '#')
        {
            double a2 = opnd.top();
            opnd.pop();
            double a1 = opnd.top();
            opnd.pop();
            char op = optr.top();
            optr.pop();
    
            double result = Operate(a1, op, a2);
            opnd.push(result);
        }
    
        return opnd.top();
    }
    
    int main()
    {
        while (cin >> s)
            cout << "结果为:" << Compute()<< endl << endl;
    }
    

    这段代码的功能是进行四则运算,优点:将数字字符转化为可以直接运算的数字,方便运算;将部分运算符优先级用数字表示,便于比较字符优先级。
    地址:https://gitee.com/adressad/codes/9ystlgoh6b413n5vfupa839

    5. 代码Git提交记录截图

  • 相关阅读:
    filter 静态资源
    getRequestURI,getRequestURL的区别
    基于NodeJs的网页爬虫的构建(二)
    基于NodeJs的网页爬虫的构建(一)
    Reverse Words in a String
    Sum Root to Leaf Numbers
    Search Insert Position
    Wildcard Matching
    Trapping Rain Water
    Gray Code
  • 原文地址:https://www.cnblogs.com/lanjinghui/p/8734319.html
Copyright © 2011-2022 走看看