๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Algorithm

[๋ฐฑ์ค€] 1526๋ฒˆ: ๊ฐ€์žฅ ํฐ ๊ธˆ๋ฏผ์ˆ˜(์‹œ๋ฎฌ๋ ˆ์ด์…˜)

by ์ฃผ๋ฐœ2 2020. 3. 4.
๋ฐ˜์‘ํ˜•

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

 

1526๋ฒˆ: ๊ฐ€์žฅ ํฐ ๊ธˆ๋ฏผ์ˆ˜

์ฒซ์งธ ์ค„์— N์ด ์ฃผ์–ด์ง„๋‹ค. N์€ 4๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™๊ณ  1,000,000๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ์ž์—ฐ์ˆ˜์ด๋‹ค.

www.acmicpc.net

์ฝ”๋“œ

import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		int N = scan.nextInt();

		// N ๋ถ€ํ„ฐ ๋งŒ์กฑํ•˜๋Š” ์ˆ˜๊นŒ์ง€ ๊ฐ์†Œํ•˜๋ฉด์„œ ์ฐพ๋Š”๋‹ค.
		for(int i=N; i>=4; i--) {
			boolean flag = true;
			int num = i;
			while(num != 0) {
				// num์˜ ๋์— ์ž๋ฆฌ๊ฐ€ 4์ด๊ฑฐ๋‚˜ 7์ผ๊ฒฝ์šฐ => ๋‹ค์Œ ์ž๋ฆฌ์ˆ˜๋ฅผ ์ฐพ๊ธฐ ์œ„ํ•ด 10์œผ๋กœ ๋‚˜๋ˆ”
				if(num % 10 == 4 || num % 10 == 7)
					num /= 10;
				// num์ด 4๋‚˜ 7๋กœ ๋‚˜๋ˆ„์–ด ๋–จ์–ด์ง€์ง€ ์•Š๋Š”๊ฒฝ์šฐ => ์กฐ๊ฑด ๋งŒ์กฑ X, ๋‹ค์Œ์ˆ˜ ์ฐพ๊ธฐ ์œ„ํ•ด while๋ฌธ ํƒˆ์ถœ
				else {
					flag = false;
					break;
				}
			}
			
			if(flag) {
				System.out.println(i);
				break;
			}
		}
		scan.close();
	}

}

ํ’€์ด

๋‹จ์ˆœํ•œ ๊ตฌํ˜„๋ฌธ์ œ ์ธ ์ค„ ์•Œ์•˜์œผ๋‚˜ ... ์ƒ๊ฐ๋ณด๋‹ค ๋งŽ์ด ํ—ค๋งค์—ˆ๋‹ค.

์ฃผ์–ด์ง„ N์„ ๋งŒ์กฑํ•˜๋Š” ๊ฐ€์žฅ ํฐ ์ˆ˜๋ฅผ ์ฐพ์„๋•Œ๊นŒ์ง€ ๊ฐ’์„ ์ค„์—ฌ๊ฐ€๋ฉฐ ์™„์ „ ํƒ์ƒ‰์„ ํ•œ๋‹ค๋Š” ํ’€์ด๊ฐ€ ์žˆ์–ด์„œ ๊ทธ๋Œ€๋กœ ํ•ด๋ดค๋‹ค.

์ž…๋ ฅ์˜ˆ์ œ๋Œ€๋กœ 100์„ ์˜ˆ์‹œ๋กœ ๋“ค์–ด๋ณด๋ฉด,

100๋ถ€ํ„ฐ 1์”ฉ ๊ฐ์†Œํ•˜๋ฉฐ for๋ฌธ์„ ๋„๋Š”๋ฐ, 

์ผ์˜์ž๋ฆฌ๊ฐ€ 4๋‚˜ 7์ผ๊ฒฝ์šฐ ๋‹ค์‹œ 10์„ ๋‚˜๋ˆ ์„œ ๊ทธ ๋‹ค์Œ ์ž๋ฆฌ๋„ 4๋‚˜ 7์ธ์ง€ ํŒ๋ณ„ํ•œ๋‹ค.

๋งŒ์•ฝ num์ด 0์ด ๋ ๋•Œ๊นŒ์ง€ 4๋‚˜ 7๋กœ ๋‚˜๋ˆ„์–ด๋–จ์–ด์ง„๋‹ค๋ฉด ๊ทธ ์ˆ˜๊ฐ€ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ์ตœ๋Œ“๊ฐ’์ด ๋œ๋‹ค.

100 -> 99 -> 98 -> 97

97 % 10 == 7  ---> num /= 10 (num = 9)

9%10 == 9 ์ด๋ฏ€๋กœ, while๋ฌธ์„ ๋น ์ ธ๋‚˜์˜ค๊ณ , ๋‹ค์Œ for๋ฌธ์„ ์ง„ํ–‰ํ•˜๊ฒŒ ๋œ๋‹ค.

 

 

๋‹ค๋ฅธ ํ’€์ด๋“ค(ํ , ์žฌ๊ท€)

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Queue;

public class Main {

	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		String tmp = in.readLine();
		int num = Integer.parseInt(tmp);
		Queue<Integer> q = new LinkedList<Integer>();
		int ans = 0;
		if (num >= 4) {
			q.add(4);
			ans = 4;
		}
		if (num >= 7) {
			q.add(7);
			ans = 7;
		}
		while (!q.isEmpty()) {
			int tmp1 = q.peek();
			q.poll();
			tmp1 *= 10;
			if (tmp1 + 4 <= num) {
				ans = tmp1 + 4;
				q.add(ans);
			}
			if (tmp1 + 7 <= num) {
				ans = tmp1 + 7;
				q.add(ans);
			}
		}
		System.out.println(ans);
	}

}

 

 

import java.util.Scanner;
public class Main {
    static int N;
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        N = sc.nextInt();
        System.out.println(DFS(0));
    }

    static int DFS(int number) {
        int value=number;
        if(number*10+4 <=N)
            value = DFS(number*10+4);
        if(number*10+7 <=N)
            value = Math.max(value, DFS(number*10+7));
        return value;
    }
}

 

 

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {

	static int result;
	
	static void DFS(int number, int N) {
		if (number > N) return;
		result = Math.max(result, number);
		DFS(number*10 + 4 , N);
		DFS(number*10 + 7 , N);
	}
	public static void main(String[] args) throws IOException {
		
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int N = Integer.parseInt(br.readLine());
		
		DFS(4,N);
		DFS(7,N);
		
		System.out.println(result);
	}
}

์žฌ๊ท€ ์ฝ”๋“œ๋Š” ๋ด๋„ ์ดํ•ด๊ฐ€ ์•ˆ๋˜๋Š”๋ฐ ์ €๋ ‡๊ฒŒ ์žฌ๊ท€์ ์œผ๋กœ ์งœ๋ ค๋ฉด ์–ผ๋งˆ๋‚˜ ๋” ์—ฐ์Šตํ•ด์•ผํ• ๊นŒ .............

 

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€