WEEKLYCALENDAR 문제 런타임오류 질문

  • bum4985
    bum4985
    #include<stdio.h>
    #include<string.h>
    
    
    int week[10];
    
    int givenumber(int number2)
    {
        int number[15]={0,31,28,31,30,31,30,31,31,30,31,30,31};
    
        if(number2==0) number2=12;
        if(number2==13) number2=1;
    
        return number[number2];
    }
    void givelastday(int firstday,int lastday,int i,int m){
    
        if(i!=7){
    
            if(firstday==lastday)
            {
                week[i]=firstday;
                week[i+1]=1;
                givelastday(2,givenumber(m+1),i+2,m+1);
            }
            else{
                week[i]=firstday;
                firstday++;
                givelastday(firstday,lastday,i+1,m);
            }
        }
        else{
            for(int i=0;i<7;i++)
                printf("%d ",week[i]);
        }
    }
    
    int main(void)
    {
        int count;
        scanf("%d",&count);
    
        while(count--)
        {
            int m,d;
            int lastday;
    
            char sun[30];
            int firstday;
            scanf("%d%d",&m,&d);
            getchar();
            gets(sun);
    
    
            if(!strcmp("Sunday",sun))
            {
                lastday=givenumber(m);
                firstday=d;
    
                givelastday(firstday,lastday,0,m);
    
    
            }
            if(!strcmp("Monday",sun))
            {
                int firstday=d-1;
                if(firstday<1)
                {
                    if(m-1==0)m=13;
                    firstday=givenumber(m-1)+d-1;
                    givelastday(firstday,givenumber(m-1),0,m-1);
                }
                else{
                givelastday(firstday,lastday,0,m);
                }
            }
            if(!strcmp("Tuesday",sun))
            {
                int firstday=d-2;
                if(firstday<1)
                {
                    if(m-1==0)m=13;
                    firstday=givenumber(m-1)+d-2;
                    givelastday(firstday,givenumber(m-1),0,m-1);
                }
                else{
                givelastday(firstday,lastday,0,m);
                }
            }
            if(!strcmp("Wednesday",sun))
            {
                int firstday=d-3;
                if(firstday<1)
                {
                    if(m-1==0)m=13;
                    firstday=givenumber(m-1)+d-3;
                    givelastday(firstday,givenumber(m-1),0,m-1);
                }
                else{
                givelastday(firstday,lastday,0,m);
                }
            }
            if(!strcmp("Thursday",sun))
            {
                int firstday=d-4;
                if(firstday<1)
                {
                    if(m-1==0)m=13;
                    firstday=givenumber(m-1)+d-4;
                    givelastday(firstday,givenumber(m-1),0,m-1);
                }
                else{
                givelastday(firstday,lastday,0,m);
                }
            }
            if(!strcmp("Friday",sun))
            {
                int firstday=d-5;
                if(firstday<1)
                {
                    if(m-1==0)m=13;
                    firstday=givenumber(m-1)+d-5;
                    givelastday(firstday,givenumber(m-1),0,m-1);
                }
                else{
                givelastday(firstday,lastday,0,m);
                }
            }
            if(!strcmp("Saturday",sun))
            {
                int firstday=d-6;
                if(firstday<1)
                {
                    if(m-1==0)m=13;
                    firstday=givenumber(m-1)+d-6;
                    givelastday(firstday,givenumber(m-1),0,m-1);
                }
                else{
                givelastday(firstday,lastday,0,m);
                }
            }
    
        }
    }
    

    튜토리얼 초심자 문제에 있는 WEEKLYCALENDAR 문제입니다.

    날짜를 입력하면 한주를 출력해주는 문제입니다.

    문제는 비쥬얼스튜디오에서는 잘 되는데 알고스팟에서

    RTE (SIGSEGV: segmentation fault, probably incorrect memory access) 런타임 에러가 뜹니다.

    이문제가 할당되지 않은 영역에 접근하려고 할때 생긴다고 하는데

    아무리 봐도 어디가 잘못 됐는지 모르겠습니다.

    아시는 분은 답변주시면 정말 감사하겠습니다. 몇일째 해매고 있습니다.


    10년 전
1개의 댓글이 있습니다.
  • Being
    Being

    i가 6일 때 한 달의 마지막 날이라면 givelastday()가 계속해서 넘어가겠네요.


    10년 전 link
  • 정회원 권한이 있어야 커멘트를 다실 수 있습니다. 정회원이 되시려면 온라인 저지에서 5문제 이상을 푸시고, 가입 후 7일 이상이 지나셔야 합니다. 현재 문제를 푸셨습니다.