11 | 브루트 포스 | 가장 간단한 알고리즘인, 모든 경우의 수를 검사하는 브루트 포스 알고리즘을 배워 봅시다. |
1 | 2798 | 블랙잭 |
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scan = new Scanner(System.in);
int T = scan.nextInt();
int Max = scan.nextInt();
int n[] = new int [T];
for(int i = 0; i<T; i++)
n[i] = scan.nextInt();
scan.close();
int sum = 0;
for(int i= 0 ; i<T-2; i++) {
for(int j= i+1; j<T-1; j++) {
for(int z=j+1; z<T; z++) {
if(n[i]+n[j]+n[z] <= Max && n[i]+n[j]+n[z] > sum)
sum = n[i]+n[j]+n[z];
}
}
}
System.out.println(sum);
}
}
2 | 2231 | 분해합 |
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int N = scan.nextInt();
scan.close();
int min = N;
for(int i = N-1; i>0; i--) {
int test = i;
int sum = 0;
while(test >0) {
sum = sum + test%10;
test = test/10;
}
if(sum + i == N && i < min)
min = i;
}
if( min == N) System.out.println(0);
else System.out.println(min);
}
}
3 | 7568 | 덩치 |
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int N = scan.nextInt();
int people[][] = new int [N][2];
for(int i=0; i<N; i++) {
people[i][0] = scan.nextInt();
people[i][1] = scan.nextInt();
}
scan.close();
for(int i=0; i<N; i++) {
int rank = 1;
for(int j=0; j<N; j++)
if(people[i][0] < people[j][0] && people[i][1] < people[j][1])
rank++;
System.out.println(rank);
}
}
}
4 | 1018 | 체스판 다시 칠하기 |
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int N = scan.nextInt();
int M = scan.nextInt();
scan.nextLine();
StringBuffer sbf;
String test[] = new String[N]; //줄단위로 입력을 먼저 받고
for(int i=0; i<N; i++)
test[i] = scan.nextLine();
boolean chess[][] = new boolean [N][M]; // 2차원 배열만들고
for(int i=0; i<N; i++) { //한단어씩 2차원배열에 넣고 체스판 그림.
sbf = new StringBuffer(test[i]);
for(int j=0; j<M; j++) {
if(sbf.charAt(j) == 'W') chess[i][j] = true;
else chess[i][j] = false;
}
}
int min = 64;
for(int i=0; i<=N-8; i++) {
for(int j=0; j<=M-8; j++) {
boolean TF = chess[i][j];
int count = 0;
for(int x = i; x< i+8; x++) {
for(int y= j; y< j+8; y++) {
if(chess[x][y] != TF) count++;
TF = !TF;
}
TF = !TF;
}
count =Math.min(count, 64-count);
if(min > count) min = count;
}
}
System.out.println(min);
}
}
5 | 1436 | 영화감독 숌 |
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int T = scan.nextInt();
scan.close();
int count = 0;
int i = 0;
while(T != count) {
i++;
if(String.valueOf(i).contains("666") == true) count++;
}
System.out.println(i);
}
}
728x90
반응형