• toc {:toc}

문제

λ‹€μ†œμ΄λŠ” μ€μ§„μ΄μ˜ μ˜†μ§‘μ— μƒˆλ‘œ 이사왔닀. λ‹€μ†œμ΄λŠ” 자기 λ°© 번호λ₯Ό μ˜ˆμœΒ ν”ŒλΌμŠ€ν‹± 숫자둜 문에 뢙이렀고 ν•œλ‹€.

λ‹€μ†œμ΄μ˜ μ˜†μ§‘μ—μ„œλŠ” ν”ŒλΌμŠ€ν‹± 숫자λ₯Ό ν•œ μ„ΈνŠΈλ‘œ νŒλ‹€. ν•œ μ„ΈνŠΈμ—λŠ” 0λ²ˆλΆ€ν„° 9λ²ˆκΉŒμ§€ μˆ«μžκ°€ ν•˜λ‚˜μ”© λ“€μ–΄μžˆλ‹€. λ‹€μ†œμ΄μ˜ λ°© λ²ˆν˜Έκ°€ μ£Όμ–΄μ‘Œμ„ λ•Œ, ν•„μš”ν•œ μ„ΈνŠΈμ˜ 개수의 μ΅œμ†Ÿκ°’μ„ 좜λ ₯ν•˜μ‹œμ˜€. (6은 9λ₯Ό λ’€μ§‘μ–΄μ„œ μ΄μš©ν•  수 있고, 9λŠ” 6을 λ’€μ§‘μ–΄μ„œ μ΄μš©ν•  수 μžˆλ‹€.)

μž…λ ₯

첫째 쀄에 λ‹€μ†œμ΄μ˜ λ°© 번호 N이 μ£Όμ–΄μ§„λ‹€. N은 1,000,000보닀 μž‘κ±°λ‚˜ 같은 μžμ—°μˆ˜μ΄λ‹€.

좜λ ₯

첫째 쀄에 ν•„μš”ν•œ μ„ΈνŠΈμ˜ 개수λ₯Ό 좜λ ₯ν•œλ‹€.

좜처:https://www.acmicpc.net/problem/1475

풀이

ν’€μ΄μ˜ μ£Όμš” νŠΉμ§• β‡’ 6κ³Ό 9λŠ” ν•¨κ»˜ μ‚¬μš©ν•  수 μžˆλ‹€.

λ”°λΌμ„œ num[6]+num[9]λ₯Ό 톡해 6κ³Ό 9κ°€ λ‚˜μ˜¨ 개수의 ν•©μœΌλ‘œ μ„ΈνŠΈμ˜ 수λ₯Ό μ²˜λ¦¬ν•œλ‹€.

6κ³Ό 9λŠ” μ²˜λ¦¬ν–ˆμœΌλ‹ˆ 0으둜 λ‘”λ‹€.

이후 λ‚˜λ¨Έμ§€ 숫자λ₯Ό μ²˜λ¦¬ν•΄μ€€λ‹€.

#include <bits/stdc++.h>
using namespace std;
 
int num[10];
 
int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	int N, max;
	cin >> N;
	do{
		num[N%10]++;
		N = N/10;
	}while(N!=0);
	
	max = -1;
	
	if(max<(num[6]+num[9])/2){
		if((num[6]+num[9])%2!=0)
			max = (num[6]+num[9]+1)/2;
		else
			max = (num[6]+num[9])/2;
	}
	num[6]=num[9]=0;
	
	for(auto elem : num){
		if(max < elem){
			max = elem;
		}
	}
	cout << max << "\n";
	
	return 0;
}

이 ν’€μ΄λŠ” 6κ³Ό 9의 처리λ₯Ό λ‚˜μ€‘μœΌλ‘œ λ―Έλ€˜λ‹€.

λ―Έλ£¨λŠ” 방법은 continueλ₯Ό μ‚¬μš©ν•˜μ—¬ μ²˜λ¦¬ν–ˆλ‹€.

++ansλŠ” κ²°κ΅­ int이기 λ•Œλ¬Έμ— μ†Œμˆ˜μ μ€ 버렀진닀. 이λ₯Ό μ΄μš©ν•΄ (a[6]+a[9]+1)/2)λ§Œμ„ μ‚¬μš©ν•˜μ—¬ κ°„κ²°ν™” ν•  수 μžˆλ‹€.

// Authored by : OceanShape
// Co-authored by : BaaaaaaaaaaarkingDog
// http://boj.kr/be312f0135b040c0acf8960181870067
#include <bits/stdc++.h>
using namespace std;
 
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  
  // ans = 1둜 λ‘ μœΌλ‘œμ„œ N = 0μΌλ•Œμ— λŒ€ν•œ μ˜ˆμ™Έμ²˜λ¦¬κ°€ μžμ—°μŠ€λŸ½κ²Œ κ°€λŠ₯해짐
  int N, a[10] = {}, ans = 1;
  cin >> N;
  
  // 자리수 μΆ”μΆœ
  while(N){
    a[N%10]++;
    N /= 10;
  }
 
  for(int i = 0; i < 10; i++){
    if(i == 6 || i == 9) continue;
    ans = max(ans, a[i]);
  }
  // (a[6]+a[9])/2λ₯Ό μ˜¬λ¦Όν•œ 값이 6, 9에 λŒ€ν•œ ν•„μš”ν•œ μ„ΈνŠΈμ˜ μˆ˜μ΄λ―€λ‘œ (a[6]+a[9]+1)/2을 계산
  ans = max(ans, (a[6]+a[9]+1)/2);
  cout << ans;
}