๋ฐ์ํ
https://www.acmicpc.net/problem/3943
์ฝ๋
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
int T = Integer.parseInt(bf.readLine());
for(int i=0; i<T; i++) {
int n = Integer.parseInt(bf.readLine());
int max = n;
while(n!=1) {
n = (n%2 == 0) ? n/2 : (n*3)+1;
max = Math.max(max, n);
}
System.out.println(max);
}
bf.close();
}
}
ํ์ด
๋ฌธ์ ์ ๋ง๊ฒ ๊ตฌํํด์ฃผ๋ฉด ๋๋ค.
๋ฌธ์ ๋ถ๋ฅ์ DP์ฌ์ ๊ณ์ dp๋ง ์๊ฐํ๋๋ฐ ..
๊ทธ๋ฅ n์ด 1์ด ๋ ๋๊น์ง ์ง์๋ฉด 2๋ก ๋๋๊ณ , ํ์๋ฉด 3์ ๊ณฑํ๊ณ 1์ ๋ํ๋ฉด ๋๋ค.
๊ทธ ํ ์ต๋๊ฐ์ ์ฐพ์์ฃผ๋ฉด ๋.
+ System.out.println() ๋์ BufferedWriter๋ฅผ ์ฌ์ฉํ๋ฉด ์๊ฐ์ด 1/3์ผ๋ก ์ค์ด๋ ๋ค.
BufferedWriter ์ฝ๋
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int T = Integer.parseInt(bf.readLine());
for(int i=0; i<T; i++){
int n = Integer.parseInt(bf.readLine());
int max = n;
while(n!=1) {
n = (n%2 == 0) ? n/2 : (n*3)+1;
max = Math.max(n, max);
}
bw.write(max + "\n");
}
bw.flush();
bw.close();
bf.close();
}
}
๋ฐ์ํ
'Algorithm' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Codeforces] 1186A: Vus the Cossack and a Contest (0) | 2020.02.23 |
---|---|
[Codeforces] 959A: Mahmoud and Ehab and the even-odd game (0) | 2020.02.22 |
[๋ฐฑ์ค] 1592๋ฒ: ์์์ด์ ์น๊ตฌ๋ค(๊ตฌํ, ์ํ, ์๋ฎฌ๋ ์ด์ ) (0) | 2020.02.21 |
[Codeforces] 935A: Fafa and his Company(brute force) (0) | 2020.02.21 |
[๋ฐฑ์ค] 2579๋ฒ: ๊ณ๋จ ์ค๋ฅด๊ธฐ(DP, ๋์ ๊ณํ๋ฒ) (0) | 2020.02.21 |
๋๊ธ