5 solutions

  • 2
    @ 2025-10-20 16:46:05
    #include<bits/stdc++.h>
    using namespace std;
    int main()
    {
    	int n;
    	cin>>n;
    	queue<int> q;
    	for(int i=1;i<=n;i++)
    	{
    		string s;
    		cin>>s;
    		if(s=="push")
    		{
    			int x;
    			cin>>x;
    			q.push(x);
    		}
    		else if(s=="pop")
    		{
    			q.pop();
    		}
    		else if(s=="empty")
    		{
    			if(!q.empty())
    			{
    				cout<<"NO"<<endl;
    			}
    			else{ //空
    				cout<<"YES"<<endl;
    			}
    		}
    		else
    		{
    			cout<<q.front()<<endl;	
    		}
    	}
    	return 0;
    }
    
    • 2
      @ 2025-10-20 16:44:00
      #include<bits/stdc++.h>
      using namespace std;
      const int N=100010;
      int q[N];
      int main()
      {
      	int n;
      	cin>>n;
      	int hh=0,tt=-1;
      	for(int i=1;i<=n;i++)
      	{
      		string s;
      		cin>>s;
      		if(s=="push")
      		{
      			int x;
      			cin>>x;
      			tt++;
      			q[tt]=x;
      		}
      		else if(s=="pop")
      		{
      			hh++;
      		}
      		else if(s=="empty")
      		{
      			if(hh<=tt)
      			{
      				cout<<"NO"<<endl;
      			}
      			else{ //空
      				cout<<"YES"<<endl;
      			}
      		}
      		else
      		{
      			cout<<q[hh]<<endl;	
      		}
      	}
      	return 0;
      }
      
      • 1
        @ 2026-9-7 22:12:26
        #include<bits/stdc++.h>
        using namespace std;
        int a[100005];
        int main()
        {
        	int n,l=1,r=0;
        	cin>>n;
        	for(int i=1;i<=n;i++){
        		string s;
        		cin>>s;
        		if(s=="push"){
        			int x;
        			cin>>x;
        			a[++r]=x;
        		}
        		else if(s=="pop"){
        			l++;
        		}
        		else if(s=="empty"){
        
        			cout<<(r<l?"YES":"NO")<<endl;
        		}
        		else {
        			cout<<a[l]<<endl;
        		}
        	}
        	return 0;
        }
        
        • -4
          @ 2024-7-22 10:17:42

          数组:c++

          #include<bits/stdc++.h>
          using namespace std;
          const int N=100010;
          int q[N];
          int hh=0,tt=-1;
          int main()
          {
          	int n;
          	cin>>n;
          	while(n--)
          	{
          		string s;
          		cin>>s;
          		if(s=="push")
          		{
          			int t;
          			cin>>t;
          			q[++tt]=t;
          		}
          		else if(s=="pop")
          		{
          			hh++;
          		}
          		else if(s=="empty")
          		{
          			if(hh<=tt)
          			{
          				cout<<"NO"<<endl;
          			}
          			else
          			{
          				cout<<"YES"<<endl;
          			}
          		}
          		else
          		{
          			cout<<q[hh]<<endl;
          		}
          	}
          	return 0;
          }
          
          • -5
            @ 2024-7-22 10:18:36

            第二种:c++

            #include<bits/stdc++.h>
            using namespace std;
            queue<int> q;
            int main()
            {
            	int n;
            	cin>>n;
            	for(int i=1;i<=n;i++)
            	{
            		string a;
            		cin>>a;
            		if(a=="push")
            		{
            			int x;
            			cin>>x;
            			q.push(x);
            		}
            		if(a=="pop") q.pop();
            		if(a=="empty")
            		{
            			if(q.empty()) cout<<"YES"<<endl;
            			else cout<<"NO"<<endl;
            		}
            		if(a=="query")
            		{
            			cout<<q.front()<<endl;
            		}
            	}
            	return 0;
            }
            
            
            • 1

            Information

            ID
            162
            Time
            1000ms
            Memory
            256MiB
            Difficulty
            1
            Tags
            (None)
            # Submissions
            119
            Accepted
            32
            Uploaded By