1 solutions

  • 0
    @ 2024-12-10 22:16:30

    C :

    #include<stdio.h>/*试编写程序,检查输入的圆括号串的括号是否配对,若配对输出“OK”,若不配对输出“Error ”。*/
    #include<stdlib.h>
    #include<string.h>
    char c[6000];
    int main()
    {int i,m=0;
         gets(c);
         for(i=0;i<strlen(c);i++)
         {if(c[i]=='('){m++;}
         else if(c[i]==')')m--;
                      
                      if(m<0){printf("NO");return 0;}           
                                 }
         if(m==0)printf("YES");
         else if(m>0)printf("NO");
         
        
        
        
    return 0;}
    

    C++ :

    #include<iostream>
    #include<cstring>
    #include<cstdio>
    #include<cstdlib>
    using namespace std;
    char a[260];
    int main()
    {
    	gets(a);
    	int top=0;
    	for(int i=0;i<=strlen(a)-1;i++)
    	{
    		if(a[i]=='(')
    			top++;
    		if(a[i]==')')
    		{
    			if(top>0) 
    				top--;
    			else 
    			{
    				cout<<"NO";
    				return 0;
    			}
    		}
    	}
    	if(top==0)
    		cout<<"YES";
    	else 
    		cout<<"NO";
    		return 0;
    }
    
    
    • 1

    Information

    ID
    532
    Time
    1000ms
    Memory
    128MiB
    Difficulty
    (None)
    Tags
    # Submissions
    0
    Accepted
    0
    Uploaded By