队列加分项

作业要求

队列加分项

实现代码

public class yhsj {
    public static void main(String[] args) {
        Scanner sca = new Scanner(System.in);
        CircularArrayQueue<Integer> y = new CircularArrayQueue<Integer>();
        y.enqueue(0);
        y.enqueue(1);
        System.out.println("输入你想要的行数:");
        int count = sca.nextInt();
        for (int i = 0;i<=count;){
            int a = y.dequeue();
            int b = y.first();
            if(a==0){
                i++;
                y.enqueue(0);
            }
            y.enqueue(a+b);
            if (a==0)
                System.out.println();
            else
                System.out.print(a+"  ");
        }
    }
}

成果截图

队列加分项

单步跟踪

队列加分项

遇到的问题

  1. 当要求的行数过大时会打印出负数
  • 问题详情:测试中我把行数设置的较大,然后就会出现打印出负数的情况
    队列加分项
  • 解决过程:在请教同学后,我们初步断定是数据溢出,然后我又查了一下Intege类型的最大值极限Integer.MAX_VALUE = 2147483647,这样便证实了我们的想法。

相关文章:

  • 2021-10-04
  • 2021-09-17
  • 2022-01-03
  • 2021-10-05
  • 2022-01-04
  • 2022-12-23
猜你喜欢
  • 2021-06-03
  • 2021-11-19
  • 2021-06-17
  • 2021-12-04
  • 2021-08-25
  • 2021-06-14
相关资源
相似解决方案