XHAENEUNG 질문입니다.

  • chanhee92
    chanhee92

    https://www.algospot.com/judge/problem/read/XHAENEUNG
    문제의 링크는 위의 링크구요.

    제가 만든 알고리즘은 간단하게,

    1. 차례대로 대입 및 각 변수에 저장
    2. 변환 함수를 통해 int 값으로 변환

      • = 기호 전, 피연산자 들의 문자열은 하나하나 케이스로 변환
      • = 기호 이후, 결과 값은 문자열이 변형되어도 같다고 인정될수 있으니, 아스키코드값의 합을 사용해 변환(예, zero 는 122+101+114+111 이므로 448 값이 되고, 448 값을 0 으로 변환)
    3. if 문을 사용해 비교후 판독

    #include<iostream>
    #include<string>
    using namespace std;
    int convert(string s){
        if (s == "one")
            return 1;
        else if (s == "two")
            return 2;
        else if (s == "three")
            return 3;
        else if (s == "four")
            return 4;
        else if (s == "five")
            return 5;
        else if (s == "six")
            return 6;
        else if (s == "seven")
            return 7;
        else if (s == "eight")
            return 8;
        else if (s == "nine")
            return 9;
        else if (s == "ten")
            return 10;
        else if (s == "zero")
            return 0;
        else
            return -1;
    }
    int convert2(char *s){
        int sum = 0;
        for (int i = 0; s[i] != NULL; i++){
            sum = sum + (int)s[i];
        }
        if (sum == 448)
            return 0;
        else if (sum == 322)
            return 1;
        else if (sum == 346)
            return 2;
        else if (sum == 536)
            return 3;
        else if (sum == 444)
            return 4;
        else if (sum == 426)
            return 5;
        else if (sum == 340)
            return 6;
        else if (sum == 545)
            return 7;
        else if (sum == 529)
            return 8;
        else if (sum == 426)
            return 9;
        else if (sum == 327)
            return 10;
        else
            return -1;
    }
    int operation1(int a, int b, char s){
        if (s == '+')
            return a + b;
        else if (s == '-')
            return a - b;
        else if (s == '*')
            return a*b;
        else
            return -1;
    }
    int main(){
        int c;
        char operation;
        char equal;
        string buf1[2];
        char buf[11];
        cin >> c;
        while (c--){
            cin >> buf1[0] >> operation >> buf1[1] >> equal >> buf;
            if (operation1(convert(buf1[0]), convert(buf1[1]), operation) == convert2(buf) &&
                (operation1(convert(buf1[0]), convert(buf1[1]), operation) >= 0 && operation1(convert(buf1[0]), convert(buf1[1]), operation)<=10))
                cout << "YES" << endl;
            else
                cout << "NO" << endl;
    
        }
        return 0;
    }
    

    왜 자꾸 오답으로 뜨는걸까요... 방법이 잘못된건가요 ?


    8년 전
3개의 댓글이 있습니다.
  • astein
    astein

    변환함수(convert2)가 정상적으로 동작하지 않습니다. 잘못된 입력에서 -1이 아닌 값을 리턴할 수 있습니다.


    8년 전 link
  • chanhee92
    chanhee92

    @astein 님
    convert2 에서 제가 오류를 찾았네요.. 어떠어떠한 조합으로는 원래 의미하는 숫자가 아닌 다른 숫자의 값과 합이 일치할 수도 있다는 점에서 저 방식이 아닌 sort 해서 비교하는 방법으로 바꿨습니다. 감사합니다 ㅎㅎ


    8년 전 link
  • chanhee92
    chanhee92

    그런대 여기서 바꾼 코드로 해도 오답처리가 되네요..


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