염치불구하고.. xhaeneung 문제 소스코드 문제점 분석좀 부탁드립니다.

  • K4L1
    K4L1
    #include <stdio.h>
    #include <string.h>
    
    void count(char str[], int count[]);
    
    void count(char str[], int count[])
    {
        int i, j;
        char alp = 'a';
        for(i=0;i<26;i++)
        {
            for(j=0;j<11;j++)   if(str[j] == alp)   count[i]++;
            alp++;
        }
    }
    
    int main()
    {
        int i, j, size;
        int num_a, num_b, num_ans;
        char num[11][6] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"};
        char *p, op, str_a[6], str_b[6], str_out[11], str_ans[11];
    
        scanf("%d", &size);
        for(i=0;i<size;i++)
        {
            int count_out[26] = {0}, count_ans[26] = {0};
    
            scanf("%s %c %s %*c %s", str_a, &op, str_b, str_out);
    
            for(j=0;j<11;j++)
            {
                if(!strcmp(str_a, num[j]))  num_a = j;
                if(!strcmp(str_b, num[j]))  num_b = j;
            }
    
            while((p = strchr(str_out, '\n')) != NULL)  *p = '\0';
            switch(op)
            {
                case '+':
                    num_ans = num_a + num_b;
                    break;
    
                case '-':
                    num_ans = num_a - num_b;
                    break;
    
                case '*':
                    num_ans = num_a * num_b;
                    break;
            }
    
            for(j=0;j<11;j++)
            {
                if(j == num_ans)    strcpy(str_ans, num[j]);
            }
    
            count(str_out, count_out);
    
            count(str_ans, count_ans);
    
            for(j=0;j<26;j++)
            {
                if(count_out[j] == count_ans[j])
                {
    
                    if(j == 25) printf("Yes\n");
                    continue;
                }
                else
                {
                    printf("No\n");
                    break;
                }
            }
        }
    }
    

    대충 생각한 풀이법은 입력받은 문자열과 정답 문자열의 각 알파벳 개수를 배열로 저장하고 비교하는 것인데.. 어디가 잘못된 것일까요?


    8년 전
1개의 댓글이 있습니다.
  • hyunhwan
    hyunhwan

    num_ans의 답이 0미만 혹은 10을 초과하는 경우 str_ans 문자열 배열이 기존의 값으로 유지되는 문제가 있을 것으로 보입니다.


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