회원가입
로그인
Toggle navigation
문제
문제
전체 문제
문제 출처
단계별로 풀어보기
알고리즘 분류
추가된 문제
문제 순위
문제
푼 사람이 한 명인 문제
아무도 못 푼 문제
최근 제출된 문제
최근 풀린 문제
랜덤
출처
ICPC
Olympiad
한국정보올림피아드
한국정보올림피아드시․도지역본선
전국 대학생 프로그래밍 대회 동아리 연합
대학교 대회
카카오 코드 페스티벌
Coder's High
ICPC
Regionals
World Finals
Korea Regional
Africa and the Middle East Regionals
Europe Regionals
Latin America Regionals
North America Regionals
South Pacific Regionals
문제집
대회
4
채점 현황
랭킹
게시판
그룹
더 보기
재채점 기록
블로그
강의
실험실
도움말
BOJ Stack
BOJ Book
전체
공지
자유
질문
오타/오역/요청
게시판 공지
홍보
업데이트
solved.ac
글쓰기
질문 도움말
자주묻는 질문
스택 문제 어디가 틀렸을까요?
10828번 - 스택
ook505
6년 전
0
예제는 정상적으로 실행되는데ㅠ 어디가 틀렸는지 잘 모르겠습니다..
어디가 틀렸을까요..??
#include <stdio.h> #include <string.h> void push(int *stack, int x); void pop(int *stack); void size(); void empty(); void top(int *stack); int top_num = -1; int main() { int i, num, element, stack[10001] = { NULL }; char command[6]; scanf("%d", &num); for (i = 0; i < num; i++) { scanf("%s", command); if (!strcmp(command, "push")) { scanf("%d", &element); push(stack, element); } else if (!strcmp(command, "pop")) pop(stack); else if (!strcmp(command, "size")) size(); else if (!strcmp(command, "top")) top(stack); else if (!strcmp(command, "empty")) empty(); } } void push(int *stack, int x) { stack[++top_num] = x; } void pop(int *stack) { int temp; if (top_num == -1) printf("-1\n"); else { temp = top_num; printf("%d\n", stack[top_num--]); stack[temp] = NULL; } } void size() { printf("%d\n", top_num + 1); } void empty() { if (top_num == -1) printf("1\n"); else printf("0\n"); } void top(int *stack) { printf("%d\n", stack[top_num]); }
sgchoi5
6년 전
1
index 가 -1 로 초기화되고 난 후에, top 명령어가 처음 호출되면 잘못된 값이 나오겠네요...
댓글을 작성하려면
로그인
해야 합니다.
ook505 6년 전
예제는 정상적으로 실행되는데ㅠ 어디가 틀렸는지 잘 모르겠습니다..
어디가 틀렸을까요..??