Study/PS(Algorithm)
[C++] 백준 BOJ 1475 방 번호
노랑펭귄
2019. 1. 16. 17:34
문제링크 : https://noj.am/1475
문자열을 입력 받아서 갯수를 카운팅 하고 그 중에서 가장 많이 카운팅 된 숫자가 몇개인 지를 구하는 문제이다.
단, 문제의 조건에서 6과 9는 뒤집어서 사용할 수도 있다고 했기 때문에 0.5씩 카운팅하여 쉽게 해결하였다.
이러한 과정을 위해서 float형을 사용하였다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | // BOJ_1475 방 번호 #include <iostream> #include <string> using namespace std; int max(int a, int b){ return a > b ? a : b; } int round(float input){ float temp = (int)input; return input != temp ? input + 1 : input; } int main() { ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); string room_number; cin >> room_number; // 0~8까지 float cnt[9] = {0.0, }; for(auto i : room_number){ int num = i - '0'; // 6이나 9일 경우는 0.5만 카운트 if(num == 6 || num == 9){ cnt[6] += 0.5; continue; } cnt[num]++; } int result = 0; for(auto i : cnt) result = max(result, round(i)); cout << result; return 0; } | cs |