[알고리즘] 백준 4375

dlwl98·2022년 5월 17일
0

알고리즘공부

목록 보기
3/34
post-thumbnail

백준 4375번

해결 과정 요약

자릿수를 증가시켜가며 나누어 떨어지는지 확인한다.

풀이

#include <bits/stdc++.h>
using namespace std;

int com;
long long ret, n;

int main(){
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    while(cin >> com){
        n = 1; ret = 1;
        while(n%com != 0){
            n = 10*n + 1;
            n %= com;
            ret++;
        }
        cout << ret << "\n";
    }
    return 0;
}

트러블슈팅

  • 계속 자릿수를 늘려가다보니 타입의 범위를 초과하는 문제가 발생.
    • while문을 돌면서 % 연산을 통해 수의 크기를 줄여주는 방식으로 해결.

0개의 댓글