브루트포스

[백준] 1526. 가장 큰 금민수 풀이

hch06 2025. 2. 2. 18:07

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

 

n부터 1까지 숫자를 감소시키면서 하나씩 검사한다.
각 자릿수를 검사하면서 4와 7 외의 숫자가 있는지 확인한다.
4와 7로만 이루어진 숫자가 등장하면 반복문 탈출 & 해당 숫자 출력

 


C

더보기
더보기
#include <stdio.h>

int isGmminsu(int v){
    while(v){
        if(v % 10 != 4 && v % 10 != 7) return 0;
        v /= 10;
    }
    return 1;
}

int main() {
    int n; scanf("%d", &n);
    int max = 0;
    for(int i = n; i > 0; i--){
        if(isGmminsu(i)) {max = i; break;}
    }
    printf("%d", max);
}

Python

더보기
더보기
def isGmminsu(v):
    while v:
        if v % 10 != 4 and v % 10 != 7:
            return 0
        v //= 10
    return 1

n = int(input())
max = 0
for i in range(1, n+1):
    if isGmminsu(i):
        if i > max: max = i
print(max)