D.S.
Übermensch : To a Brighter Future
D.S.
전체 방문자
오늘
어제
  • 분류 전체보기 (109)
    • Lecture Notes (1)
      • CS231n (1)
    • Thoughts on Videos (ToV) (7)
    • Tech (0)
    • 개인 기록 (0)
      • 프로그래밍공부일지 (0)
      • CS Study 한눈에 보기 (0)
    • 프로그래밍 (65)
      • C (35)
      • C++ (0)
      • Python (12)
      • 이코테python (8)
      • 2022군장병AI_SW_Elice (4)
      • 2022군장병AI_SW_Kakao (1)
      • Solidity (2)
      • Web (3)
    • Mental Augmentation (28)
      • Kwik Reading (22)
      • 독서 (0)
      • 갸꿀팁 (2)
      • 3-Part Memory Training (1)
      • Kwik Recall (3)
    • Physical Augmentation (3)
      • Health Journal (2)
      • Sinclair Podcast (1)
    • Others (0)
      • 여행 (0)
    • Idea Bank (0)
      • VBA (0)
      • PLANS (0)
    • 22-2학기 수업 (0)
      • 데이터베이스시스템 (0)

블로그 메뉴

  • 홈
  • 태그
  • 방명록

글쓰기

Kwik Reading

공지사항

인기 글

태그

  • Eye Fixation
  • ai/sw
  • lifespan
  • Kwik Reading
  • Kwik Brain
  • 짐 퀵
  • 노화의 종말
  • 뇌 요가
  • kwik recall
  • 백준
  • 번역
  • 디지털 방해
  • 비문학독해
  • 기술독서
  • 속독 훈련
  • subvocalization
  • 독서
  • brain yoga
  • 속발음
  • 군장병
  • elice
  • 속독
  • 습관
  • Dandapani
  • 우뇌
  • baekjoon
  • Infinity Technique
  • brain break
  • Jim Kwik
  • 학습

최근 댓글

hELLO · Designed By 정상우.
D.S.

Übermensch : To a Brighter Future

프로그래밍/C

baekjoon 4949 attempt1이 안되는 이유와 수정코드

2022. 1. 22. 23:59

<<<<attempt1>>>>

#include<stdio.h>
#include<string.h>
//():0 []:1 

int main(){
    int stack[105];
    int top = 0;
    char string[105];
    
    
    while(1){
        fgets(string,105,stdin);
        if(string[0]=='.'&&string[1]=='\n')
            return 0;
        for(int i=0;string[i]!='\0';i++){
            if(string[i]=='\n'){
                if(top==0)
                    printf("yes\n");
                else
                    printf("no\n");
                top=0;
            }
            else if(string[i]=='(')
                stack[top++] = 0;
            else if(string[i]=='[')
                stack[top++] = 1;
            else if(string[i]==')'){
                if(stack[top-1]==0)//////////*************top=0인 경우에 stack[-1]에 접근할 수 없으므로 틀림.
                    top--;
                else{
                    printf("no\n");
                    top = 0;
                    break;
                }
            }
            else if(string[i]==']'){
                if(stack[top-1]==1)//////////*************top=0인 경우에 stack[-1]에 접근할 수 없으므로 틀림.
                    top--;
                else{
                    printf("no\n");
                    top = 0;
                    break;
                }
            }
        }
    }
    return 0;
}

 

 

----------------------------------------------------------------------------------------------------------------------------------------

반례를 찾으려고 하기 보다는 내 코드에 어떠한 문제가 발생할 수 있는지 기본적인것부터 확인해보자.

***배열의 경우에는 범위를 벗어나는 경우가 빈번히 발생하므로 이를 주의하도록 하자!!!

----------------------------------------------------------------------------------------------------------------------------------------

 

<<<<수정코드>>>>

 

#include<stdio.h>
#include<string.h>
//():0 []:1 

int main(){
    int stack[105] = {-1,};
    int top = 0;
    char string[105];
    
    
    
    while(1){
        fgets(string,105,stdin);
        if(string[0]=='.'&&string[1]=='\n')
            return 0;
        for(int i=0;string[i]!='\0';i++){
            if(string[i]=='\n'){
                if(top==0)
                    printf("yes\n");
                else
                    printf("no\n");
                top=0;
                break;
            }
            else if(string[i]=='(')
                stack[top++] = 0;
            else if(string[i]=='[')
                stack[top++] = 1;
            else if(string[i]==')'){
                if(top>0){
                    if(stack[top-1]==0)
                        top--;
                    else{
                        printf("no\n");
                        top = 0;
                        break;
                    }
                }
                else{
                    printf("no\n");
                    top = 0;
                    break;
                }
            }
            else if(string[i]==']'){
                if(top>0){
                    if(stack[top-1]==1)
                        top--;
                    else{
                        printf("no\n");
                        top = 0;
                        break;
                    }
                }
                else{
                    printf("no\n");
                    top = 0;
                    break;
                }
            }
        }
    }
    return 0;
}

'프로그래밍 > C' 카테고리의 다른 글

baekjoon 10828.c 스택구현 w/structure pointer  (0) 2022.01.30
baekjoon 1107 w/ dfs 나중에 다시 풀어볼만한 문제(0ms에 도전)  (0) 2022.01.23
baekjoon 1929 소수구하기 // 시간초과 & 에라토스테네스의 체  (0) 2022.01.22
baekjoon 10816 attempt 1 & why it is wrong & 수정 코드  (0) 2022.01.18
baekjoon10814 //using 전역변수  (0) 2022.01.17
    '프로그래밍/C' 카테고리의 다른 글
    • baekjoon 10828.c 스택구현 w/structure pointer
    • baekjoon 1107 w/ dfs 나중에 다시 풀어볼만한 문제(0ms에 도전)
    • baekjoon 1929 소수구하기 // 시간초과 & 에라토스테네스의 체
    • baekjoon 10816 attempt 1 & why it is wrong & 수정 코드
    D.S.
    D.S.

    티스토리툴바