码迷,mamicode.com
首页 > 编程语言 > 详细

算法笔记_080:蓝桥杯练习 队列操作(Java)

时间:2017-03-15 12:10:39      阅读:206      评论:0      收藏:0      [点我收藏+]

标签:alt   style   scan   opera   []   port   .com   第一个   ref   

目录

1 问题描述

2 解决方案

 


1 问题描述

问题描述
  队列操作题。根据输入的操作命令,操作队列(1)入队、(2)出队并输出、(3)计算队中元素个数并输出。
输入格式
  第一行一个数字N。
  下面N行,每行第一个数字为操作命令(1)入队、(2)出队并输出、(3)计算队中元素个数并输出。
输出格式
  若干行每行显示一个2或3命令的输出结果。注意:2.出队命令可能会出现空队出队(下溢),请输出“no”,并退出。
样例输入
7
1 19
1 56
2
3
2
3
2
样例输出
19
1
56
0
no
数据规模和约定
  1<=N<=50

 


2 解决方案

技术分享

特别注意:注意:2.出队命令可能会出现空队出队(下溢),请输出“no”,并退出

 

具体代码如下:

import java.util.ArrayList;
import java.util.Scanner;

public class Main {
    
    public void printResult(int[][] operation) {
        ArrayList<Integer> list = new ArrayList<Integer>();
        for(int i = 0;i < operation.length;i++) {
            if(operation[i][0] == 1)
                list.add(operation[i][1]);
            else {
                if(operation[i][0] == 2) {
                    if(list.size() == 0) {
                        System.out.println("no");
                        return; //注意,此时题意要求直接退出
                    }
                    else {
                        System.out.println(list.get(0));
                        list.remove(0);
                    }
                } else if(operation[i][0] == 3) {
                    System.out.println(list.size());
                }
            }
        }
        return;
    }
    
    public static void main(String[] args) {
        Main test = new Main();
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        if(n < 1 || n > 50)
            return;
        int[][] operation = new int[n][2];
        for(int i = 0;i < n;i++) {
            int a = in.nextInt();
            if(a == 1) {
                int b = in.nextInt();
                operation[i][0] = a;
                operation[i][1] = b;
            } else {
                operation[i][0] = a;
            }
        }
        test.printResult(operation);
    }
}

 

算法笔记_080:蓝桥杯练习 队列操作(Java)

标签:alt   style   scan   opera   []   port   .com   第一个   ref   

原文地址:http://www.cnblogs.com/liuzhen1995/p/6553083.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!