https://www.acmicpc.net/problem/1526
์ฝ๋
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);
}
}
์ฌ๊ท ์ฝ๋๋ ๋ด๋ ์ดํด๊ฐ ์๋๋๋ฐ ์ ๋ ๊ฒ ์ฌ๊ท์ ์ผ๋ก ์ง๋ ค๋ฉด ์ผ๋ง๋ ๋ ์ฐ์ตํด์ผํ ๊น .............
'Algorithm' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Codeforces] 509A: Maximum in Table (0) | 2020.03.05 |
---|---|
[๋ฐฑ์ค] 1551๋ฒ: ์์ด์ ๋ณํ(์ํ, ์๋ฎฌ๋ ์ด์ ) (0) | 2020.03.04 |
[๋ฐฑ์ค] 2563๋ฒ: ์์ข ์ด(์์ ํ์, ๊ตฌํ) (0) | 2020.03.04 |
[๋ฐฑ์ค] 1789๋ฒ: ์๋ค์ ํฉ(๊ตฌํ) (0) | 2020.03.04 |
[๋ฐฑ์ค] 1764๋ฒ: ๋ฃ๋ณด์ก(๊ตฌํ, ์ ๋ ฌ) - HashSet (0) | 2020.03.04 |
๋๊ธ