2 条题解

  • 0
    @ 2025-6-14 16:37:15

    #include #include #include

    using namespace std; queue arr; int m; signed main() { cin >> m; while(m--) { string s; int x; cin >> s; if(s == "push") { cin >> x; arr.push(x); } else if(s == "pop") { if(arr.empty()) { continue; } else { arr.pop(); } } else if(s == "empty") { if(arr.empty()) { cout << "YES" << endl; } else { cout << "NO" << endl; } } else if(s == "query") { if(arr.empty()) { cout << "empty" << endl; } else { cout << arr.front() << endl; } } } return 0; }

    • 0
      @ 2025-5-8 22:15:53

      JAVA AC代码:

      
      import java.util.*;
      import java.util.StringTokenizer;
      import java.io.*;
      
      public class Main {
      
      	public static void main(String[] args) {
      		int m=in.nextInt();
      		Queue<Integer> q=new LinkedList<>();//java中普通Queue的实例用LinkedList
      		while(m-->0){
      			String op=in.next();
      			if(op.equals("push")){
      				int x=in.nextInt();
      				q.add(x);
      			}else if(op.equals("pop")){
      				if(!q.isEmpty()){
      					q.poll();
      				}
      			}else if(op.equals("empty")) {
      				if(q.isEmpty()) out.println("YES");
      				else out.println("NO");
      			}else {
      				if(q.isEmpty()) out.println("empty");
      				else out.println(q.peek());
      			}
      		}
      		out.close();
      	}
      	
      	
      
      	static FastReader in=new FastReader();
      	static PrintWriter out=new PrintWriter(System.out);
      	static class FastReader{
      		static BufferedReader br;
      		static StringTokenizer st;
      		FastReader(){
      			br=new BufferedReader(new InputStreamReader(System.in));
      		}
      		String next() {
      			String str="";
      			while(st==null||!st.hasMoreElements()) {
      				try {
      					str=br.readLine();
      				}catch(IOException e) {
      					throw new RuntimeException(e);
      				}
      				st=new StringTokenizer(str);
      			}
      			return st.nextToken();
      		}
      		int nextInt() {
      			return Integer.parseInt(next());
      		}
      		double nextDouble() {
      			return Double.parseDouble(next());
      		}
      		long nextLong() {
      			return Long.parseLong(next());
      		}
      	}
      	
      }
      
      
      • 1

      信息

      ID
      57
      时间
      1000ms
      内存
      256MiB
      难度
      2
      标签
      递交数
      32
      已通过
      17
      上传者