1≤ each password ≤ 2,000 2,000 ≤ sum of the password ≤ 4,000
Implementation
솔직히 나는 구현을 디게 멍청하게 풀었다. 풀다가 아 이길이 아닌데.. 이거 다르게 풀면 빠른데.. 이걸 알았지만 문제 뭐냐 여태까지 풀었던 코드가 꽤 길어서
그냥 오기로 내 방식대로 풀고 효율적인 코드도 작성해보고 둘 다 제출해봤다. 다행이 둘다 AC를 바로 맞아서 뿌듯하긴 했으나..
실력이 많이 안 좋아진 것을 실감하는 계기가 되었다… 무슨 몇 달 쉬면 퇴물이 되버리냐 더 열심히 해야겠다.
아 그리고 문제 이해가 약간 가볍게 읽으면 이상한데 빠지니 좀 잘 읽어보고 풀도록 하자!
오늘의 정답 코드는 두개가 올라갑니다아
정답 코드들 CPP</>
#include <iostream>#include <queue>#include <vector>#include <algorithm>using namespace std;using ll = long long;char postNumbers[] = { 'I' , 'V', 'X', 'L', 'C', 'D', 'M'};int values[] = { 1, 5, 10, 50, 100, 500, 1000};int solution(string current){ int result = 0; int size = 7; int idx = 0; int currentStack = 0; while(idx < current.size()){ bool check = false; int currentCost = 0; for(int i = 6; i > -1; i--){ if(current[idx] == postNumbers[i]){ if(i <= size){ size = i; check = true; } currentCost = values[i]; break; } } currentStack = values[size]; idx++; result += currentCost - currentStack*(!check)*2; } return result;}int pow(int a, int b){ int result = a * 1000; while(b-->0) result /= 10; return result;}string getResult(int num) { string result = ""; int values[] = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1}; string numerals[] = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"}; for(int i = 0; i < 13; i++) { while(num >= values[i]) { result += numerals[i]; num -= values[i]; if(numerals[i].size() > 1) break; } } return result;}int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); string a, b; cin >> a >> b; int resultNumber = solution(a) + solution(b); cout << resultNumber<<"\n"<<getResult(resultNumber); return 0;}
CPP</>
#include <iostream>#include <queue>#include <vector>#include <algorithm>using namespace std;using ll = long long;char postNumbers[] = { 'I', 'V', 'X', 'L', 'C', 'D', 'M'};int values[] = { 1, 5, 10, 50, 100, 500, 1000};int solution(string current) { int result = 0; int size = 7; int idx = 0; int currentStack = 0; while(idx < current.size()) { bool check = false; int currentCost = 0; for(int i = 6; i > -1; i--) { if(current[idx] == postNumbers[i]) { if(i <= size) { size = i; check = true; } currentCost = values[i]; break; } } currentStack = values[size]; idx++; result += currentCost - currentStack*(!check)*2; } return result;}int pow(int a, int b) { int result = a * 1000; while(b-- > 0) result /= 10; return result;}string getResult(string value) { string result = ""; bool check[] = {0, 0, 0, 0, 0, 0}; string checkString[] = { "IV", "IX", "XL", "XC", "CD", "CM" }; int current = 0; while(current < value.size()) { int currentValue = value[current] - '0'; int position = value.size() - 1 - current; for(int i = 3; i > 1; i--) { int limit = i * i; if(currentValue == limit) { int idx = position * 2 + !(limit == 4); if(check[idx]) continue; check[idx] = true; result += checkString[idx]; currentValue -= limit; } } for(int i = 6; i > -1; i--) { int power = 1; for(int j = 0; j < position; j++) power *= 10; if(values[i] > power * 5 || values[i] < power) continue; while(currentValue >= values[i] / power) { result += postNumbers[i]; currentValue -= values[i] / power; } } current++; } return result;}int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); string a, b; cin >> a >> b; int resultNumber = solution(a) + solution(b); string resultString = ""; int t = 1; while(resultNumber >= t) { resultString += '0' + resultNumber%(t*10)/t; t *= 10; } reverse(resultString.begin(), resultString.end()); cout << resultNumber<<"\n" << getResult(resultString); return 0;}