-
[배열]-기본-방 번호카테고리 없음 2022. 11. 18. 13:44
< 배열 > - < 실버 5 >
https://www.acmicpc.net/problem/1475
1475번: 방 번호
첫째 줄에 다솜이의 방 번호 N이 주어진다. N은 1,000,000보다 작거나 같은 자연수이다.
www.acmicpc.net
[ 1475 ] 방 번호
다솜이는 은진이의 옆집에 새로 이사왔다. 다솜이는 자기 방 번호를 예쁜 플라스틱 숫자로 문에 붙이려고 한다.
다솜이의 옆집에서는 플라스틱 숫자를 한 세트로 판다. 한 세트에는 0번부터 9번까지 숫자가 하나씩 들어있다. 다솜이의 방 번호가 주어졌을 때, 필요한 세트의 개수의 최솟값을 출력하시오. (6은 9를 뒤집어서 이용할 수 있고, 9는 6을 뒤집어서 이용할 수 있다.)
:: 입력 ::
첫째 줄에 다솜이의 방 번호 N이 주어진다. N은 1,000,000보다 작거나 같은 자연수이다.
:: 출력 ::
첫째 줄에 필요한 세트의 개수를 출력한다.
6이랑 9를 처리하는 공식을 잘못 세워서 3번인가 틀림정답 코드
#include <iostream> #include <algorithm> #include <vector> #include <string> using namespace std; int main() { ios::sync_with_stdio(0); cin.tie(0); int n, ans =1; cin >> n; int nums[10] = { 0, }; while (n) { nums[n % 10]++; n /= 10; } for (int i = 0; i < 10; i++){ if (i == 6 || i == 9) { continue; } ans = max(ans, nums[i]); } ans = max(ans, (nums[6] + nums[9] + 1) / 2); cout << ans; }
221215
다시 풀어봄 !
6이랑 9를 잘 처리하지 못해서 틀렸던 게 기억나서
공식을 먼저 세운뒤에 풀었고 한번에 맞음 !#include <iostream> #include <vector> #include <string> using namespace std; int nums[10]; int main() { int set = 0; string n; cin >> n; for (char c : n) { int k = c - '0'; nums[k]++; } for (int i = 0; i < 10; i++) { if (i == 6 || i == 9)continue; if (set < nums[i]) set = nums[i]; } if (set < ((nums[6] + nums[9] + 1) / 2)) set = ((nums[6] + nums[9] + 1) / 2); cout << set; }