본문 바로가기

Algorithm/Brute Force

(C++) - 백준(BOJ) 9161 : Sir Bedavere’s Bogus Division Solutions

반응형

https://www.acmicpc.net/problem/9161

 

9161번: Sir Bedavere’s Bogus Division Solutions

The wise Sir Bedavere often uses non-standard logic, yet achieves positive results. Well, it seems he has been at it again, this time with division. He has determined that canceling the common digit of a numerator and denominator produces the correct answe

www.acmicpc.net

전수조사 문제였습니다.

📕 풀이방법

📔 입력 및 초기화

사소한 경우(111의 배수)를 거르기 위해 trivialNum을 선언 후 111로 초기화해줍니다.

📔 풀이과정

분모와 분자의 세 자리들을 for loop를 수행하며 모두 탐색합니다.

두 가지의 경우에 해당하면 정답입니다.

1. 분자 / 분모 = 최 우측 자리 수 제거한 분자 / 최 좌측 자리 수 제거한 분모 이므로 분모 위치를 옮기면 다음과 같습니다.

분자 * 최 좌측 자리 수 제거한 분모 = 분모 * 최 우측 자리 수 제거한 분자

2. 제거한 자리들은 수가 같아야 합니다.

* 분모 분자가 모두 111의 배수라면 정답이 아니므로 continue해줍니다.

📔 정답출력

형식에 맞게 출력해줍니다.


📕 Code

'

#include <bits/stdc++.h>
using namespace std;
int trivialNum = 111;
int main(){
  for(int top = 100; top <= 999; top++){
    for(int bottom = 100; bottom <= 999; bottom++){
      if(top % trivialNum == 0 && bottom % trivialNum == 0) continue;
      if(bottom*(top / 10) == top*(bottom % 100) && (top % 10) == (bottom /100))
        printf("%d / %d = %d / %d\n", top, bottom, top/10, bottom%100);
    }
  }
}

*더 나은 내용을 위한 지적, 조언은 언제나 환영합니다.