본문 바로가기

반응형

알고리즘/알고리즘 문제해결전략

(5)
예제 : 수열의 빠른 합 1 2 3 4 5 6 7 8 9 10 11 12 13 public class FastSum { public static void main(String[] args) { System.out.println(fastSum(10)); } public static int fastSum(int n) { if(n == 1) { return 1; } if(n%2 == 1) return fastSum(n-1) + n; return 2*fastSum(n/2)+n/2*n/2; } } Colored by Color Scripter cs 1 + 2 + 3 + 4 + 5 + ... + n 을 반으로 나누어서 계산해 보자 1 + 2 + 3 + ... n/2 + (n/2+2) + (n/2+3) + (n/2+4) + (n/2+n/2)..
문제 ID : CLOCKSYNC, 난이도: 중 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 package main; import java.util.ArrayList; import java.util.Scanner; public class MainClass { final static int INF = 9999; final static int SWITCHES = 10; final stat..
예제 : 여행하는 외판원 어떤 나라에 n(2
문제 ID : BOARDCOVER , 난이도: 하 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 package test; import java.util.Scanner; public class test{ public static void main(String[] args) { int board[][]; Scanner scanner = new Scanner(System.in); int test = scanner.nextInt(); for(int i = 0..
문제 ID : PICNIC, 난이도: 하 picnic 소풍 항상 친구인 학생들 끼리만 짝을 지어주는 프로그램 /* 테스트 케이스 : C 학생 수 : N 학생 짝의 수 : M 서로 친구인 학생 쌍이 주어진다. */ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 #include using namespace std; int dfs(int taken[11]); int dfs2(int taken[11]); int taken[11] = {0,}; int checkFriend[11][11] = {0,}; int C,N,M = 0; int main(){ cin >> C; for(int i = 0; i > N >> M; for(int j = 0; j > friend1 >> friend..

반응형