Priceless
[백준] 1339번: 단어 수학 (C++) 본문

아마 처음으로 성공한 백준 골드 문제가 아닐까 싶다!
문제 자체는 별로 어렵게 느껴지지 않았다
다만 입력이 A~J 인 것이 아니라
A~Z 영어 대문자를 다 받는 것으로 고려해야 했다
처음에 아스키 코드를 찾아보기 귀찮아서 A~J는 10개니까
if 문으로 때우려고 했다(다음부터 그러지 않겠습니다..)
그러니 계속 오류도 나고 코드가 너무 길어졌다
그래서 바로 아스키 코드 값으로 배열에 저장하고
알파벳 마다 수가 얼마나 곱해져있는지 계산한다
큰 순서대로 9를 할당하고
곱해진 수들을 합하면 된다
// baekjoon 1339
#include<iostream>
#include<algorithm>
#include<string>
#include<cmath>
using namespace std;
// codes for fast I/O
void init(){
cin.tie(0);
cout.tie(0);
ios_base::sync_with_stdio(false);
}
int main(){
init();
int n;
cin >> n;
int count[27] = {0};
for(int i = 0; i < n; i++){
string input;
cin >> input;
long long len = input.length();
for(int j = 0; j < len; j++){
int num = input[j] - 64;
count[num] += pow(10, len - j - 1);
/*
if(input[j] == 'A'){
count[0] += pow(10,(len - j - 1));
}
else if(input[j] == 'B'){
count[1] += pow(10,(len - j - 1));
}
else if(input[j] == 'C'){
count[2] += pow(10,(len - j - 1));
}
else if(input[j] == 'D'){
count[3] += pow(10,(len - j - 1));
}
else if(input[j] == 'E'){
count[4] += pow(10,(len - j - 1));
}
else if(input[j] == 'F'){
count[5] += pow(10,(len - j - 1));
}
else if(input[j] == 'G'){
count[6] += pow(10,(len - j - 1));
}
else if(input[j] == 'H'){
count[7] += pow(10,(len - j - 1));
}
else if(input[j] == 'I'){
count[8] += pow(10,(len - j - 1));
}
else if(input[j] == 'J'){
count[9] += pow(10,(len - j - 1));
}*/
}
}
sort(count, count + 27, greater<int>());
int sum = 0;
for(int i = 0; i < 27; i++){
if(count[i] > 0){
sum += count[i] * (9 - i);
}
else{
continue;
}
}
cout << sum;
}
'Algorithm & Test > 백준' 카테고리의 다른 글
[백준] 1010번: 다리 놓기 (C++) (0) | 2023.08.30 |
---|---|
[백준] 7568번: 덩치 (C++) (0) | 2023.08.29 |
[백준] 1946번: 신입 사원 (C++) (0) | 2023.08.20 |
[백준] 16953번: A → B (C++) (0) | 2023.08.19 |
[백준] 1783번: 병든 나이트 (C++) (0) | 2023.08.18 |