= 기호 이후, 결과 값은 문자열이 변형되어도 같다고 인정될수 있으니, 아스키코드값의 합을 사용해 변환(예, zero 는 122+101+114+111 이므로 448 값이 되고, 448 값을 0 으로 변환)
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;
}
chanhee92
https://www.algospot.com/judge/problem/read/XHAENEUNG
문제의 링크는 위의 링크구요.
제가 만든 알고리즘은 간단하게,
변환 함수를 통해 int 값으로 변환
if 문을 사용해 비교후 판독
왜 자꾸 오답으로 뜨는걸까요... 방법이 잘못된건가요 ?
11년 전