회원가입
로그인
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
문제집
대회
1
채점 현황
랭킹
게시판
그룹
더 보기
재채점 기록
블로그
강의
실험실
도움말
BOJ Stack
BOJ Book
전체
공지
자유
질문
오타/오역/요청
게시판 공지
홍보
업데이트
solved.ac
글쓰기
질문 도움말
자주묻는 질문
5번만에 맞추긴 했는데... 그 전에꺼는 왜 틀렸는지 모르겠습니다
10828번 - 스택
amateur_k
7년 전
0
C로 짠 코드인데... 한줄로 받아서 명령어가 push일 때, 토큰으로 나눠서 atoi로 함수에 전달하는 방법은 틀렸다고 하네요 왜 이런 결과가 나왔는지 알 수 있을까요?
#include <stdio.h> #include <stdlib.h> #include <string.h> int stack[10000]; int index = -1; void push(int a) { stack[++index] = a; } void pop() { if(index == -1) { printf("%d\n",-1); } else { printf("%d\n",stack[index--]); } } void size() { printf("%d\n",index+1); } void empty() { if(index == -1) { printf("%d\n",1); } else { printf("%d\n",0); } } void top() { if(index == -1) { printf("%d\n",-1); } else { printf("%d\n",stack[index]); } } int main () { int n, i, e; char command[20]; char* var; scanf("%d",&n); for(i=0; i<n; i++) { fflush(stdin); //scanf("%[^\n]",&command); scanf("%s",&command); if(!strcmp("push",command)) { scanf("%d",&e); push(e); }/* if(!strncmp("push",command, 4)) { var = strtok(command, " "); var = strtok(NULL, " "); push(atoi(var)); }*/ if(!strcmp("pop",command)) pop(); if(!strcmp("top",command)) top(); if(!strcmp("size",command)) size(); if(!strcmp("empty",command)) empty(); } return 0; }
댓글을 작성하려면
로그인
해야 합니다.
amateur_k 7년 전