본문 바로가기

CodeUp

C언어)CodeUp 1410 [올바른 괄호 1(괄호 개수 세기)]

문제 설명

프로그래밍을 할 때 가장 중요한 것 중 하나가 괄호의 개수를 맞추는 것이다.

즉, 여는 괄호가 있으면 항상 닫는 괄호가 있고, 닫는 괄호가 있으면 여는 괄호도 있어야 한다.

올바른 괄호를 확인하기 위해 가장 기본적인 방법 중 하나는 여는 괄호와 닫는 괄호의 개수를 세는 것이다.

소괄호로 이루어진 문자열을 주어지면 괄호의 개수를 출력하는 프로그램을 작성하시오.

입력

괄호로 이루어진 문자열이 입력된다. (길이 100,000 이하)

출력

여는 괄호의 개수와 닫힌 괄호의 개수를 출력한다.

입력 예시   예시 복사

((())()(()))

출력 예시

6 6

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <stdio.h>
int main(){
    char s[100001];
    int i, a=0, b=0;
    scanf("%s"&s);
    for(i=0; s[i]!='\0'; i++){
        if(s[i]=='(')
            a++;
        if(s[i]==')')
            b++;
    }
    printf("%d %d", a, b);
    return 0;
}
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none;color:white">cs

문제 풀이 방법 : 특수문자에 대해 알고 있다면 쉽게 풀 수 있다.


/*스크롤 인디케이터 시작*/ .header { position: absolute; top: 0; z-index: 1; width: 100%; background-color: #fff; } .progress-container { width: 100%; height: 4px; background: #f6f6f6; } .progress-bar { height: 4px; background: #6C6C6C; width: 0%; } /*스크롤 인디케이터 종료*/ /*왼쪽, 좌측 밑의 이미지 추가 시작*/ /*가로의 길이가 1510일때까지는 보여라*/ @media screen and (min-width:1500px) { .main_ad { display:visible; position: absolute; left: 0px; bottom: 80px; cursor: pointer; z-index: 11; } } /*가로의 길이가 1511일까지는 보이지 말아라*/ @media screen and (max-width:1510px) { .main_ad { display:none; position: absolute; left: 0px; bottom: 80px; cursor: pointer; z-index: 11; } } /*왼쪽, 좌측 밑의 이미지 추가 종료*/ /*이웃추가버튼 추가 시작*/ @media screen and (min-width:500px) { .add_adBanner { display:visible; } } @media screen and (max-width:510px) { .add_adBanner { display:none; } } .add_adBanner ul { position: absolute; left: 10px; bottom: 22px; cursor: pointer; z-index: 12; } .add_adBanner li { margin : 0 0 0 0; padding : 0 0 0 0; border : 0; float : left; } /*이웃추가버튼 추가 종료*/