├── .gitignore ├── README.md ├── cover.jpg ├── 문제풀이 ├── chap2-1 │ ├── prob2-1-3.c │ ├── prob2-1-3.cpp │ ├── prob2-1-3.java │ └── prob2-1-3.py ├── chap2-2 │ ├── prob2-2-4.c │ ├── prob2-2-4.cpp │ ├── prob2-2-4.java │ └── prob2-2-4.py ├── chap2-5 │ ├── prob2-5-3.c │ ├── prob2-5-3.cpp │ ├── prob2-5-3.java │ ├── prob2-5-3.py │ ├── prob2-5-4.c │ ├── prob2-5-4.cpp │ ├── prob2-5-4.java │ └── prob2-5-4.py ├── chap3-1 │ ├── prob3-1-2.c │ ├── prob3-1-2.cpp │ ├── prob3-1-2.java │ └── prob3-1-2.py ├── chap3-2 │ ├── prob3-2-2.c │ ├── prob3-2-2.cpp │ ├── prob3-2-2.java │ ├── prob3-2-2.py │ ├── prob3-2-3.c │ ├── prob3-2-3.cpp │ ├── prob3-2-3.java │ └── prob3-2-3.py ├── chap3-3 │ ├── prob3-3-3.c │ ├── prob3-3-3.cpp │ ├── prob3-3-3.java │ ├── prob3-3-3.py │ ├── prob3-3-4.c │ ├── prob3-3-4.cpp │ ├── prob3-3-4.java │ ├── prob3-3-4.py │ ├── prob3-3-5.c │ ├── prob3-3-5.cpp │ ├── prob3-3-5.java │ ├── prob3-3-5.py │ ├── prob3-3-6.c │ ├── prob3-3-6.cpp │ ├── prob3-3-6.java │ └── prob3-3-6.py ├── chap3-4 │ ├── prob3-4-3.c │ ├── prob3-4-3.cpp │ ├── prob3-4-3.java │ ├── prob3-4-3.py │ ├── prob3-4-4.c │ ├── prob3-4-4.cpp │ ├── prob3-4-4.java │ └── prob3-4-4.py ├── chap3-5 │ ├── prob3-5-2.c │ ├── prob3-5-2.cpp │ ├── prob3-5-2.java │ └── prob3-5-2.py ├── chap3-6 │ ├── prob3-6-3.c │ ├── prob3-6-3.cpp │ ├── prob3-6-3.java │ └── prob3-6-3.py ├── chap3-7 │ ├── prob3-7-4.c │ ├── prob3-7-4.cpp │ ├── prob3-7-4.java │ ├── prob3-7-4.py │ ├── prob3-7-6.c │ ├── prob3-7-6.cpp │ ├── prob3-7-6.java │ └── prob3-7-6.py ├── chap4-1 │ ├── prob4-1-2.c │ ├── prob4-1-2.cpp │ ├── prob4-1-2.java │ ├── prob4-1-2.py │ ├── prob4-1-3.c │ ├── prob4-1-3.cpp │ ├── prob4-1-3.java │ ├── prob4-1-3.py │ ├── prob4-1-4.c │ ├── prob4-1-4.cpp │ ├── prob4-1-4.java │ ├── prob4-1-4.py │ ├── prob4-1-5.c │ ├── prob4-1-5.cpp │ ├── prob4-1-5.java │ └── prob4-1-5.py ├── chap4-2 │ ├── prob4-2-1.c │ ├── prob4-2-1.cpp │ ├── prob4-2-1.java │ ├── prob4-2-1.py │ ├── prob4-2-2.c │ ├── prob4-2-2.cpp │ ├── prob4-2-2.java │ └── prob4-2-2.py ├── chap4-3 │ ├── prob4-3-2.c │ ├── prob4-3-2.cpp │ ├── prob4-3-2.java │ ├── prob4-3-2.py │ ├── prob4-3-3.c │ ├── prob4-3-3.cpp │ ├── prob4-3-3.java │ └── prob4-3-3.py ├── chap4-4 │ ├── prob4-4-2.c │ ├── prob4-4-2.cpp │ ├── prob4-4-2.java │ ├── prob4-4-2.py │ ├── prob4-4-3.c │ ├── prob4-4-3.cpp │ ├── prob4-4-3.java │ ├── prob4-4-3.py │ ├── prob4-4-3_fast.py │ ├── prob4-4-4.c │ ├── prob4-4-4.cpp │ ├── prob4-4-4.java │ └── prob4-4-4.py ├── chap4-5 │ ├── prob4-5-5.c │ ├── prob4-5-5.cpp │ ├── prob4-5-5.java │ ├── prob4-5-5.py │ ├── prob4-5-6.c │ ├── prob4-5-6.cpp │ ├── prob4-5-6.java │ ├── prob4-5-6.py │ ├── prob4-5-7.c │ ├── prob4-5-7.cpp │ ├── prob4-5-7.java │ ├── prob4-5-7.py │ ├── prob4-5-8.cpp │ ├── prob4-5-8.java │ └── prob4-5-8.py ├── chap4-6 │ ├── prob4-6-2.c │ ├── prob4-6-2.cpp │ ├── prob4-6-2.java │ ├── prob4-6-2.py │ ├── prob4-6-3.c │ ├── prob4-6-3.cpp │ ├── prob4-6-3.java │ └── prob4-6-3.py ├── chap4-7 │ ├── prob4-7-2.c │ ├── prob4-7-2.cpp │ ├── prob4-7-2.java │ ├── prob4-7-2.py │ ├── prob4-7-3.c │ ├── prob4-7-3.cpp │ ├── prob4-7-3.java │ ├── prob4-7-3.py │ ├── prob4-7-4.c │ ├── prob4-7-4.cpp │ ├── prob4-7-4.java │ └── prob4-7-4.py ├── chap5-10 │ ├── prob5-10-2.c │ ├── prob5-10-2.cpp │ ├── prob5-10-2.java │ ├── prob5-10-2.py │ ├── prob5-10-3.c │ ├── prob5-10-3.cpp │ ├── prob5-10-3.java │ ├── prob5-10-3.py │ ├── prob5-10-5.c │ ├── prob5-10-5.cpp │ ├── prob5-10-5.java │ ├── prob5-10-5.py │ ├── prob5-10-6.c │ ├── prob5-10-6.cpp │ ├── prob5-10-6.java │ └── prob5-10-6.py ├── chap5-2 │ ├── prob5-2-2.c │ ├── prob5-2-2.cpp │ ├── prob5-2-2.java │ ├── prob5-2-2.py │ ├── prob5-2-3.c │ ├── prob5-2-3.cpp │ ├── prob5-2-3.java │ └── prob5-2-3.py ├── chap5-3 │ ├── prob5-3-1.c │ ├── prob5-3-1.cpp │ ├── prob5-3-1.java │ └── prob5-3-1.py ├── chap5-4 │ ├── prob5-4-2.c │ ├── prob5-4-2.cpp │ ├── prob5-4-2.java │ ├── prob5-4-2.py │ ├── prob5-4-5.c │ ├── prob5-4-5.cpp │ ├── prob5-4-5.java │ └── prob5-4-5.py ├── chap5-5 │ ├── prob5-5-2.c │ ├── prob5-5-2.cpp │ ├── prob5-5-2.java │ └── prob5-5-2.py ├── chap5-6 │ ├── prob5-6-2.c │ ├── prob5-6-2.cpp │ ├── prob5-6-2.java │ └── prob5-6-2.py ├── chap5-7 │ ├── prob5-7-3.c │ ├── prob5-7-3.cpp │ ├── prob5-7-3.java │ ├── prob5-7-3.py │ ├── prob5-7-4.c │ ├── prob5-7-4.cpp │ ├── prob5-7-4.java │ └── prob5-7-4.py ├── chap5-8 │ ├── prob5-8-2.c │ ├── prob5-8-2.cpp │ ├── prob5-8-2.java │ ├── prob5-8-2.py │ ├── prob5-8-3.cpp │ ├── prob5-8-3.java │ └── prob5-8-3.py ├── chap5-9 │ ├── prob5-9-1.c │ ├── prob5-9-1.cpp │ ├── prob5-9-1.java │ ├── prob5-9-1.py │ ├── prob5-9-2.c │ ├── prob5-9-2.cpp │ ├── prob5-9-2.java │ ├── prob5-9-2.py │ ├── prob5-9-3.c │ ├── prob5-9-3.cpp │ ├── prob5-9-3.java │ └── prob5-9-3.py ├── chap6-16_20 │ ├── prob6-16.c │ ├── prob6-16.cpp │ ├── prob6-16.java │ ├── prob6-16.py │ ├── prob6-17.c │ ├── prob6-17.cpp │ ├── prob6-17.java │ ├── prob6-17.py │ ├── prob6-18.c │ ├── prob6-18.cpp │ ├── prob6-18.java │ ├── prob6-18.py │ ├── prob6-19.c │ ├── prob6-19.cpp │ ├── prob6-19.java │ ├── prob6-19.py │ ├── prob6-20.c │ ├── prob6-20.cpp │ ├── prob6-20.java │ └── prob6-20.py ├── chap6-21_25 │ ├── prob6-21.c │ ├── prob6-21.cpp │ ├── prob6-21.java │ ├── prob6-21.py │ ├── prob6-22.c │ ├── prob6-22.cpp │ ├── prob6-22.java │ ├── prob6-22.py │ ├── prob6-23.c │ ├── prob6-23.cpp │ ├── prob6-23.java │ ├── prob6-23.py │ ├── prob6-24.c │ ├── prob6-24.cpp │ ├── prob6-24.java │ ├── prob6-24.py │ ├── prob6-25.c │ ├── prob6-25.cpp │ ├── prob6-25.java │ └── prob6-25.py └── chap6-26_30 │ ├── prob6-26.c │ ├── prob6-26.cpp │ ├── prob6-26.java │ ├── prob6-26.py │ ├── prob6-27.c │ ├── prob6-27.cpp │ ├── prob6-27.java │ ├── prob6-27.py │ ├── prob6-28.c │ ├── prob6-28.cpp │ ├── prob6-28.java │ └── prob6-28.py └── 본문 ├── c ├── Code_2_01_1.c ├── Code_2_01_2.c ├── Code_2_01_3.c ├── Code_2_01_4.c ├── Code_2_02_1.c ├── Code_2_02_2.c ├── Code_2_03_1.c ├── Code_2_04_1.c ├── Code_2_04_2.c ├── Code_2_04_3.c ├── Code_2_06_1.c ├── Code_3_01_1.c ├── Code_3_01_2.c ├── Code_3_01_3.c ├── Code_3_02_1.c ├── Code_3_02_2.c ├── Code_3_03_1.c ├── Code_3_04_1.c ├── Code_3_04_2.c ├── Code_3_05_1.c ├── Code_3_06_1.c ├── Code_3_06_2.c ├── Code_3_06_3.c ├── Code_3_06_4.c ├── Code_3_06_5.c ├── Code_3_06_6.c ├── Code_3_07_1.c ├── Code_3_07_2.c ├── Code_3_07_3.c ├── Code_3_08_1.c ├── Code_4_01_1.c ├── Code_4_02_1.c ├── Code_4_02_2.c ├── Code_4_03_1.c ├── Code_4_04_1.c ├── Code_4_05_1.c ├── Code_4_05_2.c ├── Code_4_05_2_stack.c ├── Code_4_05_3.c ├── Code_4_06_1.c ├── Code_4_06_2.c ├── Code_4_06_3.c ├── Code_4_06_4.c ├── Code_4_06_5.c ├── Code_4_06_6.c ├── Code_4_07_1.c ├── Code_5_02_1.c ├── Code_5_02_2.c ├── Code_5_03_1.c ├── Code_5_03_2.c ├── Code_5_04_1.c ├── Code_5_05_1.c ├── Code_5_05_2.c ├── Code_5_06_1.c ├── Code_5_07_1.c ├── Code_5_07_2.c ├── Code_5_09_1.c ├── Code_5_09_2.c ├── Code_5_10_1.c ├── Code_5_10_2.c ├── Code_5_10_3.c └── Code_5_10_4.c ├── cpp ├── Code_2_01_1.cpp ├── Code_2_01_2.cpp ├── Code_2_01_3.cpp ├── Code_2_01_4.cpp ├── Code_2_02_1.cpp ├── Code_2_02_2.cpp ├── Code_2_03_1.cpp ├── Code_2_04_1.cpp ├── Code_2_04_2.cpp ├── Code_2_04_3.cpp ├── Code_2_06_1.cpp ├── Code_3_01_1.cpp ├── Code_3_01_2.cpp ├── Code_3_01_3.cpp ├── Code_3_02_1.cpp ├── Code_3_02_2.cpp ├── Code_3_03_1.cpp ├── Code_3_04_1.cpp ├── Code_3_04_2.cpp ├── Code_3_05_1.cpp ├── Code_3_06_1.cpp ├── Code_3_06_2.cpp ├── Code_3_06_3.cpp ├── Code_3_06_4.cpp ├── Code_3_06_5.cpp ├── Code_3_06_6.cpp ├── Code_3_07_1.cpp ├── Code_3_07_2.cpp ├── Code_3_07_3.cpp ├── Code_3_08_1.cpp ├── Code_4_01_1.cpp ├── Code_4_02_1.cpp ├── Code_4_02_2.cpp ├── Code_4_03_1.cpp ├── Code_4_04_1.cpp ├── Code_4_05_1.cpp ├── Code_4_05_2.cpp ├── Code_4_05_2_stack.cpp ├── Code_4_05_3.cpp ├── Code_4_06_1.cpp ├── Code_4_06_2.cpp ├── Code_4_06_3.cpp ├── Code_4_06_4.cpp ├── Code_4_06_5.cpp ├── Code_4_06_6.cpp ├── Code_4_07_1.cpp ├── Code_5_02_1.cpp ├── Code_5_02_2.cpp ├── Code_5_03_1.cpp ├── Code_5_03_2.cpp ├── Code_5_04_1.cpp ├── Code_5_05_1.cpp ├── Code_5_05_2.cpp ├── Code_5_06_1.cpp ├── Code_5_07_1.cpp ├── Code_5_07_2.cpp ├── Code_5_09_1.cpp ├── Code_5_09_2.cpp ├── Code_5_10_1.cpp ├── Code_5_10_2.cpp ├── Code_5_10_3.cpp └── Code_5_10_4.cpp ├── java ├── Code_2_01_1.java ├── Code_2_01_2.java ├── Code_2_01_3.java ├── Code_2_01_4.java ├── Code_2_02_1.java ├── Code_2_02_2.java ├── Code_2_03_1.java ├── Code_2_04_1.java ├── Code_2_04_2.java ├── Code_2_04_3.java ├── Code_2_06_1.java ├── Code_3_01_1.java ├── Code_3_01_2.java ├── Code_3_01_3.java ├── Code_3_02_1.java ├── Code_3_02_2.java ├── Code_3_03_1.java ├── Code_3_04_1.java ├── Code_3_04_2.java ├── Code_3_05_1.java ├── Code_3_06_1.java ├── Code_3_06_2.java ├── Code_3_06_3.java ├── Code_3_06_4.java ├── Code_3_06_5.java ├── Code_3_06_6.java ├── Code_3_07_1.java ├── Code_3_07_2.java ├── Code_3_07_3.java ├── Code_3_08_1.java ├── Code_4_01_1.java ├── Code_4_02_1.java ├── Code_4_02_2.java ├── Code_4_03_1.java ├── Code_4_04_1.java ├── Code_4_05_1.java ├── Code_4_05_2.java ├── Code_4_05_2_stack.java ├── Code_4_05_3.java ├── Code_4_06_1.java ├── Code_4_06_2.java ├── Code_4_06_3.java ├── Code_4_06_4.java ├── Code_4_06_5.java ├── Code_4_06_6.java ├── Code_4_07_1.java ├── Code_5_02_1.java ├── Code_5_02_2.java ├── Code_5_03_1.java ├── Code_5_03_2.java ├── Code_5_04_1.java ├── Code_5_05_1.java ├── Code_5_05_2.java ├── Code_5_06_1.java ├── Code_5_07_1.java ├── Code_5_07_2.java ├── Code_5_09_1.java ├── Code_5_09_2.java ├── Code_5_10_1.java ├── Code_5_10_2.java ├── Code_5_10_3.java └── Code_5_10_4.java └── python ├── Code_2_01_1.py ├── Code_2_01_2.py ├── Code_2_01_3.py ├── Code_2_01_4.py ├── Code_2_02_1.py ├── Code_2_02_2.py ├── Code_2_03_1.py ├── Code_2_04_1.py ├── Code_2_04_2.py ├── Code_2_04_3.py ├── Code_2_06_1.py ├── Code_3_01_1.py ├── Code_3_01_2.py ├── Code_3_01_3.py ├── Code_3_02_1.py ├── Code_3_02_2.py ├── Code_3_03_1.py ├── Code_3_04_1.py ├── Code_3_04_2.py ├── Code_3_05_1.py ├── Code_3_06_1.py ├── Code_3_06_2.py ├── Code_3_06_3.py ├── Code_3_06_4.py ├── Code_3_06_5.py ├── Code_3_06_6.py ├── Code_3_07_1.py ├── Code_3_07_2.py ├── Code_3_07_3.py ├── Code_3_08_1.py ├── Code_4_01_1.py ├── Code_4_02_1.py ├── Code_4_02_2.py ├── Code_4_03_1.py ├── Code_4_04_1.py ├── Code_4_05_1.py ├── Code_4_05_2.py ├── Code_4_05_2_stack.py ├── Code_4_05_3.py ├── Code_4_06_1.py ├── Code_4_06_2.py ├── Code_4_06_3.py ├── Code_4_06_4.py ├── Code_4_06_4_extra.py ├── Code_4_06_5.py ├── Code_4_06_6.py ├── Code_4_07_1.py ├── Code_5_02_1.py ├── Code_5_02_2.py ├── Code_5_03_1.py ├── Code_5_03_2.py ├── Code_5_04_1.py ├── Code_5_05_1.py ├── Code_5_05_2.py ├── Code_5_06_1.py ├── Code_5_07_1.py ├── Code_5_07_2.py ├── Code_5_09_1.py ├── Code_5_09_2.py ├── Code_5_10_1.py ├── Code_5_10_2.py ├── Code_5_10_3.py ├── Code_5_10_3_extra.py └── Code_5_10_4.py /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store -------------------------------------------------------------------------------- /cover.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wikibook/algorithm-math/d67215d3e8f9a6d91cadc9bd496965b01900efad/cover.jpg -------------------------------------------------------------------------------- /문제풀이/chap2-1/prob2-1-3.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | // 입력 5 | int A1, A2, A3; 6 | scanf("%d%d%d", &A1, &A2, &A3); 7 | 8 | // 출력 9 | printf("%d\n", A1 * A2 * A3); 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /문제풀이/chap2-1/prob2-1-3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | // 입력 6 | int A1, A2, A3; 7 | cin >> A1 >> A2 >> A3; 8 | 9 | // 출력 10 | cout << A1 * A2 * A3 << endl; 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /문제풀이/chap2-1/prob2-1-3.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Main { 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | 7 | // 입력 8 | int A1 = sc.nextInt(); 9 | int A2 = sc.nextInt(); 10 | int A3 = sc.nextInt(); 11 | 12 | // 출력 13 | System.out.println(A1 * A2 * A3); 14 | } 15 | }; 16 | -------------------------------------------------------------------------------- /문제풀이/chap2-1/prob2-1-3.py: -------------------------------------------------------------------------------- 1 | A1, A2, A3 = map(int, input().split()) 2 | print(A1 * A2 * A3) 3 | -------------------------------------------------------------------------------- /문제풀이/chap2-2/prob2-2-4.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int N, A[109]; 4 | int Answer = 0; 5 | 6 | int main() { 7 | // 입력 8 | scanf("%d", &N); 9 | for (int i = 1; i <= N; i++) scanf("%d", &A[i]); 10 | 11 | // 답 계산 12 | for (int i = 1; i <= N; i++) { 13 | Answer += A[i]; 14 | } 15 | 16 | // 출력 17 | printf("%d\n", Answer % 100); 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /문제풀이/chap2-2/prob2-2-4.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int N, A[109]; 5 | int Answer = 0; 6 | 7 | int main() { 8 | // 입력 9 | cin >> N; 10 | for (int i = 1; i <= N; i++) { 11 | cin >> A[i]; 12 | } 13 | 14 | // 답 계산 15 | for (int i = 1; i <= N; i++) { 16 | Answer += A[i]; 17 | } 18 | 19 | // 출력 20 | cout << Answer % 100 << endl; 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /문제풀이/chap2-2/prob2-2-4.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Main { 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | 7 | // 입력 8 | int N = sc.nextInt(); 9 | int[] A = new int[N + 1]; 10 | for (int i = 1; i <= N; i++) { 11 | A[i] = sc.nextInt(); 12 | } 13 | 14 | // 답 계산 15 | int Answer = 0; 16 | for (int i = 1; i <= N; i++) { 17 | Answer += A[i]; 18 | } 19 | 20 | // 출력 21 | System.out.println(Answer % 100); 22 | } 23 | }; 24 | -------------------------------------------------------------------------------- /문제풀이/chap2-2/prob2-2-4.py: -------------------------------------------------------------------------------- 1 | # 입력 2 | N = int(input()) 3 | A = list(map(int, input().split())) 4 | 5 | # 답 계산 6 | Answer = 0 7 | for i in range(N): 8 | Answer += A[i] 9 | 10 | # 출력 11 | print(Answer % 100) 12 | -------------------------------------------------------------------------------- /문제풀이/chap2-5/prob2-5-3.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | // 입력 5 | int N; 6 | scanf("%d", &N); 7 | 8 | // 답 계산 9 | long long Answer = 1; 10 | for (int i = 2; i <= N; i++) Answer *= i; 11 | 12 | // 출력 13 | printf("%lld\n", Answer); 14 | return 0; 15 | } -------------------------------------------------------------------------------- /문제풀이/chap2-5/prob2-5-3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | long long N; 6 | long long Answer = 1; 7 | cin >> N; 8 | for (int i = 2; i <= N; i++) Answer *= i; // Answer에 i를 곱함 9 | cout << Answer << endl; 10 | return 0; 11 | } -------------------------------------------------------------------------------- /문제풀이/chap2-5/prob2-5-3.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Main { 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | 7 | // 입력 8 | long N = sc.nextInt(); 9 | 10 | // 답 계산 11 | long Answer = 1; 12 | for (int i = 1; i <= N; i++) Answer *= i; 13 | 14 | // 출력 15 | System.out.println(Answer); 16 | } 17 | }; -------------------------------------------------------------------------------- /문제풀이/chap2-5/prob2-5-3.py: -------------------------------------------------------------------------------- 1 | # 입력 2 | N = int(input()) 3 | 4 | # 답 계산 5 | Answer = 1 6 | for i in range(1,N+1): 7 | Answer *= i 8 | 9 | # 출력 10 | print(Answer) -------------------------------------------------------------------------------- /문제풀이/chap2-5/prob2-5-4.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | bool isprime(int x) { 5 | for (int i = 2; i <= x - 1; i++) { 6 | // x를 i로 나눈 나머지가 0이라면, x는 i로 나누어짐 7 | if (x % i == 0) return false; 8 | } 9 | return true; 10 | } 11 | 12 | int main() { 13 | // 입력 14 | int N; 15 | scanf("%d", &N); 16 | 17 | // 답 출력(공백 문자로 구분해서 출력하는 경우) 18 | for (int i = 2; i <= N; i++) { 19 | if (isprime(i) == true) { 20 | if (i >= 3) printf(" "); 21 | printf("%d", i); 22 | } 23 | } 24 | printf("\n"); 25 | return 0; 26 | } -------------------------------------------------------------------------------- /문제풀이/chap2-5/prob2-5-4.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | bool isprime(int x) { 5 | for (int i = 2; i <= x - 1; i++) { 6 | // x를 i로 나눈 나머지가 0이라면, x는 i로 나누어짐 7 | if (x % i == 0) return false; 8 | } 9 | return true; 10 | } 11 | 12 | int main() { 13 | // 입력 14 | int N; 15 | cin >> N; 16 | 17 | // 답 출력(공백 문자로 구분해서 출력하는 경우) 18 | for (int i = 2; i <= N; i++) { 19 | if (isprime(i) == true) { 20 | if (i >= 3) cout << " "; 21 | cout << i; 22 | } 23 | } 24 | cout << endl; 25 | return 0; 26 | } -------------------------------------------------------------------------------- /문제풀이/chap2-5/prob2-5-4.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Main { 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | 7 | // 입력 8 | int N = sc.nextInt(); 9 | 10 | // 출력 11 | for (int i = 2; i <= N; i++) { 12 | if (isPrime(i) == true) { 13 | if (i >= 3) System.out.print(" "); 14 | System.out.print(i); 15 | } 16 | } 17 | System.out.println(); 18 | } 19 | static boolean isPrime(long N) { 20 | // 2 이상의 정수 N을 매개변수로 받고, N이 소수라면 True를, 아니라면 False를 리턴하는 함수 21 | for (long i = 2; i <= N - 1; i++) { 22 | if (N % i == 0) { 23 | return false; 24 | } 25 | } 26 | return true; 27 | } 28 | } -------------------------------------------------------------------------------- /문제풀이/chap2-5/prob2-5-4.py: -------------------------------------------------------------------------------- 1 | def isprime(N): 2 | for i in range(2, N): 3 | if N % i == 0: 4 | return False 5 | return True 6 | 7 | N = int(input()) 8 | A = [] 9 | for i in range(2,N+1): 10 | if isprime(i) == True: 11 | A.append(i) 12 | 13 | print(*A) -------------------------------------------------------------------------------- /문제풀이/chap3-1/prob3-1-2.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() { 5 | // 입력 6 | long long N; 7 | scanf("%lld", &N); 8 | 9 | // 소인수분해(공백으로 구분해서 출력) 10 | bool flag = false; 11 | for (long long i = 2; i * i <= N; i++) { 12 | while (N % i == 0) { 13 | if (flag == true) printf(" "); 14 | flag = true; 15 | N /= i; 16 | printf("%lld", i); 17 | } 18 | } 19 | if (N >= 2) { 20 | if (flag == true) printf(" "); 21 | flag = true; 22 | printf("%lld", N); 23 | } 24 | printf("\n"); 25 | return 0; 26 | } -------------------------------------------------------------------------------- /문제풀이/chap3-1/prob3-1-2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | // 입력 6 | long long N; 7 | cin >> N; 8 | 9 | // 소인수분해(공백으로 구분해서 출력) 10 | bool flag = false; 11 | for (long long i = 2; i * i <= N; i++) { 12 | while (N % i == 0) { 13 | if (flag == true) cout << " "; 14 | flag = true; 15 | N /= i; 16 | cout << i; 17 | } 18 | } 19 | if (N >= 2) { 20 | if (flag == true) cout << " "; 21 | flag = true; 22 | cout << N; 23 | } 24 | cout << endl; 25 | return 0; 26 | } -------------------------------------------------------------------------------- /문제풀이/chap3-1/prob3-1-2.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Main { 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | 7 | // 입력 8 | long N = sc.nextLong(); 9 | 10 | // 소인수분해/출력 11 | boolean flag = false; 12 | for (long i = 2; i * i <= N; i++) { 13 | while (N % i == 0) { 14 | if (flag == true) System.out.print(" "); 15 | flag = true; 16 | N /= i; 17 | System.out.print(i); 18 | } 19 | } 20 | 21 | // 마지막에 남은 N 22 | if (N >= 2) { 23 | if (flag == true) System.out.print(" "); 24 | flag = true; 25 | System.out.print(N); 26 | } 27 | System.out.println(); 28 | } 29 | } -------------------------------------------------------------------------------- /문제풀이/chap3-1/prob3-1-2.py: -------------------------------------------------------------------------------- 1 | # 입력 2 | N = int(input()) 3 | 4 | # 소인수분해 5 | Answer = [] 6 | LIMIT = int(N ** 0.5) 7 | for i in range(2, LIMIT + 1): 8 | while N % i == 0: 9 | N /= i 10 | Answer.append(i) 11 | 12 | if N >= 2: 13 | Answer.append(int(N)) 14 | 15 | # 출력 16 | print(*Answer) -------------------------------------------------------------------------------- /문제풀이/chap3-2/prob3-2-2.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | long long GCD(long long A, long long B) { 4 | while (A >= 1 && B >= 1) { 5 | if (A < B) B = B % A; // A < B라면, 큰 수를 b로 변경합니다. 6 | else A = A % B; // A >= B라면 큰 수를 a로 변경합니다. 7 | } 8 | if (A >= 1) return A; 9 | return B; 10 | } 11 | 12 | long long N; 13 | long long A[100009]; 14 | 15 | int main() { 16 | // 입력 17 | scanf("%lld", &N); 18 | for (int i = 1; i <= N; i++) scanf("%lld", &A[i]); 19 | 20 | // 답 구하기 21 | long long R = GCD(A[1], A[2]); 22 | for (int i = 3; i <= N; i++) { 23 | R = GCD(R, A[i]); 24 | } 25 | 26 | // 출력 27 | printf("%lld\n", R); 28 | return 0; 29 | } -------------------------------------------------------------------------------- /문제풀이/chap3-2/prob3-2-2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | long long GCD(long long A, long long B) { 6 | while (A >= 1 && B >= 1) { 7 | if (A < B) B = B % A; // A < B라면, 큰 수를 b로 변경합니다. 8 | else A = A % B; // A >= B라면 큰 수를 a로 변경합니다. 9 | } 10 | if (A >= 1) return A; 11 | return B; 12 | } 13 | 14 | long long N; 15 | long long A[100009]; 16 | 17 | int main() { 18 | // 입력 19 | cin >> N; 20 | for (int i = 1; i <= N; i++) cin >> A[i]; 21 | 22 | // 답 구하기 23 | long long R = GCD(A[1], A[2]); 24 | for (int i = 3; i <= N; i++) { 25 | R = GCD(R, A[i]); 26 | } 27 | 28 | // 출력 29 | cout << R << endl; 30 | return 0; 31 | } -------------------------------------------------------------------------------- /문제풀이/chap3-2/prob3-2-2.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Main { 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | 7 | // 입력 8 | int N = sc.nextInt(); 9 | long[] A = new long[N + 1]; 10 | for (int i = 1; i <= N; i++) A[i] = sc.nextLong(); 11 | 12 | // 답 구하기 13 | long R = GCD(A[1], A[2]); 14 | for (int i = 3; i <= N; i++) { 15 | R = GCD(R, A[i]); 16 | } 17 | 18 | // 출력 19 | System.out.println(R); 20 | } 21 | static long GCD(long A, long B) { 22 | // 양의 정수 A와 B의 최대 공약수를 리턴하는 함수 23 | while (A >= 1 && B >= 1) { 24 | if (A < B) { 25 | B %= A; 26 | } 27 | else { 28 | A %= B; 29 | } 30 | } 31 | if (A >= 1) { 32 | return A; 33 | } 34 | return B; 35 | } 36 | } -------------------------------------------------------------------------------- /문제풀이/chap3-2/prob3-2-2.py: -------------------------------------------------------------------------------- 1 | # 양의 정수 A와 B의 최대 공약수를 리턴하는 함수 2 | def GCD(A, B): 3 | while A >= 1 and B >= 1: 4 | if A < B: 5 | B = B % A # A < B라면, 큰 수를 b로 변경합니다. 6 | else: 7 | A = A % B # A >= B라면 큰 수를 a로 변경합니다. 8 | if A >= 1: 9 | return A 10 | return B 11 | 12 | # 입력 13 | N = int(input()) 14 | A = list(map(int, input().split())) 15 | 16 | # 답 구하기 17 | R = GCD(A[0], A[1]) 18 | for i in range(2, N): 19 | R = GCD(R, A[i]) 20 | 21 | # 출력 22 | print(R) -------------------------------------------------------------------------------- /문제풀이/chap3-2/prob3-2-3.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | // 최대공약수를 리턴하는 함수 4 | long long GCD(long long A, long long B) { 5 | while (A >= 1 && B >= 1) { 6 | if (A < B) B = B % A; // A < B라면, 큰 수를 b로 변경합니다. 7 | else A = A % B; // A >= B라면 큰 수를 a로 변경합니다. 8 | } 9 | if (A >= 1) return A; 10 | return B; 11 | } 12 | 13 | // 최소공배수를 리턴하는 함수 14 | long long LCM(long long A, long long B) { 15 | return (A / GCD(A, B)) * B; 16 | } 17 | 18 | long long N; 19 | long long A[100009]; 20 | 21 | int main() { 22 | // 입력 23 | scanf("%lld", &N); 24 | for (int i = 1; i <= N; i++) scanf("%lld", &A[i]); 25 | 26 | // 답 구하기 27 | long long R = LCM(A[1], A[2]); 28 | for (int i = 3; i <= N; i++) { 29 | R = LCM(R, A[i]); 30 | } 31 | 32 | // 출력 33 | printf("%lld\n", R); 34 | return 0; 35 | } -------------------------------------------------------------------------------- /문제풀이/chap3-2/prob3-2-3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | // 최대공약수를 리턴하는 함수 6 | long long GCD(long long A, long long B) { 7 | while (A >= 1 && B >= 1) { 8 | if (A < B) B = B % A; // A < B라면, 큰 수를 b로 변경합니다. 9 | else A = A % B; // A >= B라면 큰 수를 a로 변경합니다. 10 | } 11 | if (A >= 1) return A; 12 | return B; 13 | } 14 | 15 | // 최소공배수를 리턴하는 함수 16 | long long LCM(long long A, long long B) { 17 | return (A / GCD(A, B)) * B; 18 | } 19 | 20 | long long N; 21 | long long A[100009]; 22 | 23 | int main() { 24 | // 입력 25 | cin >> N; 26 | for (int i = 1; i <= N; i++) cin >> A[i]; 27 | 28 | // 답 구하기 29 | long long R = LCM(A[1], A[2]); 30 | for (int i = 3; i <= N; i++) { 31 | R = LCM(R, A[i]); 32 | } 33 | 34 | // 출력 35 | cout << R << endl; 36 | return 0; 37 | } -------------------------------------------------------------------------------- /문제풀이/chap3-2/prob3-2-3.py: -------------------------------------------------------------------------------- 1 | # 최대공약수를 리턴하는 함수 2 | def GCD(A, B): 3 | while A >= 1 and B >= 1: 4 | if A < B: 5 | B = B % A # A < B라면, 큰 수를 b로 변경합니다. 6 | else: 7 | A = A % B # A >= B라면 큰 수를 a로 변경합니다. 8 | if A >= 1: 9 | return A 10 | return B 11 | 12 | # 최소공배수를 리턴하는 함수 13 | def LCM(A, B): 14 | return int(A / GCD(A, B)) * B 15 | 16 | # 입력 17 | N = int(input()) 18 | A = list(map(int, input().split())) 19 | 20 | # 답 구하기 21 | R = LCM(A[0], A[1]) 22 | for i in range(2, N): 23 | R = LCM(R, A[i]) 24 | 25 | # 출력 26 | print(R) -------------------------------------------------------------------------------- /문제풀이/chap3-3/prob3-3-3.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | long long n, r; 4 | long long Fact_n = 1; 5 | long long Fact_r = 1; 6 | long long Fact_nr = 1; 7 | 8 | int main() { 9 | // 입력 10 | scanf("%lld%lld", &n, &r); 11 | 12 | // 팩토리얼 구하기 13 | for (int i = 1; i <= n; i++) Fact_n *= i; 14 | for (int i = 1; i <= r; i++) Fact_r *= i; 15 | for (int i = 1; i <= n - r; i++) Fact_nr *= i; 16 | 17 | // 출력 18 | printf("%lld\n", Fact_n / (Fact_r * Fact_nr)); 19 | return 0; 20 | } -------------------------------------------------------------------------------- /문제풀이/chap3-3/prob3-3-3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | long long n, r; 5 | long long Fact_n = 1; 6 | long long Fact_r = 1; 7 | long long Fact_nr = 1; 8 | 9 | int main() { 10 | // 입력 11 | cin >> n >> r; 12 | 13 | // 팩토리얼 구하기 14 | for (int i = 1; i <= n; i++) Fact_n *= i; 15 | for (int i = 1; i <= r; i++) Fact_r *= i; 16 | for (int i = 1; i <= n - r; i++) Fact_nr *= i; 17 | 18 | // 출력 19 | cout << Fact_n / (Fact_r * Fact_nr) << endl; 20 | return 0; 21 | } -------------------------------------------------------------------------------- /문제풀이/chap3-3/prob3-3-3.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Main { 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | 7 | // 입력 8 | int n = sc.nextInt(); 9 | int r = sc.nextInt(); 10 | 11 | // 팩토리얼 구하기 12 | long fact_n = 1, fact_r = 1, fact_nr = 1; 13 | for (int i = 1; i <= n; i++) fact_n *= i; 14 | for (int i = 1; i <= r; i++) fact_r *= i; 15 | for (int i = 1; i <= n - r; i++) fact_nr *= i; 16 | 17 | // 출력 18 | System.out.println(fact_n / (fact_r * fact_nr)); 19 | } 20 | } -------------------------------------------------------------------------------- /문제풀이/chap3-3/prob3-3-3.py: -------------------------------------------------------------------------------- 1 | # 입력 2 | n,r = map(int, input().split()) 3 | 4 | # 팩토리얼 구하기 5 | fact_n = 1 6 | for i in range(1, n+1): 7 | fact_n *= i 8 | 9 | fact_r = 1 10 | for i in range(1, r+1): 11 | fact_r *= i 12 | 13 | fact_nr = 1 14 | for i in range(1, n-r+1): 15 | fact_nr *= i 16 | 17 | # 출력 18 | print(int(fact_n / (fact_r * fact_nr))) -------------------------------------------------------------------------------- /문제풀이/chap3-3/prob3-3-4.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | long long N; 4 | long long A[200009]; 5 | long long a = 0, b = 0, c = 0, d = 0; // 오버플로우를 피할 수 있게 long long 사용 6 | 7 | int main() { 8 | // 입력 9 | scanf("%lld", &N); 10 | for (int i = 1; i <= N; i++) scanf("%lld", &A[i]); 11 | 12 | // a, b, c, d의 개수 세기 13 | for (int i = 1; i <= N; i++) { 14 | if (A[i] == 100) a += 1; 15 | if (A[i] == 200) b += 1; 16 | if (A[i] == 300) c += 1; 17 | if (A[i] == 400) d += 1; 18 | } 19 | 20 | // 출력(답은 a * d + b * c) 21 | printf("%lld\n", a * d + b * c); 22 | return 0; 23 | } -------------------------------------------------------------------------------- /문제풀이/chap3-3/prob3-3-4.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | long long N; 5 | long long A[200009]; 6 | long long a = 0, b = 0, c = 0, d = 0; // 오버플로우를 피할 수 있게 long long 사용 7 | 8 | int main() { 9 | // 입력 10 | cin >> N; 11 | for (int i = 1; i <= N; i++) cin >> A[i]; 12 | 13 | // a, b, c, d의 개수 세기 14 | for (int i = 1; i <= N; i++) { 15 | if (A[i] == 100) a += 1; 16 | if (A[i] == 200) b += 1; 17 | if (A[i] == 300) c += 1; 18 | if (A[i] == 400) d += 1; 19 | } 20 | 21 | // 출력(답은 a * d + b * c) 22 | cout << a * d + b * c << endl; 23 | return 0; 24 | } -------------------------------------------------------------------------------- /문제풀이/chap3-3/prob3-3-4.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Main { 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | 7 | // 입력 8 | int N = sc.nextInt(); 9 | int[] A = new int[N + 1]; 10 | for (int i = 1; i <= N; i++) A[i] = sc.nextInt(); 11 | 12 | // 답 구하기 13 | long a = 0, b = 0, c = 0, d = 0; 14 | for (int i = 1; i <= N; i++) { 15 | if (A[i] == 100) a += 1; 16 | if (A[i] == 200) b += 1; 17 | if (A[i] == 300) c += 1; 18 | if (A[i] == 400) d += 1; 19 | } 20 | 21 | // 출력 22 | System.out.println(a * d + b * c); 23 | } 24 | } -------------------------------------------------------------------------------- /문제풀이/chap3-3/prob3-3-4.py: -------------------------------------------------------------------------------- 1 | # 입력 2 | N = int(input()) 3 | A = list(map(int, input().split())) 4 | 5 | # 답 구하기 6 | a = 0 7 | b = 0 8 | c = 0 9 | d = 0 10 | for i in range(N): 11 | if A[i] == 100: 12 | a += 1 13 | if A[i] == 200: 14 | b += 1 15 | if A[i] == 300: 16 | c += 1 17 | if A[i] == 400: 18 | d += 1 19 | 20 | # 출력 21 | print(a * d + b * c) -------------------------------------------------------------------------------- /문제풀이/chap3-3/prob3-3-5.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | long long N; 4 | long long A[500009]; 5 | long long x = 0, y = 0, z = 0; 6 | 7 | int main() { 8 | // 입력 9 | scanf("%lld", &N); 10 | for (int i = 1; i <= N; i++) scanf("%lld", &A[i]); 11 | 12 | // a, b, c, d의 개수 세기 13 | for (int i = 1; i <= N; i++) { 14 | if (A[i] == 1) x += 1; 15 | if (A[i] == 2) y += 1; 16 | if (A[i] == 3) z += 1; 17 | } 18 | 19 | // 출력 20 | printf("%lld\n", x * (x - 1) / 2 + y * (y - 1) / 2 + z * (z - 1) / 2); 21 | return 0; 22 | } -------------------------------------------------------------------------------- /문제풀이/chap3-3/prob3-3-5.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | long long N; 5 | long long A[500009]; 6 | long long x = 0, y = 0, z = 0; 7 | 8 | int main() { 9 | // 입력 10 | cin >> N; 11 | for (int i = 1; i <= N; i++) cin >> A[i]; 12 | 13 | // a, b, c, d의 개수 세기 14 | for (int i = 1; i <= N; i++) { 15 | if (A[i] == 1) x += 1; 16 | if (A[i] == 2) y += 1; 17 | if (A[i] == 3) z += 1; 18 | } 19 | 20 | // 출력 21 | cout << x * (x - 1) / 2 + y * (y - 1) / 2 + z * (z - 1) / 2 << endl; 22 | return 0; 23 | } -------------------------------------------------------------------------------- /문제풀이/chap3-3/prob3-3-5.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Main { 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | 7 | // 입력 8 | int N = sc.nextInt(); 9 | int[] A = new int[N + 1]; 10 | for (int i = 1; i <= N; i++) A[i] = sc.nextInt(); 11 | 12 | // 답 구하기 13 | long x = 0, y = 0, z = 0; 14 | for (int i = 1; i <= N; i++) { 15 | if (A[i] == 1) x += 1; 16 | if (A[i] == 2) y += 1; 17 | if (A[i] == 3) z += 1; 18 | } 19 | 20 | // 출력 21 | System.out.println(x * (x - 1) / 2 + y * (y - 1) / 2 + z * (z - 1) / 2); 22 | } 23 | } -------------------------------------------------------------------------------- /문제풀이/chap3-3/prob3-3-5.py: -------------------------------------------------------------------------------- 1 | # 입력 2 | N = int(input()) 3 | A = list(map(int, input().split())) 4 | 5 | # 답 구하기 6 | x = 0 7 | y = 0 8 | z = 0 9 | for i in range(N): 10 | if A[i] == 1: 11 | x += 1 12 | if A[i] == 2: 13 | y += 1 14 | if A[i] == 3: 15 | z += 1 16 | 17 | # 출력 18 | print(x * (x - 1) // 2 + y * (y - 1) // 2 + z * (z - 1) // 2) -------------------------------------------------------------------------------- /문제풀이/chap3-3/prob3-3-6.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | long long N, A[200009]; 4 | long long cnt[100009]; 5 | long long Answer = 0; 6 | 7 | int main() { 8 | // 입력 9 | scanf("%lld", &N); 10 | for (int i = 1; i <= N; i++) scanf("%lld", &A[i]); 11 | 12 | // cnt[1], cnt[2], ..., cnt[99999] 세기 13 | for (int i = 1; i <= 99999; i++) cnt[i] = 0; 14 | for (int i = 1; i <= N; i++) cnt[A[i]] += 1; 15 | 16 | // 답 구하기 17 | for (int i = 1; i <= 49999; i++) { 18 | Answer += cnt[i] * cnt[100000 - i]; 19 | } 20 | Answer += cnt[50000] * (cnt[50000] - 1) / 2; 21 | 22 | // 출력 23 | printf("%lld\n", Answer); 24 | return 0; 25 | } -------------------------------------------------------------------------------- /문제풀이/chap3-3/prob3-3-6.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | long long N, A[200009]; 5 | long long cnt[100009]; 6 | long long Answer = 0; 7 | 8 | int main() { 9 | // 입력 10 | cin >> N; 11 | for (int i = 1; i <= N; i++) cin >> A[i]; 12 | 13 | // cnt[1], cnt[2], ..., cnt[99999] 세기 14 | for (int i = 1; i <= 99999; i++) cnt[i] = 0; 15 | for (int i = 1; i <= N; i++) cnt[A[i]] += 1; 16 | 17 | // 답 구하기 18 | for (int i = 1; i <= 49999; i++) { 19 | Answer += cnt[i] * cnt[100000 - i]; 20 | } 21 | Answer += cnt[50000] * (cnt[50000] - 1) / 2; 22 | 23 | // 출력 24 | cout << Answer << endl; 25 | return 0; 26 | } -------------------------------------------------------------------------------- /문제풀이/chap3-3/prob3-3-6.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Main { 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | 7 | // 입력 8 | int N = sc.nextInt(); 9 | int[] A = new int[N + 1]; 10 | for (int i = 1; i <= N; i++) A[i] = sc.nextInt(); 11 | 12 | // 답 구하기 13 | long[] cnt = new long[100009]; 14 | for (int i = 1; i <= 99999; i++) cnt[i] = 0; 15 | for (int i = 1; i <= N; i++) cnt[A[i]] += 1; 16 | 17 | long Answer = 0; 18 | for (int i = 1; i <= 49999; i++) Answer += cnt[i] * cnt[100000 - i]; 19 | Answer += cnt[50000] * (cnt[50000] - 1) / 2; 20 | 21 | // 출력 22 | System.out.println(Answer); 23 | } 24 | } -------------------------------------------------------------------------------- /문제풀이/chap3-3/prob3-3-6.py: -------------------------------------------------------------------------------- 1 | # 입력 2 | N = int(input()) 3 | A = list(map(int, input().split())) 4 | 5 | # 답 구하기 6 | cnt = [0 for i in range(100000)] 7 | for i in range(N): 8 | cnt[A[i]] += 1 9 | 10 | Answer = 0 11 | for i in range(1, 50000): 12 | Answer += cnt[i] * cnt[100000 - i] 13 | Answer += cnt[50000] * (cnt[50000] - 1) // 2 14 | 15 | # 출력 16 | print(Answer) -------------------------------------------------------------------------------- /문제풀이/chap3-4/prob3-4-3.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int N; 4 | double A[109], B[109]; 5 | double Answer = 0.0; 6 | 7 | int main() { 8 | // 입력 9 | scanf("%d", &N); 10 | for (int i = 1; i <= N; i++) scanf("%lf", &A[i]); 11 | for (int i = 1; i <= N; i++) scanf("%lf", &B[i]); 12 | 13 | // 기댓값 구하기 14 | for (int i = 1; i <= N; i++) { 15 | double eval = A[i] * (1.0 / 3.0) + B[i] * (2.0 / 3.0); 16 | Answer += eval; 17 | } 18 | 19 | // 출력 20 | printf("%.12lf\n", Answer); 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /문제풀이/chap3-4/prob3-4-3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int N; 5 | double A[109], B[109]; 6 | double Answer = 0.0; 7 | 8 | int main() { 9 | // 입력 10 | cin >> N; 11 | for (int i = 1; i <= N; i++) cin >> A[i]; 12 | for (int i = 1; i <= N; i++) cin >> B[i]; 13 | 14 | // 기댓값 구하기 15 | for (int i = 1; i <= N; i++) { 16 | double eval = A[i] * (1.0 / 3.0) + B[i] * (2.0 / 3.0); 17 | Answer += eval; 18 | } 19 | 20 | // 출력 21 | printf("%.12lf\n", Answer); 22 | return 0; 23 | } -------------------------------------------------------------------------------- /문제풀이/chap3-4/prob3-4-3.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Main { 4 | public static void main(String[] args) { 5 | // 입력 6 | Scanner sc = new Scanner(System.in); 7 | int N = sc.nextInt(); 8 | int[] A = new int[N + 1]; 9 | int[] B = new int[N + 1]; 10 | for (int i = 1; i <= N; i++) A[i] = sc.nextInt(); 11 | for (int i = 1; i <= N; i++) B[i] = sc.nextInt(); 12 | 13 | // 답 계산/출력 14 | double Answer = 0.0; 15 | for (int i = 1; i <= N; i++) { 16 | Answer += (1.0 / 3.0) * A[i] + (2.0 / 3.0) * B[i]; 17 | } 18 | System.out.format("%.12f\n", Answer); 19 | } 20 | } -------------------------------------------------------------------------------- /문제풀이/chap3-4/prob3-4-3.py: -------------------------------------------------------------------------------- 1 | # 입력 2 | N = int(input()) 3 | A = list(map(int, input().split())) 4 | B = list(map(int, input().split())) 5 | 6 | # 답 구하기 7 | Answer = 0.0 8 | for i in range(N): 9 | Answer += (1.0 / 3.0) * A[i] + (2.0 / 3.0) * B[i] 10 | 11 | # 출력 12 | print("{:.12f}".format(Answer)) -------------------------------------------------------------------------------- /문제풀이/chap3-4/prob3-4-4.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int N; 4 | double Answer = 0; 5 | 6 | int main() { 7 | // 입력 8 | scanf("%d", &N); 9 | 10 | // 기댓값 구하기 11 | for (int i = N; i >= 1; i--) { 12 | Answer += 1.0 * N / i; 13 | } 14 | 15 | // 출력 16 | printf("%.12lf\n", Answer); 17 | return 0; 18 | } -------------------------------------------------------------------------------- /문제풀이/chap3-4/prob3-4-4.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int N; 5 | double Answer = 0; 6 | 7 | int main() { 8 | // 입력 9 | cin >> N; 10 | 11 | // 기댓값 구하기 12 | for (int i = N; i >= 1; i--) { 13 | Answer += 1.0 * N / i; 14 | } 15 | 16 | // 출력 17 | printf("%.12lf\n", Answer); 18 | return 0; 19 | } -------------------------------------------------------------------------------- /문제풀이/chap3-4/prob3-4-4.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Main { 4 | public static void main(String[] args) { 5 | // 입력 6 | Scanner sc = new Scanner(System.in); 7 | int N = sc.nextInt(); 8 | 9 | // 답 계산 10 | double Answer = 0.0; 11 | for (int i = N; i >= 1; i--) { 12 | Answer += 1.0 * N / i; 13 | } 14 | 15 | // 출력 16 | System.out.format("%.12f\n", Answer); 17 | } 18 | } -------------------------------------------------------------------------------- /문제풀이/chap3-4/prob3-4-4.py: -------------------------------------------------------------------------------- 1 | # 입력 2 | N = int(input()) 3 | 4 | # 답 계산 5 | Answer = 0.0 6 | for i in range(1, N+1): 7 | Answer += 1.0 * N / i 8 | 9 | # 출력 10 | print("{:.12f}".format(Answer)) -------------------------------------------------------------------------------- /문제풀이/chap3-5/prob3-5-2.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main() { 6 | int N = 1000000; 7 | int M = 0; 8 | 9 | for (int i = 1; i <= N; i++) { 10 | double px = 6.0 * rand() / (double)RAND_MAX; 11 | double py = 9.0 * rand() / (double)RAND_MAX; 12 | 13 | // 점 (3, 3)과의 거리입니다. 이 값이 3 이하라면, 반지름 3인 원 내부에 포함됩니다. 14 | double dist_33 = sqrt((px - 3.0) * (px - 3.0) + (py - 3.0) * (py - 3.0)); 15 | 16 | // 점 (3, 7)과의 거리입니다. 이 값이 2 이하라면, 반지름 2인 원 내부에 포함됩니다. 17 | double dist_37 = sqrt((px - 3.0) * (px - 3.0) + (py - 7.0) * (py - 7.0)); 18 | 19 | // 조건 분기 20 | if (dist_33 <= 3.0 || dist_37 <= 2.0) M += 1; 21 | } 22 | 23 | // 답 출력 24 | printf("%d\n", M); 25 | return 0; 26 | } -------------------------------------------------------------------------------- /문제풀이/chap3-5/prob3-5-2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | int N = 1000000; 7 | int M = 0; 8 | 9 | for (int i = 1; i <= N; i++) { 10 | double px = 6.0 * rand() / (double)RAND_MAX; 11 | double py = 9.0 * rand() / (double)RAND_MAX; 12 | 13 | // 점 (3, 3)과의 거리입니다. 이 값이 3 이하라면, 반지름 3인 원 내부에 포함됩니다. 14 | double dist_33 = sqrt((px - 3.0) * (px - 3.0) + (py - 3.0) * (py - 3.0)); 15 | 16 | // 점 (3, 7)과의 거리입니다. 이 값이 2 이하라면, 반지름 2인 원 내부에 포함됩니다. 17 | double dist_37 = sqrt((px - 3.0) * (px - 3.0) + (py - 7.0) * (py - 7.0)); 18 | 19 | // 조건 분기 20 | if (dist_33 <= 3.0 || dist_37 <= 2.0) M += 1; 21 | } 22 | 23 | // 답 출력 24 | cout << M << endl; 25 | return 0; 26 | } -------------------------------------------------------------------------------- /문제풀이/chap3-5/prob3-5-2.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Main { 4 | public static void main(String[] args) { 5 | Random rd = new Random(); 6 | int N = 1000000; // N은 시행 횟수(적절하게 변경해서 사용해 주세요) 7 | int M = 0; 8 | for (int i = 1; i <= N; i++) { 9 | double px = 6.0 * rd.nextDouble(); 10 | double py = 9.0 * rd.nextDouble(); 11 | 12 | // 점 (3, 3)과의 거리입니다. 이 값이 3 이하라면, 반지름 3인 원 내부에 포함됩니다. 13 | double dist_33 = Math.sqrt((px - 3.0) * (px - 3.0) + (py - 3.0) * (py - 3.0)); 14 | 15 | // 점 (3, 7)과의 거리입니다. 이 값이 2 이하라면, 반지름 2인 원 내부에 포함됩니다. 16 | double dist_37 = Math.sqrt((px - 3.0) * (px - 3.0) + (py - 7.0) * (py - 7.0)); 17 | 18 | if (dist_33 <= 3.0 || dist_37 <= 2.0) M += 1; 19 | } 20 | System.out.println(M); 21 | } 22 | } -------------------------------------------------------------------------------- /문제풀이/chap3-5/prob3-5-2.py: -------------------------------------------------------------------------------- 1 | import random 2 | 3 | N = 1000000 # N은 시행 횟수(적절하게 변경해서 사용해 주세요) 4 | M = 0 5 | for i in range(N): 6 | px = 6.0 * random.random() 7 | py = 9.0 * random.random() 8 | 9 | # 점 (3, 3)과의 거리입니다. 이 값이 3 이하라면, 반지름 3인 원 내부에 포함됩니다. 10 | dist_33 = ((px - 3.0) * (px - 3.0) + (py - 3.0) * (py - 3.0)) ** 0.5 11 | 12 | # 점 (3, 7)과의 거리입니다. 이 값이 2 이하라면, 반지름 2인 원 내부에 포함됩니다. 13 | dist_37 = ((px - 3.0) * (px - 3.0) + (py - 7.0) * (py - 7.0)) ** 0.5 14 | 15 | # 판정 16 | if (dist_33 <= 3.0 or dist_37 <= 2.0): 17 | M += 1 18 | 19 | print(M) -------------------------------------------------------------------------------- /문제풀이/chap3-7/prob3-7-4.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int N, S, A[69]; 5 | bool dp[69][10009]; 6 | 7 | int main() { 8 | // 입력 9 | scanf("%d%d", &N, &S); 10 | for (int i = 1; i <= N; i++) scanf("%d", &A[i]); 11 | 12 | // 배열 초기화 13 | dp[0][0] = true; 14 | for (int i = 1; i <= S; i++) dp[0][i] = false; 15 | 16 | // 동적계획법 17 | for (int i = 1; i <= N; i++) { 18 | for (int j = 0; j <= S; j++) { 19 | // j < A[i]라면, 카드 i를 선택할 수 없음 20 | if (j < A[i]) dp[i][j] = dp[i-1][j]; 21 | // j >= A[i]라면, 선택/선택하지 않음이라는 선택지가 있음 22 | if (j >= A[i]) { 23 | if (dp[i-1][j] == true || dp[i-1][j-A[i]] == true) dp[i][j] = true; 24 | else dp[i][j] = false; 25 | } 26 | } 27 | } 28 | 29 | // 답 출력 30 | if (dp[N][S] == true) printf("Yes\n"); 31 | else printf("No\n"); 32 | return 0; 33 | } -------------------------------------------------------------------------------- /문제풀이/chap3-7/prob3-7-4.py: -------------------------------------------------------------------------------- 1 | # 입력 2 | N, S = map(int, input().split()) 3 | A = list(map(int, input().split())) 4 | 5 | # 배열 초기화 6 | dp = [ [ None ] * (S + 1) for i in range(N + 1) ] 7 | dp[0][0] = True 8 | for i in range(1, S + 1): 9 | dp[0][i] = False 10 | 11 | # 동적계획법 12 | for i in range(1, N + 1): 13 | for j in range(0, S + 1): 14 | if j < A[i - 1]: 15 | # j < A[i-1]라면, 카드 i를 선택할 수 없음 16 | dp[i][j] = dp[i - 1][j] 17 | else: 18 | # j >= A[i-1]라면, 선택/선택하지 않음이라는 선택지가 있음 19 | if (dp[i - 1][j] == True or dp[i - 1][j - A[i - 1]] == True): 20 | dp[i][j] = True 21 | else: 22 | dp[i][j] = False 23 | 24 | # 답 출력 25 | if dp[N][S] == True: 26 | print("Yes") 27 | else: 28 | print("No") -------------------------------------------------------------------------------- /문제풀이/chap3-7/prob3-7-6.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | long long N, A[500009]; 4 | long long dp1[500009], dp2[500009]; 5 | 6 | long long max(long long a, long long b) { 7 | if (a < b) return b; 8 | return a; 9 | } 10 | 11 | int main() { 12 | // 입력 13 | scanf("%lld", &N); 14 | for (int i = 1; i <= N; i++) scanf("%lld", &A[i]); 15 | 16 | // 배열 초기화 17 | dp1[0] = 0; 18 | dp2[0] = 0; 19 | 20 | // 동적계획법 21 | for (int i = 1; i <= N; i++) { 22 | dp1[i] = dp2[i - 1] + A[i]; 23 | dp2[i] = max(dp1[i - 1], dp2[i - 1]); 24 | } 25 | 26 | // 답 출력 27 | printf("%lld\n", max(dp1[N], dp2[N])); 28 | return 0; 29 | } -------------------------------------------------------------------------------- /문제풀이/chap3-7/prob3-7-6.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | long long N, A[500009]; 6 | long long dp1[500009], dp2[500009]; 7 | 8 | int main() { 9 | // 입력 10 | cin >> N; 11 | for (int i = 1; i <= N; i++) cin >> A[i]; 12 | 13 | // 배열 초기화 14 | dp1[0] = 0; 15 | dp2[0] = 0; 16 | 17 | // 동적계획법 18 | for (int i = 1; i <= N; i++) { 19 | dp1[i] = dp2[i - 1] + A[i]; 20 | dp2[i] = max(dp1[i - 1], dp2[i - 1]); 21 | } 22 | 23 | // 답 출력 24 | cout << max(dp1[N], dp2[N]) << endl; 25 | return 0; 26 | } -------------------------------------------------------------------------------- /문제풀이/chap3-7/prob3-7-6.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Main { 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | 7 | // 입력 8 | int N = sc.nextInt(); 9 | int[] A = new int[N + 1]; 10 | for (int i = 1; i <= N; i++) { 11 | A[i] = sc.nextInt(); 12 | } 13 | 14 | // 배열 초기화 15 | long[] dp1 = new long[N + 1]; 16 | long[] dp2 = new long[N + 1]; 17 | dp1[0] = 0; 18 | dp2[0] = 0; 19 | 20 | // 동적계획법 21 | for (int i = 1; i <= N; i++) { 22 | dp1[i] = dp2[i - 1] + A[i]; 23 | dp2[i] = Math.max(dp1[i - 1], dp2[i - 1]); 24 | } 25 | 26 | // 답을 계산하고 출력 27 | long Answer = Math.max(dp1[N], dp2[N]); 28 | System.out.println(Answer); 29 | } 30 | } -------------------------------------------------------------------------------- /문제풀이/chap3-7/prob3-7-6.py: -------------------------------------------------------------------------------- 1 | # 입력 2 | N = int(input()) 3 | A = list(map(int, input().split())) 4 | 5 | # 배열 초기화 6 | dp1 = [ None ] * (N + 1) 7 | dp2 = [ None ] * (N + 1) 8 | dp1[0] = 0 9 | dp2[0] = 0 10 | 11 | # 동적계획법 12 | for i in range(1, N + 1): 13 | dp1[i] = dp2[i - 1] + A[i - 1] 14 | dp2[i] = max(dp1[i - 1], dp2[i - 1]) 15 | 16 | # 출력 17 | Answer = max(dp1[N], dp2[N]) 18 | print(Answer) -------------------------------------------------------------------------------- /문제풀이/chap4-1/prob4-1-2.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int N; 5 | double x[2009], y[2009]; 6 | double Answer = 1000000000.0; // 굉장히 큰 값으로 초기화 7 | 8 | int main() { 9 | // 입력 10 | scanf("%d", &N); 11 | for (int i = 1; i <= N; i++) scanf("%lf%lf", &x[i], &y[i]); 12 | 13 | // 전체 탐색 14 | for (int i = 1; i <= N; i++) { 15 | for (int j = i + 1; j <= N; j++) { 16 | // dist는 i번째 점과 j번째 점 사이의 거리 17 | double dist = sqrt((x[i]-x[j]) * (x[i]-x[j]) + (y[i]-y[j]) * (y[i]-y[j])); 18 | if (Answer > dist) { 19 | Answer = dist; 20 | } 21 | } 22 | } 23 | 24 | // 답 출력 25 | printf("%.12lf\n", Answer); 26 | return 0; 27 | } -------------------------------------------------------------------------------- /문제풀이/chap4-1/prob4-1-2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int N; 6 | double x[2009], y[2009]; 7 | double Answer = 1000000000.0; // 굉장히 큰 값으로 초기화 8 | 9 | int main() { 10 | // 입력 11 | cin >> N; 12 | for (int i = 1; i <= N; i++) cin >> x[i] >> y[i]; 13 | 14 | // 전체 탐색 15 | for (int i = 1; i <= N; i++) { 16 | for (int j = i + 1; j <= N; j++) { 17 | // dist는 i번째 점과 j번째 점 사이의 거리 18 | double dist = sqrt((x[i]-x[j]) * (x[i]-x[j]) + (y[i]-y[j]) * (y[i]-y[j])); 19 | Answer = min(Answer, dist); 20 | } 21 | } 22 | 23 | // 답 출력 24 | printf("%.12lf\n", Answer); 25 | return 0; 26 | } -------------------------------------------------------------------------------- /문제풀이/chap4-1/prob4-1-2.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Main { 4 | public static void main(String[] args) { 5 | // 입력 6 | Scanner sc = new Scanner(System.in); 7 | int N = sc.nextInt(); 8 | double[] x = new double[N + 1]; 9 | double[] y = new double[N + 1]; 10 | for (int i = 1; i <= N; i++) { 11 | x[i] = sc.nextDouble(); 12 | y[i] = sc.nextDouble(); 13 | } 14 | 15 | // 전체 탐색 16 | double Answer = 1000000000.0; 17 | for (int i = 1; i <= N; i++) { 18 | for (int j = i + 1; j <= N; j++) { 19 | double dist = Math.sqrt((x[i] - x[j]) * (x[i] - x[j]) + (y[i] - y[j]) * (y[i] - y[j])); 20 | Answer = Math.min(Answer, dist); 21 | } 22 | } 23 | 24 | // 출력 25 | System.out.format("%.12f\n", Answer); 26 | } 27 | } -------------------------------------------------------------------------------- /문제풀이/chap4-1/prob4-1-2.py: -------------------------------------------------------------------------------- 1 | # 입력 2 | N = int(input()) 3 | x = [0 for i in range(N)] 4 | y = [0 for i in range(N)] 5 | for i in range(N): 6 | x[i],y[i] = map(int, input().split()) 7 | 8 | # 전체 탐색 9 | Answer = 1000000000.0 10 | for i in range(N): 11 | for j in range(i+1,N): 12 | dist = (((x[i] - x[j]) ** 2 + (y[i] - y[j]) ** 2) ** 0.5) 13 | Answer = min(Answer, dist) 14 | 15 | # 답 출력 16 | print("{:.12f}".format(Answer)) 17 | -------------------------------------------------------------------------------- /문제풀이/chap4-1/prob4-1-3.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | double X1, Y1, R1; 5 | double X2, Y2, R2; 6 | 7 | int main() { 8 | // 입력 9 | scanf("%lf%lf%lf", &X1, &Y1, &R1); 10 | scanf("%lf%lf%lf", &X2, &Y2, &R2); 11 | 12 | // 원 중심과의 거리 구하기 13 | double d = sqrt((X1 - X2) * (X1 - X2) + (Y1 - Y2) * (Y1 - Y2)); 14 | 15 | // 답 출력 16 | if (d < abs(R1 - R2)) printf("1\n"); 17 | else if (d == abs(R1 - R2)) printf("2\n"); 18 | else if (d < R1 + R2) printf("3\n"); 19 | else if (d == R1 + R2) printf("4\n"); 20 | else printf("5\n"); 21 | return 0; 22 | } -------------------------------------------------------------------------------- /문제풀이/chap4-1/prob4-1-3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | double X1, Y1, R1; 6 | double X2, Y2, R2; 7 | 8 | int main() { 9 | // 입력 10 | cin >> X1 >> Y1 >> R1; 11 | cin >> X2 >> Y2 >> R2; 12 | 13 | // 원 중심과의 거리 구하기 14 | double d = sqrt((X1 - X2) * (X1 - X2) + (Y1 - Y2) * (Y1 - Y2)); 15 | 16 | // 답 출력 17 | if (d < abs(R1 - R2)) cout << "1" << endl; 18 | else if (d == abs(R1 - R2)) cout << "2" << endl; 19 | else if (d < R1 + R2) cout << "3" << endl; 20 | else if (d == R1 + R2) cout << "4" << endl; 21 | else cout << "5" << endl; 22 | return 0; 23 | } -------------------------------------------------------------------------------- /문제풀이/chap4-1/prob4-1-3.py: -------------------------------------------------------------------------------- 1 | # 입력 2 | X1,Y1,R1 = map(float, input().split()) 3 | X2,Y2,R2 = map(float, input().split()) 4 | 5 | # 원 중심과의 거리 구하기 6 | d = (((X1 - X2) ** 2 + (Y1 - Y2) ** 2) ** 0.5) 7 | 8 | # 답 출력 9 | if d < abs(R1 - R2): 10 | print("1") 11 | elif d == abs(R1 - R2): 12 | print("2") 13 | elif d < R1 + R2: 14 | print("3") 15 | elif d == R1 + R2: 16 | print("4") 17 | else: 18 | print("5") -------------------------------------------------------------------------------- /문제풀이/chap4-1/prob4-1-4.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | const double PI = 3.14159265358979; 5 | 6 | int main() { 7 | // 입력 8 | double A, B, H, M; 9 | scanf("%lf%lf%lf%lf", &A, &B, &H, &M); 10 | 11 | // 좌표 구하기 12 | double AngleH = 30.0 * H + 0.5 * M; 13 | double AngleM = 6.0 * M; 14 | double Hx = A * cos(AngleH * PI / 180.0), Hy = A * sin(AngleH * PI / 180.0); 15 | double Mx = B * cos(AngleM * PI / 180.0), My = B * sin(AngleM * PI / 180.0); 16 | 17 | // 거리 구하기 → 출력 18 | double d = sqrt((Hx - Mx) * (Hx - Mx) + (Hy - My) * (Hy - My)); 19 | printf("%.12lf\n", d); 20 | return 0; 21 | } -------------------------------------------------------------------------------- /문제풀이/chap4-1/prob4-1-4.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | const double PI = 3.14159265358979; 6 | 7 | int main() { 8 | // 입력 9 | double A, B, H, M; 10 | cin >> A >> B >> H >> M; 11 | 12 | // 좌표 구하기 13 | double AngleH = 30.0 * H + 0.5 * M; 14 | double AngleM = 6.0 * M; 15 | double Hx = A * cos(AngleH * PI / 180.0), Hy = A * sin(AngleH * PI / 180.0); 16 | double Mx = B * cos(AngleM * PI / 180.0), My = B * sin(AngleM * PI / 180.0); 17 | 18 | // 거리 구하기 → 출력 19 | double d = sqrt((Hx - Mx) * (Hx - Mx) + (Hy - My) * (Hy - My)); 20 | printf("%.12lf\n", d); 21 | return 0; 22 | } -------------------------------------------------------------------------------- /문제풀이/chap4-1/prob4-1-4.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Main { 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | 7 | // 입력 8 | double A = sc.nextDouble(); 9 | double B = sc.nextDouble(); 10 | double H = sc.nextDouble(); 11 | double M = sc.nextDouble(); 12 | 13 | // 좌표 구하기 14 | double PI = 3.14159265358979; 15 | double AngleH = 30.0 * H + 0.5 * M; 16 | double AngleM = 6.0 * M; 17 | double Hx = A * Math.cos(AngleH * PI / 180.0); 18 | double Hy = A * Math.sin(AngleH * PI / 180.0); 19 | double Mx = B * Math.cos(AngleM * PI / 180.0); 20 | double My = B * Math.sin(AngleM * PI / 180.0); 21 | 22 | // 답 출력 23 | double d = Math.sqrt((Hx - Mx) * (Hx - Mx) + (Hy - My) * (Hy - My)); 24 | System.out.format("%.12f\n", d); 25 | } 26 | } -------------------------------------------------------------------------------- /문제풀이/chap4-1/prob4-1-4.py: -------------------------------------------------------------------------------- 1 | import math 2 | 3 | # 입력 4 | A, B, H, M = map(float, input().split()) 5 | 6 | # 좌표 구하기 7 | PI = 3.14159265358979 8 | AngleH = 30.0 * H + 0.5 * M 9 | AngleM = 6.0 * M 10 | Hx = A * math.cos(AngleH * PI / 180.0) 11 | Hy = A * math.sin(AngleH * PI / 180.0) 12 | Mx = B * math.cos(AngleM * PI / 180.0) 13 | My = B * math.sin(AngleM * PI / 180.0) 14 | 15 | # 답 출력 16 | d = (((Hx - Mx) ** 2 + (Hy - My) ** 2) ** 0.5) 17 | print("{:.12f}".format(d)) -------------------------------------------------------------------------------- /문제풀이/chap4-2/prob4-2-1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | long long N, A[200009], S[200009]; 4 | long long M, B[200009]; 5 | 6 | int main() { 7 | // 입력 8 | scanf("%lld", &N); 9 | for (int i = 1; i <= N - 1; i++) scanf("%lld", &A[i]); 10 | scanf("%lld", &M); 11 | for (int i = 1; i <= M; i++) scanf("%lld", &B[i]); 12 | 13 | // 누적합 구하기 14 | S[1] = 0; 15 | for (int i = 2; i <= N; i++) S[i] = S[i - 1] + A[i - 1]; 16 | 17 | // 답 구하기 18 | long long Answer = 0; 19 | for (int i = 1; i <= M - 1; i++) { 20 | if (B[i] < B[i + 1]) { 21 | Answer += (S[B[i + 1]] - S[B[i]]); 22 | } 23 | else { 24 | Answer += (S[B[i]] - S[B[i + 1]]); 25 | } 26 | } 27 | 28 | // 출력 29 | printf("%lld\n", Answer); 30 | return 0; 31 | } -------------------------------------------------------------------------------- /문제풀이/chap4-2/prob4-2-1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | long long N, A[200009], S[200009]; 5 | long long M, B[200009]; 6 | 7 | int main() { 8 | // 입력 9 | cin >> N; 10 | for (int i = 1; i <= N - 1; i++) cin >> A[i]; 11 | cin >> M; 12 | for (int i = 1; i <= M; i++) cin >> B[i]; 13 | 14 | // 누적합 구하기 15 | S[1] = 0; 16 | for (int i = 2; i <= N; i++) S[i] = S[i - 1] + A[i - 1]; 17 | 18 | // 답 구하기 19 | long long Answer = 0; 20 | for (int i = 1; i <= M - 1; i++) { 21 | if (B[i] < B[i + 1]) { 22 | Answer += (S[B[i + 1]] - S[B[i]]); 23 | } 24 | else { 25 | Answer += (S[B[i]] - S[B[i + 1]]); 26 | } 27 | } 28 | 29 | // 출력 30 | cout << Answer << endl; 31 | return 0; 32 | } -------------------------------------------------------------------------------- /문제풀이/chap4-2/prob4-2-1.py: -------------------------------------------------------------------------------- 1 | # 입력 2 | N = int(input()) 3 | A = list(map(int, input().split())) 4 | M = int(input()) 5 | B = [0 for i in range(M)] 6 | for i in range(M): 7 | B[i] = int(input()) 8 | 9 | # 누적합 구하기 10 | S = [0 for i in range(N)] 11 | for i in range(1, N): 12 | S[i] = S[i - 1] + A[i - 1] 13 | 14 | # 답 구하기 15 | # B[i] - 1 등으로 되는 이유는 배열의 인덱스가 0부터 시작하기 때문입니다. 16 | Answer = 0 17 | for i in range(M-1): 18 | if B[i] < B[i + 1]: 19 | Answer += S[B[i + 1] - 1] - S[B[i] - 1] 20 | else: 21 | Answer += S[B[i] - 1] - S[B[i + 1] - 1] 22 | 23 | # 출력 24 | print(Answer) -------------------------------------------------------------------------------- /문제풀이/chap4-2/prob4-2-2.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int N, T; 4 | int L[500009], R[500009]; 5 | int A[500009], B[500009]; 6 | 7 | int main() { 8 | // 입력 9 | scanf("%d%d", &T, &N); 10 | for (int i = 1; i <= N; i++) scanf("%d%d", &L[i], &R[i]); 11 | 12 | // 계차 B[i] 계산하기 13 | for (int i = 0; i <= T; i++) B[i] = 0; 14 | for (int i = 1; i <= N; i++) { 15 | B[L[i]] += 1; 16 | B[R[i]] -= 1; 17 | } 18 | 19 | // 누적합 A[i] 계산하기 20 | A[0] = B[0]; 21 | for (int i = 1; i <= T; i++) { 22 | A[i] = A[i - 1] + B[i]; 23 | } 24 | 25 | // 답 출력 26 | for (int i = 0; i < T; i++) printf("%d\n", A[i]); 27 | return 0; 28 | } -------------------------------------------------------------------------------- /문제풀이/chap4-2/prob4-2-2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int N, T; 5 | int L[500009], R[500009]; 6 | int A[500009], B[500009]; 7 | 8 | int main() { 9 | // 입력 10 | cin >> T >> N; 11 | for (int i = 1; i <= N; i++) cin >> L[i] >> R[i]; 12 | 13 | // 계차 B[i] 계산하기 14 | for (int i = 0; i <= T; i++) B[i] = 0; 15 | for (int i = 1; i <= N; i++) { 16 | B[L[i]] += 1; 17 | B[R[i]] -= 1; 18 | } 19 | 20 | // 누적합 A[i] 계산하기 21 | A[0] = B[0]; 22 | for (int i = 1; i <= T; i++) { 23 | A[i] = A[i - 1] + B[i]; 24 | } 25 | 26 | // 답 출력 27 | for (int i = 0; i < T; i++) cout << A[i] << endl; 28 | return 0; 29 | } -------------------------------------------------------------------------------- /문제풀이/chap4-2/prob4-2-2.py: -------------------------------------------------------------------------------- 1 | # 입력 2 | T = int(input()) 3 | N = int(input()) 4 | A = [0 for i in range(T + 2)] 5 | B = [0 for i in range(T + 2)] 6 | L = [0 for i in range(N)] 7 | R = [0 for i in range(N)] 8 | for i in range(N): 9 | L[i], R[i] = map(int, input().split()) 10 | 11 | # 계차 B[i] 계산하기 12 | for i in range(N): 13 | B[L[i]] += 1 14 | B[R[i]] -= 1 15 | 16 | # 누적합 A[i] 계산하기 17 | A[0] = B[0] 18 | for i in range(1, T): 19 | A[i] = A[i - 1] + B[i] 20 | 21 | # 출력 22 | for i in range(T): 23 | print(A[i]) -------------------------------------------------------------------------------- /문제풀이/chap4-3/prob4-3-2.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | double r = 2.0; // √2를 구할 것이므로 5 | double a = 2.0; // 초깃값은 적당한 수를 할당합니다. 6 | 7 | for (int i = 1; i <= 5; i++) { 8 | // 점 (a, f(a))의 x 좌표와 y 좌표를 구합니다. 9 | double japyo_x = a; 10 | double japyo_y = a * a * a; 11 | 12 | // 접선의 기울기 구하기[y = (jupseon_a)x + jupseon_b] 13 | double jupseon_a = 3.0 * japyo_x * japyo_x; 14 | double jupseon_b = japyo_y - jupseon_a * japyo_x; 15 | 16 | // 다음 a의 값 next_a를 구합니다. 17 | double next_a = (r - jupseon_b) / jupseon_a; 18 | printf("Step #%d: a = %.12lf -> %.12lf\n", i, a, next_a); 19 | a = next_a; 20 | } 21 | return 0; 22 | } -------------------------------------------------------------------------------- /문제풀이/chap4-3/prob4-3-2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | double r = 2.0; // √2를 구할 것이므로 6 | double a = 2.0; // 초깃값은 적당한 수를 할당합니다. 7 | 8 | for (int i = 1; i <= 5; i++) { 9 | // 점 (a, f(a))의 x 좌표와 y 좌표를 구합니다. 10 | double japyo_x = a; 11 | double japyo_y = a * a * a; 12 | 13 | // 접선의 기울기 구하기[y = (jupseon_a)x + jupseon_b] 14 | double jupseon_a = 3.0 * japyo_x * japyo_x; 15 | double jupseon_b = japyo_y - jupseon_a * japyo_x; 16 | 17 | // 다음 a의 값 next_a를 구합니다. 18 | double next_a = (r - jupseon_b) / jupseon_a; 19 | printf("Step #%d: a = %.12lf -> %.12lf\n", i, a, next_a); 20 | a = next_a; 21 | } 22 | return 0; 23 | } -------------------------------------------------------------------------------- /문제풀이/chap4-3/prob4-3-2.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Main { 4 | public static void main(String[] args) { 5 | double r = 2.0; // √2를 구할 것이므로 6 | double a = 2.0; // 초깃값은 적당한 수를 할당합니다. 7 | for (int i = 1; i <= 5; i++) { 8 | // 점 (a, f(a))의 x 좌표와 y 좌표를 구합니다. 9 | double japyo_x = a; 10 | double japyo_y = a * a * a; 11 | 12 | // 접선 식 y = jupseon_a * x + jupseon_b를 구합니다. 13 | double jupseon_a = 3.0 * japyo_x * japyo_x; 14 | double jupseon_b = japyo_y - jupseon_a * japyo_x; 15 | 16 | // 다음 a의 값 next_a를 구합니다. 17 | double next_a = (r - jupseon_b) / jupseon_a; 18 | System.out.format("Step #%d: a = %.12f -> %.12f\n", i, a, next_a); 19 | a = next_a; 20 | } 21 | } 22 | } -------------------------------------------------------------------------------- /문제풀이/chap4-3/prob4-3-2.py: -------------------------------------------------------------------------------- 1 | r = 2.0 # √2를 구할 것이므로 2 | a = 2.0 # 초깃값은 적당한 수를 할당합니다. 3 | repeats = 5 4 | 5 | for i in range(1, repeats + 1): 6 | # 점 (a, f(a))의 x 좌표와 y 좌표를 구합니다. 7 | japyo_x, japyo_y = a, a * a * a 8 | 9 | # 접선 식 y = jupseon_a * x + jupseon_b를 구합니다. 10 | jupseon_a = 3.0 * japyo_x * japyo_x 11 | jupseon_b = japyo_y - jupseon_a * japyo_x 12 | 13 | # 다음 a의 값 next_a를 구합니다. 14 | next_a = (r - jupseon_b) / jupseon_a 15 | print("Step #{}: a = {:.12f} -> {:.12f}".format(i, a, next_a)) 16 | a = next_a -------------------------------------------------------------------------------- /문제풀이/chap4-3/prob4-3-3.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | double l = 1.0; 5 | double r = 2.0; 6 | 7 | for (int i = 1; i <= 20; i++) { 8 | double m = (l + r) / 2.0; 9 | if (m * m < 2.0) l = m; 10 | else r = m; 11 | printf("Step #%d: m = %.12lf\n", i, m); 12 | } 13 | return 0; 14 | } -------------------------------------------------------------------------------- /문제풀이/chap4-3/prob4-3-3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | double l = 1.0; 6 | double r = 2.0; 7 | 8 | for (int i = 1; i <= 20; i++) { 9 | double m = (l + r) / 2.0; 10 | if (m * m < 2.0) l = m; 11 | else r = m; 12 | printf("Step #%d: m = %.12lf\n", i, m); 13 | } 14 | return 0; 15 | } -------------------------------------------------------------------------------- /문제풀이/chap4-3/prob4-3-3.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Main { 4 | public static void main(String[] args) { 5 | double l = 1.0; 6 | double r = 2.0; 7 | for (int i = 1; i <= 20; i++) { 8 | double m = (l + r) / 2.0; 9 | if (m * m < 2.0) l = m; 10 | else r = m; 11 | System.out.format("Step #%d: m = %.12f\n", i, m); 12 | } 13 | } 14 | } -------------------------------------------------------------------------------- /문제풀이/chap4-3/prob4-3-3.py: -------------------------------------------------------------------------------- 1 | l = 1.0 2 | r = 2.0 3 | repeats = 20 4 | 5 | for i in range(1, repeats + 1): 6 | m = (l + r) / 2.0 7 | if m * m < 2.0: 8 | l = m 9 | else: 10 | r = m 11 | print("Step #{}: m = {:.12f}".format(i, m)) -------------------------------------------------------------------------------- /문제풀이/chap4-4/prob4-4-2.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() { 5 | int N = 1000; 6 | double Answer = 0.0; 7 | 8 | for (int i = 0; i < N; i++) { 9 | double x = 1.0 * (2 * i + 1) / (2 * N); 10 | double value = pow(2.0, x * x); // f((2i+1)/2N)의 값 11 | Answer += value; 12 | } 13 | printf("%.14lf\n", Answer / N); 14 | return 0; 15 | } -------------------------------------------------------------------------------- /문제풀이/chap4-4/prob4-4-2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | int N = 1000; 7 | double Answer = 0.0; 8 | 9 | for (int i = 0; i < N; i++) { 10 | double x = 1.0 * (2 * i + 1) / (2 * N); 11 | double value = pow(2.0, x * x); // f((2i+1)/2N)의 값 12 | Answer += value; 13 | } 14 | printf("%.14lf\n", Answer / N); 15 | return 0; 16 | } -------------------------------------------------------------------------------- /문제풀이/chap4-4/prob4-4-2.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Main { 4 | public static void main(String[] args) { 5 | int N = 1000; 6 | double Answer = 0.0; 7 | 8 | for (int i = 0; i < N; i++) { 9 | double x = 1.0 * (2 * i + 1) / (2 * N); 10 | double value = Math.pow(2.0, x * x); // f((2i+1)/2N)의 값 11 | Answer += value; 12 | } 13 | 14 | System.out.format("%.12f\n", Answer / N); 15 | } 16 | } -------------------------------------------------------------------------------- /문제풀이/chap4-4/prob4-4-2.py: -------------------------------------------------------------------------------- 1 | N = 1000 2 | Answer = 0.0 3 | 4 | for i in range(N): 5 | x = 1.0 * (2 * i + 1) / (2 * N) 6 | value = 2.0 ** (x * x) 7 | Answer += value 8 | 9 | print("{:.12f}".format(Answer / N)) -------------------------------------------------------------------------------- /문제풀이/chap4-4/prob4-4-3.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | long long N; 4 | long long F[10000009]; 5 | long long Answer = 0; 6 | 7 | int main() { 8 | // 입력 → 배열 초기화 9 | scanf("%lld", &N); 10 | for (int i = 1; i <= N; i++) F[i] = 0; 11 | 12 | // F[1], F[2], ..., F[N]를 계산하기 13 | for (int i = 1; i <= N; i++) { 14 | // F[i], F[2i], F[3i], ...에 1을 추가 15 | for (int j = i; j <= N; j += i) F[j] += 1; 16 | } 17 | 18 | // 답 구하기 → 출력 19 | for (int i = 1; i <= N; i++) { 20 | Answer += 1LL * i * F[i]; 21 | } 22 | printf("%lld\n", Answer); 23 | return 0; 24 | } -------------------------------------------------------------------------------- /문제풀이/chap4-4/prob4-4-3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | long long N; 5 | long long F[10000009]; 6 | long long Answer = 0; 7 | 8 | int main() { 9 | // 입력 → 배열 초기화 10 | cin >> N; 11 | for (int i = 1; i <= N; i++) F[i] = 0; 12 | 13 | // F[1], F[2], ..., F[N]를 계산하기 14 | for (int i = 1; i <= N; i++) { 15 | // F[i], F[2i], F[3i], ...에 1을 추가 16 | for (int j = i; j <= N; j += i) F[j] += 1; 17 | } 18 | 19 | // 답 구하기 → 출력 20 | for (int i = 1; i <= N; i++) { 21 | Answer += 1LL * i * F[i]; 22 | } 23 | cout << Answer << endl; 24 | return 0; 25 | } -------------------------------------------------------------------------------- /문제풀이/chap4-4/prob4-4-3.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Main { 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | 7 | // 입력 → 배열 초기화 8 | int N = sc.nextInt(); 9 | long[] F = new long[N + 1]; 10 | for (int i = 1; i <= N; i++) F[i] = 0; 11 | 12 | // F[1], F[2], ..., F[N]를 계산하기 13 | for (int i = 1; i <= N; i++) { 14 | // F[i], F[2i], F[3i], ...에 1을 추가하기 15 | for (int j = i; j <= N; j += i) F[j] += 1; 16 | } 17 | 18 | // 답 구하기 19 | long Answer = 0; 20 | for (int i = 1; i <= N; i++) { 21 | Answer += F[i] * i; 22 | } 23 | System.out.println(Answer); 24 | } 25 | } -------------------------------------------------------------------------------- /문제풀이/chap4-4/prob4-4-3.py: -------------------------------------------------------------------------------- 1 | # 참고적으로 파이썬은 속도가 느리므로 2 | # 알고리즘 테스트에서 시간 제한을 통과하지 못할 가능성이 있습니다. 3 | # 이때는 알고리즘을 조금 수정한 "prob4-4-3_fast.py"를 참고해보세요. 4 | 5 | # 입력 6 | N = int(input()) 7 | F = [0 for i in range(N + 1)] 8 | 9 | # F[1], F[2], ..., F[N]를 계산하기 10 | for i in range(1, N + 1): 11 | # F[i], F[2i], F[3i], ...에 1을 추가 12 | for j in range(1, (N // i) + 1): 13 | F[j * i] += 1 14 | 15 | # 답 구하기 16 | Answer = 0 17 | for i in range(1, N + 1): 18 | Answer += i * F[i] 19 | 20 | # 출력 21 | print(Answer) -------------------------------------------------------------------------------- /문제풀이/chap4-4/prob4-4-3_fast.py: -------------------------------------------------------------------------------- 1 | # 이 코드의 복잡도는 O(N)입니다(5.7절의 테크닉 활용). 2 | 3 | N = int(input()) 4 | Answer = 0 5 | 6 | for i in range(1, N+1): 7 | d = (N // i) 8 | Answer += i * (d * (d + 1) // 2) 9 | 10 | print(Answer) -------------------------------------------------------------------------------- /문제풀이/chap4-4/prob4-4-4.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | // 파라미터 설정/초기화 5 | long long cnt = 0; 6 | double LIMIT = 23; // 이를 30으로 변경하면, 답을 구할 수 있음 7 | double Current = 0; 8 | 9 | // 하나씩 더해 나감 10 | while (Current < LIMIT) { 11 | cnt += 1; 12 | Current += 1.0 / cnt; 13 | } 14 | 15 | // 답 출력 16 | printf("%lld\n", cnt); 17 | return 0; 18 | } -------------------------------------------------------------------------------- /문제풀이/chap4-4/prob4-4-4.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | // 파라미터 설정/초기화 6 | long long cnt = 0; 7 | double LIMIT = 23; // 이를 30으로 변경하면, 답을 구할 수 있음 8 | double Current = 0; 9 | 10 | // 하나씩 더해 나감 11 | while (Current < LIMIT) { 12 | cnt += 1; 13 | Current += 1.0 / cnt; 14 | } 15 | 16 | // 답 출력 17 | cout << cnt << endl; 18 | return 0; 19 | } -------------------------------------------------------------------------------- /문제풀이/chap4-4/prob4-4-4.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Main { 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | int cnt = 0; 7 | double LIMIT = 23; // 이를 30으로 변경하면, 답을 구할 수 있음 8 | double Current = 0; 9 | 10 | // 하나씩 더해 나감 11 | while (Current < LIMIT) { 12 | cnt += 1; 13 | Current += 1.0 / cnt; 14 | } 15 | 16 | // 출력 17 | System.out.println(cnt); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /문제풀이/chap4-4/prob4-4-4.py: -------------------------------------------------------------------------------- 1 | cnt = 0 2 | LIMIT = 23 # 이를 30으로 변경하면, 답을 구할 수 있음 3 | Current = 0 4 | 5 | # 하나씩 더해 나감 6 | while Current < LIMIT: 7 | cnt += 1 8 | Current += 1.0 / cnt 9 | 10 | # 출력 11 | print(cnt) -------------------------------------------------------------------------------- /문제풀이/chap4-5/prob4-5-5.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int N, M; 6 | int A[100009], B[100009]; 7 | vector G[100009]; 8 | 9 | int main() { 10 | // 입력 11 | cin >> N >> M; 12 | for (int i = 1; i <= M; i++) { 13 | cin >> A[i] >> B[i]; 14 | G[A[i]].push_back(B[i]); 15 | G[B[i]].push_back(A[i]); 16 | } 17 | 18 | // 답 구하기 19 | int Answer = 0; 20 | for (int i = 1; i <= N; i++) { 21 | int cnt = 0; 22 | for (int j = 0; j < G[i].size(); j++) { 23 | // G[i][j]는 정점 i에 인접한 정점 중 j번째 정점 24 | if (G[i][j] < i) cnt += 1; 25 | } 26 | // 자기자신보다 번호가 작은 인접한 정점이 1개 있다면, answer에 1을 추가 27 | if (cnt == 1) Answer += 1; 28 | } 29 | 30 | // 출력 31 | cout << Answer << endl; 32 | return 0; 33 | } -------------------------------------------------------------------------------- /문제풀이/chap4-5/prob4-5-5.py: -------------------------------------------------------------------------------- 1 | # 입력 2 | N, M = map(int, input().split()) 3 | A = [ None ] * M 4 | B = [ None ] * M 5 | for i in range(M): 6 | A[i], B[i] = map(int, input().split()) 7 | 8 | # 인접리스트 작성 9 | G = [ list() for i in range(N + 1) ] 10 | for i in range(M): 11 | G[A[i]].append(B[i]) 12 | G[B[i]].append(A[i]) 13 | 14 | # 답 구하기 15 | answer = 0 16 | for i in range(1, N + 1): 17 | cnt = 0 18 | for j in G[i]: 19 | if j < i: 20 | cnt += 1 21 | # 자기자신보다 번호가 작은 인접한 정점이 1개 있다면, answer에 1을 추가 22 | if cnt == 1: 23 | answer += 1 24 | 25 | # 답 출력 26 | print(answer) 27 | -------------------------------------------------------------------------------- /문제풀이/chap4-6/prob4-6-3.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | const long long mod = 1000000007; 4 | long long N; 5 | 6 | long long modpow(long long a, long long b, long long m) { 7 | // 반복제곱법(p에는 a^1, a^2, a^4, a^8, ...가 할당됨) 8 | long long p = a, Answer = 1; 9 | for (int i = 0; i < 60; i++) { 10 | if ((b & (1LL << i)) != 0) { Answer *= p; Answer %= m; } 11 | p *= p; p %= m; 12 | } 13 | return Answer; 14 | } 15 | 16 | // division(a, b, m)은 a÷b mod m를 리턴하는 함수 17 | long long Division(long long a, long long b, long long m) { 18 | return (a * modpow(b, m - 2, m)) % m; 19 | } 20 | 21 | int main() { 22 | // 입력 23 | scanf("%d", &N); 24 | 25 | // 답 계산 26 | long long V = modpow(4, N + 1, mod) - 1; 27 | long long Answer = Division(V, 3, mod); 28 | 29 | // 출력 30 | printf("%lld\n", Answer); 31 | return 0; 32 | } -------------------------------------------------------------------------------- /문제풀이/chap4-6/prob4-6-3.py: -------------------------------------------------------------------------------- 1 | # 반복제곱법(p에는 a**1, a**2, a**4, a**8, ...가 할당됨) 2 | def modpow(a, b, m): 3 | p = a 4 | answer = 1 5 | for i in range(60): 6 | if (b & (1 << i)) != 0: 7 | answer = (answer * p) % m 8 | p = (p * p) % m 9 | return answer 10 | 11 | # division(a, b, m)은 a÷b mod m를 리턴하는 함수 12 | def division(a, b, m): 13 | return (a * modpow(b, m - 2, m)) % m 14 | 15 | # 입력 16 | MOD = 1000000007 17 | N = int(input()) 18 | 19 | # 답 계산 20 | V = modpow(4, N + 1, MOD) - 1 21 | Answer = division(V, 3, MOD) 22 | 23 | # 출력 24 | print(Answer) -------------------------------------------------------------------------------- /문제풀이/chap5-10/prob5-10-2.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | const long long mod = 1000000007; 4 | long long N; 5 | 6 | int main() { 7 | // 입력 8 | scanf("%lld", &N); 9 | 10 | // 답 구하기 11 | long long val = N * (N + 1) / 2; 12 | val %= mod; 13 | printf("%lld\n", val * val % mod); 14 | return 0; 15 | } -------------------------------------------------------------------------------- /문제풀이/chap5-10/prob5-10-2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const long long mod = 1000000007; 5 | long long N; 6 | 7 | int main() { 8 | // 입력 9 | cin >> N; 10 | 11 | // 답 구하기 12 | long long val = N * (N + 1) / 2; 13 | val %= mod; 14 | cout << val * val % mod << endl; 15 | return 0; 16 | } -------------------------------------------------------------------------------- /문제풀이/chap5-10/prob5-10-2.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Main { 4 | public static void main(String[] args) { 5 | // 입력 6 | Scanner sc = new Scanner(System.in); 7 | long N = sc.nextLong(); 8 | 9 | // 답 구하기 10 | long mod = 1000000007; 11 | long val = N * (N + 1) / 2; 12 | val %= mod; 13 | System.out.println(val * val % mod); 14 | } 15 | } -------------------------------------------------------------------------------- /문제풀이/chap5-10/prob5-10-2.py: -------------------------------------------------------------------------------- 1 | # 입력 2 | N = int(input()) 3 | 4 | # 답 구하기 5 | mod = 1000000007 6 | val = N * (N + 1) // 2 7 | print(val * val % mod) -------------------------------------------------------------------------------- /문제풀이/chap5-10/prob5-10-3.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | const long long mod = 998244353; 4 | long long A, B, C; 5 | 6 | int main() { 7 | // 입력 8 | scanf("%lld%lld%lld", &A, &B, &C); 9 | 10 | // 계산 11 | long long D = A * (A + 1) / 2; D %= mod; 12 | long long E = B * (B + 1) / 2; E %= mod; 13 | long long F = C * (C + 1) / 2; F %= mod; 14 | 15 | // 답 출력 16 | // 여기에서 (D * E * F) % mod로 해도, 중간에 10^27를 다루어야 할 수 있으므로 17 | // long long 자료형이라도 오버플로가 발생할 수 있으므로 주의 18 | printf("%lld\n", (D * E % mod) * F % mod); 19 | return 0; 20 | } -------------------------------------------------------------------------------- /문제풀이/chap5-10/prob5-10-3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const long long mod = 998244353; 5 | long long A, B, C; 6 | 7 | int main() { 8 | // 입력 9 | cin >> A >> B >> C; 10 | 11 | // 계산 12 | long long D = A * (A + 1) / 2; D %= mod; 13 | long long E = B * (B + 1) / 2; E %= mod; 14 | long long F = C * (C + 1) / 2; F %= mod; 15 | 16 | // 답 출력 17 | // 여기에서 (D * E * F) % mod로 해도, 중간에 10^27를 다루어야 할 수 있으므로 18 | // long long 자료형이라도 오버플로가 발생할 수 있으므로 주의 19 | cout << (D * E % mod) * F % mod << endl; 20 | return 0; 21 | } -------------------------------------------------------------------------------- /문제풀이/chap5-10/prob5-10-3.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Main { 4 | public static void main(String[] args) { 5 | // 입력 6 | Scanner sc = new Scanner(System.in); 7 | long A = sc.nextLong(); 8 | long B = sc.nextLong(); 9 | long C = sc.nextLong(); 10 | 11 | // 계산 12 | long mod = 998244353; 13 | long D = A * (A + 1) / 2; D %= mod; 14 | long E = B * (B + 1) / 2; E %= mod; 15 | long F = C * (C + 1) / 2; F %= mod; 16 | 17 | // 답 출력 18 | // 여기에서 (D * E * F) % mod로 해도, 중간에 10^27를 다루어야 할 수 있으므로 19 | // long long 자료형이라도 오버플로가 발생할 수 있으므로 주의 20 | System.out.println((D * E % mod) * F % mod); 21 | } 22 | } -------------------------------------------------------------------------------- /문제풀이/chap5-10/prob5-10-3.py: -------------------------------------------------------------------------------- 1 | # 입력 2 | A, B, C = map(int, input().split()) 3 | 4 | # 계산 5 | mod = 998244353 6 | D = A * (A + 1) // 2 7 | E = B * (B + 1) // 2 8 | F = C * (C + 1) // 2 9 | 10 | # 답 출력 11 | print((D * E * F) % mod) -------------------------------------------------------------------------------- /문제풀이/chap5-10/prob5-10-5.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | // 입력 5 | long long a, b, c; 6 | scanf("%lld%lld%lld", &a, &b, &c); 7 | 8 | // c = 1 때의 조건 분기 9 | if (c == 1) { 10 | printf("No\n"); 11 | return 0; 12 | } 13 | 14 | // 좌변 계산(c의 b 제곱) 15 | long long v = 1; 16 | for (long long i = 1; i <= b; i++) { 17 | if (a / c < v) { 18 | // 이 조건 분기는 a < (v * c)를 변경한 것입니다. 19 | // 조건을 변경한 이유는 c가 10^18를 넘을수도 있기 때문입니다. 20 | // a < v * c로 하면, 최악의 경우 v * c = 10^36가 되어 오버플로가 발생합니다. 21 | printf("Yes\n"); 22 | return 0; 23 | } 24 | v *= c; 25 | } 26 | 27 | // 반복이 끝나지 않는 경우 28 | printf("No\n"); 29 | return 0; 30 | } -------------------------------------------------------------------------------- /문제풀이/chap5-10/prob5-10-5.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | // 입력 7 | long long a, b, c; 8 | cin >> a >> b >> c; 9 | 10 | // c = 1 때의 조건 분기 11 | if (c == 1) { 12 | cout << "No" << endl; 13 | return 0; 14 | } 15 | 16 | // 좌변 계산(c의 b 제곱) 17 | long long v = 1; 18 | for (long long i = 1; i <= b; i++) { 19 | if (a / c < v) { 20 | // 이 조건 분기는 a < (v * c)를 변경한 것입니다. 21 | // 조건을 변경한 이유는 c가 10^18를 넘을수도 있기 때문입니다. 22 | // a < v * c로 하면, 최악의 경우 v * c = 10^36가 되어 오버플로가 발생합니다. 23 | cout << "Yes" << endl; 24 | return 0; 25 | } 26 | v *= c; 27 | } 28 | 29 | // 반복이 끝나지 않는 경우 30 | cout << "No" << endl; 31 | return 0; 32 | } -------------------------------------------------------------------------------- /문제풀이/chap5-10/prob5-10-5.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Main { 4 | public static void main(String[] args) { 5 | // 입력 6 | Scanner sc = new Scanner(System.in); 7 | long a = sc.nextLong(); 8 | long b = sc.nextLong(); 9 | long c = sc.nextLong(); 10 | 11 | // c = 1 때의 조건 분기 12 | if (c == 1) { 13 | System.out.println("No\n"); 14 | System.exit(0); 15 | } 16 | 17 | // 좌변 계산(c의 b 제곱) 18 | long v = 1; 19 | for (long i = 1; i <= b; i++) { 20 | if (a / c < v) { 21 | // 이 조건 분기는 a < (v * c)를 변경한 것입니다. 22 | // 조건을 변경한 이유는 c가 10^18를 넘을수도 있기 때문입니다. 23 | // a < v * c로 하면, 최악의 경우 v * c = 10^36가 되어 오버플로가 발생합니다. 24 | System.out.println("Yes\n"); 25 | System.exit(0); 26 | } 27 | v *= c; 28 | } 29 | 30 | // 반복이 끝나지 않는 경우 31 | System.out.println("No\n"); 32 | } 33 | } -------------------------------------------------------------------------------- /문제풀이/chap5-10/prob5-10-5.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | # 입력 4 | a, b, c = map(int, input().split()) 5 | 6 | # c = 1 때의 조건 분기 7 | if c == 1: 8 | print("No") 9 | sys.exit() 10 | 11 | # 좌변 계산(c의 b 제곱) 12 | v = 1 13 | for i in range(b): 14 | v *= c 15 | if a < v: 16 | print("Yes") 17 | sys.exit() 18 | 19 | # 출력(No의 경우) 20 | print("No") -------------------------------------------------------------------------------- /문제풀이/chap5-2/prob5-2-2.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() { 5 | // 입력 6 | long long N; 7 | scanf("%lld", &N); 8 | 9 | // N = 2^k-1 형태로 나타낼 수 있는지 확인하기 10 | bool flag = false; 11 | for (int k = 1; k <= 60; k++) { 12 | if (N == (1LL << k) - 1LL) flag = true; 13 | } 14 | 15 | // 출력 16 | if (flag == true) printf("Second\n"); 17 | else printf("First\n"); 18 | return 0; 19 | } -------------------------------------------------------------------------------- /문제풀이/chap5-2/prob5-2-2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | // 입력 6 | long long N; 7 | cin >> N; 8 | 9 | // N = 2^k-1 형태로 나타낼 수 있는지 확인하기 10 | bool flag = false; 11 | for (int k = 1; k <= 60; k++) { 12 | if (N == (1LL << k) - 1LL) flag = true; 13 | } 14 | 15 | // 출력 16 | if (flag == true) cout << "Second" << endl; 17 | else cout << "First" << endl; 18 | return 0; 19 | } -------------------------------------------------------------------------------- /문제풀이/chap5-2/prob5-2-2.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Main { 4 | public static void main(String[] args) { 5 | // 입력 6 | Scanner sc = new Scanner(System.in); 7 | long N = sc.nextLong(); 8 | 9 | // N = 2^k-1 형태로 나타낼 수 있는지 확인하기 10 | boolean flag = false; 11 | for (int i = 1; i <= 60; i++) { 12 | if (N == (1L << i) - 1) { 13 | flag = true; 14 | } 15 | } 16 | 17 | // 출력 18 | if (flag == true) System.out.println("Second"); 19 | else System.out.println("First"); 20 | } 21 | } -------------------------------------------------------------------------------- /문제풀이/chap5-2/prob5-2-2.py: -------------------------------------------------------------------------------- 1 | # 입력 2 | N = int(input()) 3 | 4 | # N = 2^k-1 형태로 나타낼 수 있는지 확인하기 5 | flag = False 6 | for i in range(1, 60): 7 | if N == (2 ** i) - 1: 8 | flag = True 9 | 10 | if flag == True: 11 | print("Second") 12 | else: 13 | print("First") -------------------------------------------------------------------------------- /문제풀이/chap5-2/prob5-2-3.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | # 입력 4 | N, K = map(int, input().split()) 5 | A = list(map(int, input().split())) 6 | 7 | # 배열 초기화 8 | First = [-1 for i in range(N+1)] 9 | Second = [-1 for i in range(N+1)] 10 | 11 | # 답 구하기 12 | # cur은 현재 있는 마을의 번호 13 | cnt = 0 14 | cur = 1 15 | while True: 16 | # First, Second 변경 17 | if First[cur] == -1: 18 | First[cur] = cnt 19 | elif Second[cur] == -1: 20 | Second[cur] = cnt 21 | 22 | # K회 이동 후에 마을 cur에 있는지 판정 23 | if cnt == K: 24 | print(cur) 25 | sys.exit() 26 | elif Second[cur] != -1 and (K - First[cur]) % (Second[cur] - First[cur]) == 0: 27 | print(cur) 28 | sys.exit() 29 | 30 | cur = A[cur - 1] 31 | cnt += 1 -------------------------------------------------------------------------------- /문제풀이/chap5-3/prob5-3-1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | // 입력 5 | long long H, W; 6 | scanf("%lld%lld", &H, &W); 7 | 8 | // 조건 분기 9 | if (H == 1 || W == 1) { 10 | printf("1\n"); 11 | } 12 | else { 13 | printf("%lld\n", (H * W + 1LL) / 2LL); 14 | } 15 | return 0; 16 | } -------------------------------------------------------------------------------- /문제풀이/chap5-3/prob5-3-1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | // 입력 6 | long long H, W; 7 | cin >> H >> W; 8 | 9 | // 조건 분기 10 | if (H == 1 || W == 1) { 11 | cout << "1" << endl; 12 | } 13 | else { 14 | cout << (H * W + 1) / 2 << endl; 15 | } 16 | return 0; 17 | } -------------------------------------------------------------------------------- /문제풀이/chap5-3/prob5-3-1.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Main { 4 | public static void main(String[] args) { 5 | // 입력 6 | Scanner sc = new Scanner(System.in); 7 | long H = sc.nextLong(); 8 | long W = sc.nextLong(); 9 | 10 | // 조건 분기 11 | if (H == 1 || W == 1) { 12 | System.out.println(1); 13 | } 14 | else { 15 | System.out.println((H * W + 1) / 2); 16 | } 17 | } 18 | } -------------------------------------------------------------------------------- /문제풀이/chap5-3/prob5-3-1.py: -------------------------------------------------------------------------------- 1 | # 입력 2 | H, W = map(int, input().split()) 3 | 4 | # 조건 분기 5 | if H == 1 or W == 1: 6 | print(1) 7 | else: 8 | print((H * W + 1) // 2) -------------------------------------------------------------------------------- /문제풀이/chap5-4/prob5-4-2.py: -------------------------------------------------------------------------------- 1 | # 입력 2 | H, W = map(int, input().split()) 3 | A = [[] for i in range(H)] 4 | for i in range(H): 5 | A[i] = list(map(int, input().split())) 6 | 7 | # 행의 총합 계산하기 8 | gyou = [0 for i in range(H)] 9 | for i in range(H): 10 | for j in range(W): 11 | gyou[i] += A[i][j] 12 | 13 | # 열의 총합 계산하기 14 | retu = [0 for i in range(W)] 15 | for j in range(W): 16 | for i in range(H): 17 | retu[j] += A[i][j] 18 | 19 | # 각 칸의 답 계산하기 20 | Answer = [[0 for j in range(W)] for i in range(H)] 21 | for i in range(H): 22 | for j in range(W): 23 | Answer[i][j] = gyou[i] + retu[j] - A[i][j] 24 | 25 | # 출력 26 | for i in range(H): 27 | print(*Answer[i]) -------------------------------------------------------------------------------- /문제풀이/chap5-4/prob5-4-5.py: -------------------------------------------------------------------------------- 1 | # 최대공약수를 리턴하는 함수 2 | def GCD(A, B): 3 | while A >= 1 and B >= 1: 4 | if A < B: 5 | B = B % A # A < B라면, 큰 수를 b로 변경합니다. 6 | else: 7 | A = A % B # A >= B라면 큰 수를 a로 변경합니다. 8 | if A >= 1: 9 | return A 10 | return B 11 | 12 | # 최소공배수를 리턴하는 함수 13 | def LCM(A, B): 14 | return int(A / GCD(A, B)) * B 15 | 16 | # 입력 17 | N, K = map(int, input().split()) 18 | V = list(map(int, input().split())) 19 | 20 | # 비트 전체 탐색 21 | Answer = 0 22 | for i in range(1, 1 << K): 23 | cnt = 0 24 | lcm = 1 25 | for j in range(K): 26 | if (i & (1 << j)) != 0: 27 | cnt += 1 28 | lcm = LCM(lcm, V[j]) 29 | 30 | # num은 N 이하 중에서 선택된 숫자 모두의 배수인 것의 개수 31 | num = N // lcm 32 | if cnt % 2 == 1: 33 | Answer += num 34 | else: 35 | Answer -= num 36 | 37 | # 출력 38 | print(Answer) -------------------------------------------------------------------------------- /문제풀이/chap5-6/prob5-6-2.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | const long long mod = 1000000007; 4 | long long N; 5 | long long A[300009]; 6 | long long power[300009]; 7 | 8 | int main() { 9 | // 입력 10 | scanf("%lld", &N); 11 | for (int i = 1; i <= N; i++) scanf("%lld", &A[i]); 12 | 13 | // 2^i 구하기 14 | power[0] = 1; 15 | for (int i = 1; i <= N; i++) { 16 | power[i] = (2 * power[i - 1]) % mod; 17 | } 18 | 19 | // 답 구하기 20 | long long Answer = 0; 21 | for (int i = 1; i <= N; i++) { 22 | Answer += power[i - 1] * A[i]; 23 | Answer %= mod; 24 | } 25 | 26 | // 출력 27 | printf("%lld\n", Answer); 28 | return 0; 29 | } -------------------------------------------------------------------------------- /문제풀이/chap5-6/prob5-6-2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const long long mod = 1000000007; 5 | long long N; 6 | long long A[300009]; 7 | long long power[300009]; 8 | 9 | int main() { 10 | // 입력 11 | cin >> N; 12 | for (int i = 1; i <= N; i++) cin >> A[i]; 13 | 14 | // 2^i 구하기 15 | power[0] = 1; 16 | for (int i = 1; i <= N; i++) { 17 | power[i] = (2 * power[i - 1]) % mod; 18 | } 19 | 20 | // 답 구하기 21 | long long Answer = 0; 22 | for (int i = 1; i <= N; i++) { 23 | Answer += power[i - 1] * A[i]; 24 | Answer %= mod; 25 | } 26 | 27 | // 출력 28 | cout << Answer << endl; 29 | return 0; 30 | } -------------------------------------------------------------------------------- /문제풀이/chap5-6/prob5-6-2.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Main { 4 | public static void main(String[] args) { 5 | // 입력 6 | Scanner sc = new Scanner(System.in); 7 | int N = sc.nextInt(); 8 | long[] A = new long[N + 1]; 9 | for (int i = 1; i <= N; i++) { 10 | A[i] = sc.nextLong(); 11 | } 12 | 13 | // 2^i 구하기 14 | final long mod = 1000000007; 15 | long[] power = new long[N + 1]; 16 | power[0] = 1; 17 | for (int i = 1; i <= N; i++) { 18 | power[i] = (2 * power[i - 1]) % mod; 19 | } 20 | 21 | // 답 구하기 22 | long Answer = 0; 23 | for (int i = 1; i <= N; i++) { 24 | Answer += power[i - 1] * A[i]; 25 | Answer %= mod; 26 | } 27 | 28 | // 출력 29 | System.out.println(Answer); 30 | } 31 | } -------------------------------------------------------------------------------- /문제풀이/chap5-6/prob5-6-2.py: -------------------------------------------------------------------------------- 1 | # 입력 2 | N = int(input()) 3 | A = list(map(int, input().split())) 4 | 5 | # 2^i 구하기 6 | mod = 1000000007 7 | power = [0 for i in range(N)] 8 | power[0] = 1 9 | for i in range(1, N): 10 | power[i] = (2 * power[i - 1]) % mod 11 | 12 | # 답 구하기 13 | Answer = 0 14 | for i in range(N): 15 | Answer += power[i] * A[i] 16 | Answer %= mod 17 | 18 | # 출력 19 | print(Answer) -------------------------------------------------------------------------------- /문제풀이/chap5-7/prob5-7-3.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int compare_values(const void* a, const void* b) { 5 | // a가 앞에 있으면 양수, 6 | // b가 앞에 있으면 음수, 7 | // a와 b가 같은 값이면 0을 리턴하는 함수 8 | if (*(int*)a < *(int*)b) return -1; 9 | if (*(int*)a > *(int*)b) return +1; 10 | return 0; 11 | } 12 | 13 | long long N, A[200009]; 14 | long long Answer = 0; 15 | 16 | int main() { 17 | // 입력 18 | scanf("%lld", &N); 19 | for (int i = 1; i <= N; i++) scanf("%lld\n", &A[i]); 20 | 21 | // 정렬(코드 5.7.1에서 추가한 유일한 부분) 22 | qsort(A + 1, N, sizeof(long long), compare_values); 23 | 24 | // 답 구하기 → 답 출력 25 | for (int i = 1; i <= N; i++) Answer += A[i] * (-N + 2LL * i - 1LL); 26 | printf("%lld\n", Answer); 27 | return 0; 28 | } -------------------------------------------------------------------------------- /문제풀이/chap5-7/prob5-7-3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | long long N, A[200009]; 6 | long long Answer = 0; 7 | 8 | int main() { 9 | // 입력 10 | cin >> N; 11 | for (int i = 1; i <= N; i++) cin >> A[i]; 12 | 13 | // 정렬(코드 5.7.1에서 추가한 유일한 부분) 14 | sort(A + 1, A + N + 1); 15 | 16 | // 답 구하기 → 답 출력 17 | for (int i = 1; i <= N; i++) Answer += A[i] * (-N + 2LL * i - 1LL); 18 | cout << Answer << endl; 19 | return 0; 20 | } -------------------------------------------------------------------------------- /문제풀이/chap5-7/prob5-7-3.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Main { 4 | public static void main(String[] args) { 5 | // 입력 6 | Scanner sc = new Scanner(System.in); 7 | int N = sc.nextInt(); 8 | long[] A = new long[N]; 9 | for (int i = 1; i <= N; i++) { 10 | A[i - 1] = sc.nextLong(); 11 | } 12 | 13 | // 배열A 전체를 정렬 14 | Arrays.sort(A); 15 | 16 | // 답 구하기 17 | long Answer = 0; 18 | for (int i = 1; i <= N; i++) { 19 | Answer += A[i - 1] * (-N + 2L * i - 1L); 20 | } 21 | 22 | // 출력 23 | System.out.println(Answer); 24 | } 25 | } -------------------------------------------------------------------------------- /문제풀이/chap5-7/prob5-7-3.py: -------------------------------------------------------------------------------- 1 | # 입력 2 | N = int(input()) 3 | A = list(map(int, input().split())) 4 | 5 | # 배열A 전체를 정렬 6 | A.sort() 7 | 8 | # 답 구하기 9 | Answer = 0 10 | for i in range(1, N + 1): 11 | Answer += A[i - 1] * (-N + 2 * i - 1) 12 | 13 | # 출력 14 | print(Answer) -------------------------------------------------------------------------------- /문제풀이/chap5-7/prob5-7-4.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | long long N; 6 | long long X[200009], Y[200009]; 7 | 8 | int main() { 9 | // 입력 10 | cin >> N; 11 | for (int i = 1; i <= N; i++) cin >> X[i] >> Y[i]; 12 | 13 | // 배열 정렬하기 14 | sort(X + 1, X + N + 1); 15 | sort(Y + 1, Y + N + 1); 16 | 17 | // Part1의 답(x 좌표 차의 절댓값 총합) 18 | long long Part1 = 0; 19 | for (int i = 1; i <= N; i++) Part1 += X[i] * (-N + 2LL * i - 1LL); 20 | 21 | // Part2의 답(y 좌표 차의 절댓값 총합) 22 | long long Part2 = 0; 23 | for (int i = 1; i <= N; i++) Part2 += Y[i] * (-N + 2LL * i - 1LL); 24 | 25 | // 출력 26 | cout << Part1 + Part2 << endl; 27 | return 0; 28 | } -------------------------------------------------------------------------------- /문제풀이/chap5-7/prob5-7-4.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Main { 4 | public static void main(String[] args) { 5 | // 입력 6 | Scanner sc = new Scanner(System.in); 7 | int N = sc.nextInt(); 8 | long[] X = new long[N]; 9 | long[] Y = new long[N]; 10 | for (int i = 1; i <= N; i++) { 11 | X[i - 1] = sc.nextLong(); 12 | Y[i - 1] = sc.nextLong(); 13 | } 14 | 15 | // 배열 X, Y 정렬하기 16 | Arrays.sort(X); 17 | Arrays.sort(Y); 18 | 19 | // Part1의 답(x 좌표 차의 절댓값 총합) 20 | long Part1 = 0; 21 | for (int i = 1; i <= N; i++) Part1 += X[i - 1] * (-N + 2L * i - 1L); 22 | 23 | // Part2의 답(y 좌표 차의 절댓값 총합) 24 | long Part2 = 0; 25 | for (int i = 1; i <= N; i++) Part2 += Y[i - 1] * (-N + 2L * i - 1L); 26 | 27 | // 출력 28 | System.out.println(Part1 + Part2); 29 | } 30 | } -------------------------------------------------------------------------------- /문제풀이/chap5-7/prob5-7-4.py: -------------------------------------------------------------------------------- 1 | # 입력 2 | N = int(input()) 3 | X = [0 for i in range(N)] 4 | Y = [0 for i in range(N)] 5 | for i in range(N): 6 | X[i], Y[i] = map(int, input().split()) 7 | 8 | # 배열 X, Y 정렬하기 9 | X.sort() 10 | Y.sort() 11 | 12 | # Part1의 답(x 좌표 차의 절댓값 총합) 13 | Part1 = 0 14 | for i in range(1, N + 1): 15 | Part1 += X[i - 1] * (-N + 2 * i - 1); 16 | 17 | # Part2의 답(y 좌표 차의 절댓값 총합) 18 | Part2 = 0 19 | for i in range(1, N + 1): 20 | Part2 += Y[i - 1] * (-N + 2 * i - 1); 21 | 22 | # 출력 23 | print(Part1 + Part2) -------------------------------------------------------------------------------- /문제풀이/chap5-8/prob5-8-2.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | // 입력 5 | long long N; 6 | scanf("%lld", &N); 7 | 8 | // 출력 9 | printf("%lld\n", N * (N - 1) / 2); 10 | return 0; 11 | } -------------------------------------------------------------------------------- /문제풀이/chap5-8/prob5-8-2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | // 입력 6 | long long N; 7 | cin >> N; 8 | 9 | // 출력 10 | cout << N * (N - 1) / 2 << endl; 11 | return 0; 12 | } -------------------------------------------------------------------------------- /문제풀이/chap5-8/prob5-8-2.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Main { 4 | public static void main(String[] args) { 5 | // 입력 6 | Scanner sc = new Scanner(System.in); 7 | long N = sc.nextLong(); 8 | 9 | // 출력 10 | System.out.println(N * (N - 1) / 2); 11 | } 12 | } -------------------------------------------------------------------------------- /문제풀이/chap5-8/prob5-8-2.py: -------------------------------------------------------------------------------- 1 | # 입력 2 | N = int(input()) 3 | 4 | # 출력 5 | print(N * (N - 1) // 2) -------------------------------------------------------------------------------- /문제풀이/chap5-9/prob5-9-1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | // 입력 5 | long long N, Answer = 0; 6 | scanf("%lld", &N); 7 | 8 | // 10000원으로 지불 9 | Answer += (N / 10000); N %= 10000; 10 | 11 | // 5000원으로 지불 12 | Answer += (N / 5000); N %= 5000; 13 | 14 | // 1000원으로 지불 15 | Answer += (N / 1000); N %= 1000; 16 | 17 | // 답 출력 18 | printf("%lld\n", Answer); 19 | return 0; 20 | } -------------------------------------------------------------------------------- /문제풀이/chap5-9/prob5-9-1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | // 입력 6 | long long N, Answer = 0; 7 | cin >> N; 8 | 9 | // 10000원으로 지불 10 | Answer += (N / 10000); N %= 10000; 11 | 12 | // 5000원으로 지불 13 | Answer += (N / 5000); N %= 5000; 14 | 15 | // 1000원으로 지불 16 | Answer += (N / 1000); N %= 1000; 17 | 18 | // 답 출력 19 | cout << Answer << endl; 20 | return 0; 21 | } -------------------------------------------------------------------------------- /문제풀이/chap5-9/prob5-9-1.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Main { 4 | public static void main(String[] args) { 5 | // 입력 6 | Scanner sc = new Scanner(System.in); 7 | long N = sc.nextLong(); 8 | long Answer = 0; 9 | 10 | // 10000원으로 지불 11 | Answer += (N / 10000); N %= 10000; 12 | 13 | // 5000원으로 지불 14 | Answer += (N / 5000); N %= 5000; 15 | 16 | // 1000원으로 지불 17 | Answer += (N / 1000); N %= 1000; 18 | 19 | // 출력 20 | System.out.println(Answer); 21 | } 22 | } -------------------------------------------------------------------------------- /문제풀이/chap5-9/prob5-9-1.py: -------------------------------------------------------------------------------- 1 | # 입력 2 | N = int(input()) 3 | Answer = 0 4 | 5 | # 10000원으로 지불 6 | Answer += (N // 10000) 7 | N %= 10000 8 | 9 | # 5000원으로 지불 10 | Answer += (N // 5000) 11 | N %= 5000 12 | 13 | # 1000원으로 지불 14 | Answer += (N // 1000) 15 | N %= 1000 16 | 17 | # 출력 18 | print(Answer) -------------------------------------------------------------------------------- /문제풀이/chap5-9/prob5-9-2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | long long N; 7 | long long A[100009], B[100009]; 8 | 9 | int main() { 10 | // 입력 11 | cin >> N; 12 | for (int i = 1; i <= N; i++) cin >> A[i]; 13 | for (int i = 1; i <= N; i++) cin >> B[i]; 14 | 15 | // 정렬 16 | sort(A + 1, A + N + 1); 17 | sort(B + 1, B + N + 1); 18 | 19 | // 답 구하기 20 | long long Answer = 0; 21 | for (int i = 1; i <= N; i++) Answer += abs(A[i] - B[i]); 22 | cout << Answer << endl; 23 | return 0; 24 | } -------------------------------------------------------------------------------- /문제풀이/chap5-9/prob5-9-2.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Main { 4 | public static void main(String[] args) { 5 | // 입력 6 | Scanner sc = new Scanner(System.in); 7 | int N = sc.nextInt(); 8 | long[] A = new long[N]; 9 | long[] B = new long[N]; 10 | for (int i = 1; i <= N; i++) A[i - 1] = sc.nextLong(); 11 | for (int i = 1; i <= N; i++) B[i - 1] = sc.nextLong(); 12 | 13 | // 배열 A, B 정렬하기 14 | Arrays.sort(A); 15 | Arrays.sort(B); 16 | 17 | // 답 구하기 18 | long Answer = 0; 19 | for (int i = 1; i <= N; i++) { 20 | Answer += Math.abs(A[i - 1] - B[i - 1]); 21 | } 22 | 23 | // 출력 24 | System.out.println(Answer); 25 | } 26 | } -------------------------------------------------------------------------------- /문제풀이/chap5-9/prob5-9-2.py: -------------------------------------------------------------------------------- 1 | # 입력 2 | N = int(input()) 3 | A = list(map(int, input().split())) 4 | B = list(map(int, input().split())) 5 | 6 | # 배열 A, B 정렬하기 7 | A.sort() 8 | B.sort() 9 | 10 | # 답 구하기 11 | Answer = 0 12 | for i in range(N): 13 | Answer += abs(A[i] - B[i]) 14 | 15 | # 출력 16 | print(Answer) -------------------------------------------------------------------------------- /문제풀이/chap5-9/prob5-9-3.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Main { 4 | public static void main(String[] args) { 5 | // 입력 6 | Scanner sc = new Scanner(System.in); 7 | int N = sc.nextInt(); 8 | int[][] A = new int[N][2]; 9 | for (int i = 1; i <= N; i++) { 10 | A[i - 1][0] = sc.nextInt(); 11 | A[i - 1][1] = sc.nextInt(); 12 | } 13 | 14 | // 배열A를 종료 시각(A[x][1])으로 오름차순 정렬 15 | Arrays.sort(A, (a1, a2) -> Integer.compare(a1[1], a2[1])); 16 | 17 | // 종료 시각이 가장 빠른 것 선택하기를 반복함 18 | int CurrentTime = 0; 19 | int Answer = 0; 20 | for (int i = 1; i <= N; i++) { 21 | if (CurrentTime <= A[i - 1][0]) { 22 | CurrentTime = A[i - 1][1]; 23 | Answer += 1; 24 | } 25 | } 26 | 27 | // 출력 28 | System.out.println(Answer); 29 | } 30 | } -------------------------------------------------------------------------------- /문제풀이/chap5-9/prob5-9-3.py: -------------------------------------------------------------------------------- 1 | # 입력 2 | # A는 (종료 시각, 시작 시각) 형태이므로 주의하기(종료 시각 순서로 쉽게 정렬하려고 이런 형태로 만듦) 3 | N = int(input()) 4 | A = [] 5 | for i in range(N): 6 | a, b = map(int, input().split()) 7 | A.append([b, a]) 8 | 9 | # 정렬 10 | A.sort() 11 | 12 | # 종료 시각이 가장 빠른 것 선택하기를 반복함 13 | CurrentTime = 0 14 | Answer = 0 15 | for i in range(N): 16 | if CurrentTime <= A[i][1]: 17 | CurrentTime = A[i][0] 18 | Answer += 1 19 | 20 | # 출력 21 | print(Answer) -------------------------------------------------------------------------------- /문제풀이/chap6-16_20/prob6-16.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | // 입력 5 | int N, X; 6 | scanf("%d%d", &N, &X); 7 | 8 | // 모든 (a, b, c) 조합 확인하기 9 | int answer = 0; 10 | int a, b, c; 11 | for (a = 1; a <= N; a++) { 12 | for (b = a + 1; b <= N; b++) { 13 | for (c = b + 1; c <= N; c++) { 14 | if (a + b + c == X) { 15 | answer += 1; 16 | } 17 | } 18 | } 19 | } 20 | 21 | // 답 출력 22 | printf("%d\n", answer); 23 | 24 | return 0; 25 | } -------------------------------------------------------------------------------- /문제풀이/chap6-16_20/prob6-16.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | // 입력 6 | int N, X; 7 | cin >> N >> X; 8 | 9 | // 모든 (a, b, c) 조합 확인하기 10 | int answer = 0; 11 | for (int a = 1; a <= N; a++) { 12 | for (int b = a + 1; b <= N; b++) { 13 | for (int c = b + 1; c <= N; c++) { 14 | if (a + b + c == X) { 15 | answer += 1; 16 | } 17 | } 18 | } 19 | } 20 | 21 | // 답 출력 22 | cout << answer << endl; 23 | 24 | return 0; 25 | } -------------------------------------------------------------------------------- /문제풀이/chap6-16_20/prob6-16.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Main { 4 | public static void main(String[] args) { 5 | // 입력 6 | Scanner sc = new Scanner(System.in); 7 | int N = sc.nextInt(); 8 | int X = sc.nextInt(); 9 | 10 | // 모든 (a, b, c) 조합 확인하기 11 | int answer = 0; 12 | for (int a = 1; a <= N; a++) { 13 | for (int b = a + 1; b <= N; b++) { 14 | for (int c = b + 1; c <= N; c++) { 15 | if (a + b + c == X) { 16 | answer += 1; 17 | } 18 | } 19 | } 20 | } 21 | 22 | // 답 출력 23 | System.out.println(answer); 24 | } 25 | } -------------------------------------------------------------------------------- /문제풀이/chap6-16_20/prob6-16.py: -------------------------------------------------------------------------------- 1 | # 입력 2 | N, X = map(int, input().split()) 3 | 4 | # 모든 (a, b, c) 조합 확인하기 5 | answer = 0 6 | for a in range(1, N + 1): 7 | for b in range(a + 1, N + 1): 8 | for c in range(b + 1, N + 1): 9 | if a + b + c == X: 10 | answer += 1 11 | 12 | # 답 출력 13 | print(answer) -------------------------------------------------------------------------------- /문제풀이/chap6-16_20/prob6-17.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | // 입력 5 | long long N; 6 | scanf("%lld", &N); 7 | 8 | // 세로 길이를 1부터 √N까지 전체 탐색 9 | long long answer = (1LL << 60); 10 | long long x; 11 | for (x = 1; x * x <= N; x++) { 12 | if (N % x == 0 && answer > 2 * x + 2 * (N / x)) { 13 | answer = 2 * x + 2 * (N / x); 14 | } 15 | } 16 | 17 | // 답 출력 18 | printf("%lld\n", answer); 19 | 20 | return 0; 21 | } -------------------------------------------------------------------------------- /문제풀이/chap6-16_20/prob6-17.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | // 입력 7 | long long N; 8 | cin >> N; 9 | 10 | // 세로 길이를 1부터 √N까지 전체 탐색 11 | long long answer = (1LL << 60); 12 | for (long long x = 1; x * x <= N; x++) { 13 | if (N % x == 0) { 14 | answer = min(answer, 2 * x + 2 * (N / x)); 15 | } 16 | } 17 | 18 | // 답 출력 19 | cout << answer << endl; 20 | 21 | return 0; 22 | } -------------------------------------------------------------------------------- /문제풀이/chap6-16_20/prob6-17.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Main { 4 | public static void main(String[] args) { 5 | // 입력 6 | Scanner sc = new Scanner(System.in); 7 | long N = sc.nextLong(); 8 | 9 | // 세로 길이를 1부터 √N까지 전체 탐색 10 | long answer = (1L << 60); 11 | for (long x = 1; x * x <= N; x++) { 12 | if (N % x == 0) { 13 | answer = Math.min(answer, 2 * x + 2 * (N / x)); 14 | } 15 | } 16 | 17 | // 답 출력 18 | System.out.println(answer); 19 | } 20 | } -------------------------------------------------------------------------------- /문제풀이/chap6-16_20/prob6-17.py: -------------------------------------------------------------------------------- 1 | # 입력 2 | N = int(input()) 3 | 4 | # 세로 길이를 1부터 √N까지 전체 탐색 5 | LIMIT = int(N ** 0.5) 6 | answer = 10 ** 19 7 | for i in range(1, LIMIT + 1): 8 | if N % i == 0: 9 | answer = min(answer, 2 * i + 2 * (N // i)) 10 | 11 | # 답 출력 12 | print(answer) -------------------------------------------------------------------------------- /문제풀이/chap6-16_20/prob6-18.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | long long GCD(long long A, long long B) { 4 | if (B == 0) return A; 5 | return GCD(B, A % B); 6 | } 7 | 8 | int main() { 9 | // 입력 10 | long long A, B; 11 | scanf("%lld%lld", &A, &B); 12 | 13 | // 최소공배수가 10^18을 넘는지 판정 14 | if (A / GCD(A, B) > 1000000000000000000 / B) { 15 | printf("Large\n"); 16 | } 17 | else { 18 | printf("%lld\n", A / GCD(A, B) * B); 19 | } 20 | return 0; 21 | } -------------------------------------------------------------------------------- /문제풀이/chap6-16_20/prob6-18.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | long long GCD(long long A, long long B) { 5 | if (B == 0) return A; 6 | return GCD(B, A % B); 7 | } 8 | 9 | int main() { 10 | // 입력 11 | long long A, B; 12 | cin >> A >> B; 13 | 14 | // 최소공배수가 10^18을 넘는지 판정 15 | if (A / GCD(A, B) > 1000000000000000000 / B) { 16 | cout << "Large" << endl; 17 | } 18 | else { 19 | cout << A / GCD(A, B) * B << endl; 20 | } 21 | return 0; 22 | } -------------------------------------------------------------------------------- /문제풀이/chap6-16_20/prob6-18.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Main { 4 | public static void main(String[] args) { 5 | // 입력 6 | Scanner sc = new Scanner(System.in); 7 | long A = sc.nextLong(); 8 | long B = sc.nextLong(); 9 | 10 | // 최소공배수가 10^18을 넘는지 판정 11 | if (A / GCD(A, B) > 1000000000000000000L / B) { 12 | System.out.println("Large"); 13 | } 14 | else { 15 | System.out.println(A / GCD(A, B) * B); 16 | } 17 | } 18 | static long GCD(long A, long B) { 19 | if (B == 0) return A; 20 | return GCD(B, A % B); 21 | } 22 | } -------------------------------------------------------------------------------- /문제풀이/chap6-16_20/prob6-18.py: -------------------------------------------------------------------------------- 1 | # 유클리드 호제법을 사용해서 최대공약수를 구합니다(다른 방법으로는 표준 라이브러리의 math.gcd(A, B)를 사용하는 방법이 있습니다). 2 | def GCD(A, B): 3 | if B == 0: 4 | return A 5 | return GCD(B, A % B) 6 | 7 | # 입력 8 | A, B = map(int, input().split()) 9 | 10 | # 최소 공배수 구하기(참고: Python에서는 오버플로를 신경쓰지 않아도 됩니다) 11 | L = A * B // GCD(A, B) 12 | if L > 10 ** 18: 13 | print("Large") 14 | else: 15 | print(L) -------------------------------------------------------------------------------- /문제풀이/chap6-16_20/prob6-19.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int N, B[109]; 4 | 5 | int main() { 6 | // 입력 7 | scanf("%d", &N); 8 | int i; 9 | for (i = 1; i <= N - 1; i++) { 10 | scanf("%d", &B[i]); 11 | } 12 | 13 | // 수열A의 요소 합계 구하기 14 | int answer = B[1] + B[N - 1]; 15 | for (i = 2; i <= N - 1; i++) { 16 | answer += (B[i - 1] < B[i] ? B[i - 1] : B[i]); 17 | } 18 | 19 | // 답 출력 20 | printf("%d\n", answer); 21 | 22 | return 0; 23 | } -------------------------------------------------------------------------------- /문제풀이/chap6-16_20/prob6-19.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int N, B[109]; 6 | 7 | int main() { 8 | // 입력 9 | cin >> N; 10 | for (int i = 1; i <= N - 1; i++) { 11 | cin >> B[i]; 12 | } 13 | 14 | // 수열A의 요소 합계 구하고 출력하기 15 | int answer = B[1] + B[N - 1]; 16 | for (int i = 2; i <= N - 1; i++) { 17 | answer += min(B[i - 1], B[i]); 18 | } 19 | cout << answer << endl; 20 | 21 | return 0; 22 | } -------------------------------------------------------------------------------- /문제풀이/chap6-16_20/prob6-19.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Main { 4 | public static void main(String[] args) { 5 | // 입력 6 | Scanner sc = new Scanner(System.in); 7 | int N = sc.nextInt(); 8 | int[] B = new int[N]; 9 | for (int i = 1; i <= N - 1; i++) { 10 | B[i] = sc.nextInt(); 11 | } 12 | 13 | // 수열A의 요소 합계 구하기 14 | int answer = B[1] + B[N - 1]; 15 | for (int i = 2; i <= N - 1; i++) { 16 | answer += Math.min(B[i - 1], B[i]); 17 | } 18 | 19 | // 답 출력 20 | System.out.println(answer); 21 | } 22 | } -------------------------------------------------------------------------------- /문제풀이/chap6-16_20/prob6-19.py: -------------------------------------------------------------------------------- 1 | # 입력(배열B의 인덱스가 0부터 시작하므로 주의) 2 | N = int(input()) 3 | B = list(map(int, input().split())) 4 | 5 | # 수열A의 요소 합계 구하기 6 | answer = B[0] + B[N - 2] 7 | for i in range(1, N - 1): 8 | answer += min(B[i - 1], B[i]) 9 | 10 | # 답 출력 11 | print(answer) -------------------------------------------------------------------------------- /문제풀이/chap6-16_20/prob6-20.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int N, Q, C[100009], P[100009], L[100009], R[100009], S1[100009], S2[100009]; 4 | 5 | int main() { 6 | // 입력 7 | scanf("%d", &N); 8 | int i; 9 | for (i = 1; i <= N; i++) { 10 | scanf("%d%d", &C[i], &P[i]); 11 | } 12 | 13 | // 누적합 구하기 14 | for (i = 1; i <= N; i++) { 15 | S1[i] = S1[i - 1] + (C[i] == 1 ? P[i] : 0); 16 | } 17 | for (i = 1; i <= N; i++) { 18 | S2[i] = S2[i - 1] + (C[i] == 2 ? P[i] : 0); 19 | } 20 | 21 | // 답 출력 22 | scanf("%d", &Q); 23 | for (i = 1; i <= Q; i++) { 24 | scanf("%d%d", &L[i], &R[i]); 25 | printf("%d %d\n", S1[R[i]] - S1[L[i] - 1], S2[R[i]] - S2[L[i] - 1]); 26 | } 27 | return 0; 28 | } -------------------------------------------------------------------------------- /문제풀이/chap6-16_20/prob6-20.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int N, Q, C[100009], P[100009], L[100009], R[100009], S1[100009], S2[100009]; 5 | 6 | int main() { 7 | // 누적합 구하기 8 | cin >> N; 9 | for (int i = 1; i <= N; i++) cin >> C[i] >> P[i]; 10 | for (int i = 1; i <= N; i++) S1[i] = S1[i - 1] + (C[i] == 1 ? P[i] : 0); 11 | for (int i = 1; i <= N; i++) S2[i] = S2[i - 1] + (C[i] == 2 ? P[i] : 0); 12 | 13 | // 답 출력 14 | cin >> Q; 15 | for (int i = 1; i <= Q; i++) { 16 | cin >> L[i] >> R[i]; 17 | cout << S1[R[i]] - S1[L[i] - 1] << " " << S2[R[i]] - S2[L[i] - 1] << endl; 18 | } 19 | return 0; 20 | } -------------------------------------------------------------------------------- /문제풀이/chap6-16_20/prob6-20.py: -------------------------------------------------------------------------------- 1 | # 입력 2 | N = int(input()) 3 | C = [ None ] * (N + 1) 4 | P = [ None ] * (N + 1) 5 | for i in range(1, N + 1): 6 | C[i], P[i] = map(int, input().split()) 7 | 8 | # 누적합 구하기 9 | S1 = [ None ] * (N + 1) 10 | S1[0] = 0 11 | for i in range(1, N + 1): 12 | S1[i] = S1[i - 1] + (P[i] if C[i] == 1 else 0) 13 | S2 = [ None ] * (N + 1) 14 | S2[0] = 0 15 | for i in range(1, N + 1): 16 | S2[i] = S2[i - 1] + (P[i] if C[i] == 2 else 0) 17 | 18 | # 답 출력 19 | Q = int(input()) 20 | for i in range(Q): 21 | L, R = map(int, input().split()) 22 | print(S1[R] - S1[L - 1], S2[R] - S2[L - 1]) -------------------------------------------------------------------------------- /문제풀이/chap6-21_25/prob6-21.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() { 5 | double r = 2.0; // y = e^x과 y = 2의 교점을 구할 것이므로 6 | double a = 1.0; // 초깃값을 적당하게 1.0으로 설정 7 | int i; 8 | for (i = 1; i <= 5; i++) { 9 | // 점 (a, f(a))의 x 좌표와 y 좌표를 구합니다. 10 | double japyo_x = a; 11 | double japyo_y = exp(a); 12 | 13 | // 접선의 기울기 구하기[y = (jupseon_a)x + jupseon_b] 14 | double jupseon_a = japyo_y; 15 | double jupseon_b = japyo_y - jupseon_a * japyo_x; 16 | 17 | // 다음 a의 값 next_a를 구합니다. 18 | double next_a = (r - jupseon_b) / jupseon_a; 19 | printf("Step #%d: a = %.15lf -> %.15lf\n", i, a, next_a); 20 | a = next_a; 21 | } 22 | return 0; 23 | } -------------------------------------------------------------------------------- /문제풀이/chap6-21_25/prob6-21.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | double r = 2.0; // y = e^x과 y = 2의 교점을 구할 것이므로 7 | double a = 1.0; // 초깃값을 적당하게 1.0으로 설정 8 | 9 | for (int i = 1; i <= 5; i++) { 10 | // 점 (a, f(a))의 x 좌표와 y 좌표를 구합니다. 11 | double japyo_x = a; 12 | double japyo_y = exp(a); 13 | 14 | // 접선 식 y = jupseon_a * x + jupseon_b를 구합니다. 15 | double jupseon_a = japyo_y; 16 | double jupseon_b = japyo_y - jupseon_a * japyo_x; 17 | 18 | // 다음 a의 값 next_a를 구합니다. 19 | double next_a = (r - jupseon_b) / jupseon_a; 20 | printf("Step #%d: a = %.15lf -> %.15lf\n", i, a, next_a); 21 | a = next_a; 22 | } 23 | return 0; 24 | } -------------------------------------------------------------------------------- /문제풀이/chap6-21_25/prob6-21.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Main { 4 | public static void main(String[] args) { 5 | double r = 2.0; // y = e^x과 y = 2의 교점을 구할 것이므로 6 | double a = 1.0; // 초깃값은 적당한 수를 할당합니다. 7 | for (int i = 1; i <= 5; i++) { 8 | // 점 (a, f(a))의 x 좌표와 y 좌표를 구합니다. 9 | double japyo_x = a; 10 | double japyo_y = Math.exp(a); 11 | 12 | // 접선 식 y = jupseon_a * x + jupseon_b를 구합니다. 13 | double jupseon_a = japyo_y; 14 | double jupseon_b = japyo_y - jupseon_a * japyo_x; 15 | 16 | // 다음 a의 값 next_a를 구합니다. 17 | double next_a = (r - jupseon_b) / jupseon_a; 18 | System.out.format("Step #%d: a = %.15f -> %.15f\n", i, a, next_a); 19 | a = next_a; 20 | } 21 | } 22 | } -------------------------------------------------------------------------------- /문제풀이/chap6-21_25/prob6-21.py: -------------------------------------------------------------------------------- 1 | import math 2 | 3 | r = 2.0 # y = e^x과 y = 2의 교점을 구할 것이므로 4 | a = 1.0 # 초깃값을 적당하게 1.0으로 설정 5 | repeats = 5 6 | 7 | for i in range(1, repeats + 1): 8 | # 점 (a, f(a))의 x 좌표와 y 좌표를 구합니다. 9 | japyo_x, japyo_y = a, math.exp(a) 10 | 11 | # 접선 식 y = jupseon_a * x + jupseon_b를 구합니다. 12 | jupseon_a = japyo_y 13 | jupseon_b = japyo_y - jupseon_a * japyo_x 14 | 15 | # 다음 a의 값 next_a를 구합니다. 16 | next_a = (r - jupseon_b) / jupseon_a 17 | print("Step #{}: a = {:.15f}-> {:.15f}".format(i, a, next_a)) 18 | a = next_a -------------------------------------------------------------------------------- /문제풀이/chap6-21_25/prob6-22.py: -------------------------------------------------------------------------------- 1 | # 입력 2 | N = int(input()) 3 | T = list(map(int, input().split())) 4 | 5 | # 배열 초기화 6 | sumT = sum(T) 7 | dp = [ [ False ] * (sumT + 1) for i in range(N + 1) ] 8 | dp[0][0] = True 9 | 10 | # 동적계획법 11 | for i in range(1, N + 1): 12 | for j in range(sumT + 1): 13 | if j < T[i - 1]: 14 | if dp[i - 1][j] == True: 15 | dp[i][j] = True 16 | else: 17 | dp[i][j] = False 18 | if j >= T[i - 1]: 19 | if dp[i - 1][j] == True or dp[i - 1][j - T[i - 1]] == True: 20 | dp[i][j] = True 21 | else: 22 | dp[i][j] = False 23 | 24 | # 답을 계산하고 출력 25 | answer = 10 ** 10 26 | for i in range(sumT + 1): 27 | if dp[N][i] == True: 28 | cooking_time = max(i, sumT - i) 29 | answer = min(answer, cooking_time) 30 | print(answer) -------------------------------------------------------------------------------- /문제풀이/chap6-21_25/prob6-23.py: -------------------------------------------------------------------------------- 1 | # 입력 2 | L, R = map(int, input().split()) 3 | 4 | # 배열 초기화/L=1 때의 조건 분기 5 | isprime = [ True ] * (R - L + 1) 6 | if L == 1: 7 | isprime[0] = False 8 | 9 | # 에라토스테네스의 체 10 | LIMIT = int(R ** 0.5) 11 | for i in range(2, LIMIT + 1): 12 | min_value = ((L + i - 1) // i) * i 13 | for j in range(min_value, R + 1, i): 14 | if j == i: 15 | continue 16 | isprime[j - L] = False 17 | 18 | # 갯수 세서 출력하기 19 | answer = 0 20 | for i in range(R - L + 1): 21 | if isprime[i] == True: 22 | answer += 1 23 | print(answer) -------------------------------------------------------------------------------- /문제풀이/chap6-21_25/prob6-24.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int N; long long X[100009], Y[100009], A, B; 6 | 7 | int main() { 8 | // 입력 9 | cin >> N; 10 | for (int i = 0; i < N; i++) cin >> X[i] >> Y[i]; 11 | cin >> A >> B; 12 | 13 | // 교차 횟수 세기 14 | int cnt = 0; 15 | for (int i = 0; i < N; i++) { 16 | long long xa = X[i] - A, ya = Y[i] - B; 17 | long long xb = X[(i + 1) % N] - A, yb = Y[(i + 1) % N] - B; 18 | if (ya > yb) { 19 | swap(xa, xb); 20 | swap(ya, yb); 21 | } 22 | if (ya <= 0 && 0 < yb && xa * yb - xb * ya < 0) { 23 | cnt += 1; 24 | } 25 | } 26 | 27 | // 답 출력 28 | if (cnt % 2 == 1) cout << "INSIDE" << endl; 29 | else cout << "OUTSIDE" << endl; 30 | 31 | return 0; 32 | } -------------------------------------------------------------------------------- /문제풀이/chap6-21_25/prob6-24.py: -------------------------------------------------------------------------------- 1 | # 입력 2 | N = int(input()) 3 | X = [ None ] * N 4 | Y = [ None ] * N 5 | for i in range(N): 6 | X[i], Y[i] = map(int, input().split()) 7 | A, B = map(int, input().split()) 8 | 9 | # 교차 횟수 세기 10 | cnt = 0 11 | for i in range(N): 12 | xa, ya = X[i] - A, Y[i] - B 13 | xb, yb = X[(i + 1) % N] - A, Y[(i + 1) % N] - B 14 | if ya > yb: 15 | xa, xb = xb, xa 16 | ya, yb = yb, ya 17 | if ya <= 0 and 0 < yb and xa * yb - xb * ya < 0: 18 | cnt += 1 19 | 20 | # 답 출력 21 | if cnt % 2 == 1: 22 | print("INSIDE") 23 | else: 24 | print("OUTSIDE") -------------------------------------------------------------------------------- /문제풀이/chap6-26_30/prob6-26.py: -------------------------------------------------------------------------------- 1 | # numpy 라이브러리에 행렬의 T 제곱을 구하는 함수가 있습니다. 2 | # 이를 활용하면, 다음과 같이 간단하게 코드를 구현할 수 있습니다. 3 | # 물론 반복제곱법을 사용해서도 풀 수 있습니다. 이와 관련된 내용은 코드 4.7.1 등을 참고해주세요. 4 | 5 | import numpy as np 6 | 7 | Q = int(input()) 8 | for i in range(Q): 9 | S = input().split() 10 | X, Y, Z, T = float(S[0]), float(S[1]), float(S[2]), int(S[3]) 11 | A = np.array([[ 1 - X, Y, 0 ], [ 0, 1 - Y, Z ], [ X, 0, 1 - Z ]]) 12 | answer = np.linalg.matrix_power(A, T) 13 | print("{:.15f} {:.15f} {:.15f}".format(sum(answer[0]), sum(answer[1]), sum(answer[2]))) -------------------------------------------------------------------------------- /문제풀이/chap6-26_30/prob6-28.py: -------------------------------------------------------------------------------- 1 | # 뤼카 정리로 ncr mod 3 계산 2 | def ncr(n, r): 3 | if n < 3 and r < 3: 4 | A = [ 5 | [ 1, 0, 0 ], 6 | [ 1, 1, 0 ], 7 | [ 1, 2, 1 ] 8 | ] 9 | return A[n][r] 10 | return ncr(n // 3, r // 3) * ncr(n % 3, r % 3) % 3 11 | 12 | # 입력 13 | N = int(input()) 14 | C = input() 15 | 16 | # 답 구하기 17 | answer = 0 18 | for i in range(N): 19 | code = "BWR".find(C[i]) 20 | answer += code * ncr(N - 1, i) 21 | answer %= 3 22 | 23 | # 답에 (-1)^(N-1)을 곱함 24 | if N % 2 == 0: 25 | answer = (3 - answer) % 3 26 | 27 | # 답 출력("BWR"의 answer번째 문자) 28 | print("BWR"[answer]) -------------------------------------------------------------------------------- /본문/c/Code_2_01_1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int N; 5 | scanf("%d", &N); 6 | printf("%d\n", 5 + N); 7 | return 0; 8 | } -------------------------------------------------------------------------------- /본문/c/Code_2_01_2.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int A[4]; 5 | scanf("%d%d%d", &A[1], &A[2], &A[3]); 6 | printf("%d\n", A[1] + A[2] + A[3]); 7 | return 0; 8 | } -------------------------------------------------------------------------------- /본문/c/Code_2_01_3.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int N, A[59]; 5 | int Answer = 0; 6 | 7 | scanf("%d", &N); 8 | for (int i = 1; i <= N; i++) { 9 | scanf("%d", &A[i]); 10 | Answer += A[i]; 11 | } 12 | printf("%d\n", Answer); 13 | return 0; 14 | } -------------------------------------------------------------------------------- /본문/c/Code_2_01_4.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() { 5 | int N; 6 | scanf("%d", &N); 7 | char answer[100] = ""; // char 자료형 배열은 문자열을 나타냄 8 | while (N >= 1) { 9 | // N % 2는 N를 2로 나눈 나머지(예: N = 13의 경우 1) 10 | // N / 2는 N를 2로 나눈 몫(예:N = 13의 경우 6) 11 | char next_answer[101]; 12 | if (N % 2 == 0) { 13 | // next_answer에 "0"과 answer를 연결한 값을 할당 14 | sprintf(next_answer, "0%s", answer); 15 | } 16 | if (N % 2 == 1) { 17 | // next_answer에 "1"과 answer를 연결한 값을 할당 18 | sprintf(next_answer, "1%s", answer); 19 | } 20 | strcpy(answer, next_answer); 21 | N = N / 2; 22 | } 23 | printf("%s\n", answer); 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /본문/c/Code_2_02_1.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main() { 6 | // 사칙 연산 7 | printf("%d\n", 869 + 120); // 989 8 | printf("%d\n", 869 - 120); // 749 9 | printf("%d\n", 869 * 120); // 104280 10 | printf("%d\n", 869 / 120); // 7(정수 부분만 출력하므로 주의) 11 | 12 | // 나머지(mod) 13 | printf("%d\n", 8 % 5); // 3 14 | printf("%d\n", 869 % 120); // 29 15 | 16 | // 절댓값(abs)(C 언어에서 abs를 사용하려면, 를 읽어 들여야 함) 17 | printf("%d\n", abs(-45)); // 45 18 | printf("%d\n", abs(15)); // 15 19 | 20 | // 제곱(pow) 21 | printf("%d\n", (int)pow(10.0, 2.0)); // 100 22 | printf("%d\n", (int)pow(3.0, 4.0)); // 81 23 | 24 | // 루트(sqrt) 25 | printf("%.5lf\n", sqrt(4.0)); // 2.00000 26 | printf("%.5lf\n", sqrt(2.0)); // 1.41421 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /본문/c/Code_2_02_2.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int a, b; 5 | scanf("%d%d", &a, &b); // a와 b를 입력 6 | printf("%d\n", a & b); // a AND b의 값을 출력 7 | printf("%d\n", a | b); // a OR b의 값을 출력 8 | printf("%d\n", a ^ b); // a XOR b의 값을 출력 9 | return 0; 10 | } 11 | -------------------------------------------------------------------------------- /본문/c/Code_2_03_1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int cnt = 1000; 4 | 5 | int func1() { 6 | return 2021; 7 | } 8 | 9 | int func2(int pos) { 10 | cnt += 1; 11 | return cnt + pos; 12 | } 13 | 14 | int main() { 15 | printf("%d\n", func1()); // "2021"이라고 출력 16 | printf("%d\n", func2(500)); // "1501"이라고 출력 17 | printf("%d\n", func2(500)); // "1502"라고 출력 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /본문/c/Code_2_04_1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int N; 5 | scanf("%d", &N); 6 | printf("%d\n", 2 * N + 3); 7 | return 0; 8 | } 9 | -------------------------------------------------------------------------------- /본문/c/Code_2_04_2.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | // 입력 5 | int N, X, Y; 6 | scanf("%d%d%d", &N, &X, &Y); 7 | 8 | // 답 구하기 9 | int cnt = 0; 10 | int i; 11 | for (i = 1; i <= N; i++) { 12 | // mod 계산은 2.2절 참고 13 | if (i % X == 0 || i % Y == 0) { 14 | cnt += 1; 15 | } 16 | } 17 | 18 | // 출력 19 | printf("%d\n", cnt); 20 | 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /본문/c/Code_2_04_3.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | // 입력 5 | int N, S; 6 | scanf("%d%d", &N, &S); 7 | 8 | // 답 구하기 9 | int answer = 0; 10 | int i, j; 11 | for (i = 1; i <= N; i++) { 12 | for (j = 1; j <= N; j++) { 13 | if (i + j <= S) { 14 | answer += 1; 15 | } 16 | } 17 | } 18 | 19 | // 출력 20 | printf("%d\n", answer); 21 | } 22 | -------------------------------------------------------------------------------- /본문/c/Code_2_06_1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | // 입력 5 | long long N, S, A[61]; 6 | scanf("%lld%lld", &N, &S); 7 | long long i, j; 8 | for (i = 1; i <= N; i++) { 9 | scanf("%lld", &A[i]); 10 | } 11 | 12 | // 모든 패턴 탐색: (1LL << N)은 2의 N제곱 13 | for (i = 0; i < (1LL << N); i++) { 14 | long long sum = 0; 15 | for (j = 1; j <= N; j++) { 16 | // (i & (1LL << (j-1))) != 0LL이라면 17 | // i의 2진법 표기에서 아래에서 j번째 카드가 1이라는 의미입니다. 18 | // (1LL << j)는 C에서 "2의 j제곱을 의미합니다. 19 | if ((i & (1LL << (j - 1))) != 0LL) { 20 | sum += A[j]; 21 | } 22 | } 23 | if (sum == S) { 24 | printf("Yes\n"); 25 | return 0; 26 | } 27 | } 28 | printf("No\n"); 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /본문/c/Code_3_01_1.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | // 2 이상의 정수 N을 매개변수로 받고, N이 소수라면 True를, 5 | // 아니라면 False를 리턴하는 함수 6 | bool isprime(long long N) { 7 | long long i; 8 | for (i = 2; i <= N - 1; i++) { 9 | if (N % i == 0) { 10 | // N이 i으로 나누어지는 경우, 이 시점에서 소수가 아니라는 것을 알 수 있음 11 | return false; 12 | } 13 | } 14 | return true; 15 | } 16 | 17 | int main() { 18 | long long N; 19 | scanf("%lld", &N); 20 | if (isprime(N) == true) { 21 | printf("prime\n"); 22 | } 23 | else { 24 | printf("not prime\n"); 25 | } 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /본문/c/Code_3_01_2.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | // 2 이상의 정수 N을 매개변수로 받고, N이 소수라면 True를, 5 | // 아니라면 False를 리턴하는 함수 6 | bool isprime(long long N) { 7 | long long i; 8 | for (i = 2; i * i <= N; i++) { 9 | if (N % i == 0) { 10 | return false; 11 | } 12 | } 13 | return true; 14 | } 15 | 16 | int main() { 17 | long long N; 18 | scanf("%lld", &N); 19 | if (isprime(N) == true) { 20 | printf("prime\n"); 21 | } 22 | else { 23 | printf("not prime\n"); 24 | } 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /본문/c/Code_3_01_3.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | long long N; 5 | scanf("%lld", &N); 6 | long long i; 7 | for (i = 1; i * i <= N; i++) { 8 | if (N % i != 0) continue; 9 | printf("%lld\n", i); // i를 약수로 추가 10 | if (i != N / i) { 11 | printf("%lld\n", N / i); // i ≠ N/i라면, N/i도 약수에 추가 12 | } 13 | } 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /본문/c/Code_3_02_1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | // 양의 정수 A와 B의 최대 공약수를 리턴하는 함수 4 | // GCD는 Greatest Common Divisor(최대 공약수)의 약자 5 | long long GCD(long long A, long long B) { 6 | long long answer = 0; 7 | long long i; 8 | for (i = 1; i <= A && i <= B; i++) { 9 | if (A % i == 0 && B % i == 0) { 10 | answer = i; 11 | } 12 | } 13 | return answer; 14 | } 15 | 16 | int main() { 17 | long long A, B; 18 | scanf("%lld%lld", &A, &B); 19 | printf("%lld\n", GCD(A, B)); 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /본문/c/Code_3_02_2.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | // GCD는 Greatest Common Divisor(최대 공약수)의 약자 4 | long long GCD(long long A, long long B) { 5 | while (A >= 1 && B >= 1) { 6 | if (A < B) B = B % A; // A < B라면, 큰 수를 b로 변경합니다. 7 | else A = A % B; // A >= B라면 큰 수를 a로 변경합니다. 8 | } 9 | if (A >= 1) return A; 10 | return B; 11 | } 12 | 13 | int main() { 14 | long long A, B; 15 | scanf("%lld%lld", &A, &B); 16 | printf("%lld\n", GCD(A, B)); 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /본문/c/Code_3_03_1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | // 입력 5 | int N, A[109]; 6 | scanf("%d", &N); 7 | for (int i = 1; i <= N; i++) { 8 | scanf("%d", &A[i]); 9 | } 10 | 11 | // 5개의 카드 번호(i, j, k, l, m)를 전체 탐색 12 | int answer = 0; 13 | int i, j, k, l, m; 14 | for (i = 1; i <= N; i++) { 15 | for (j = i + 1; j <= N; j++) { 16 | for (k = j + 1; k <= N; k++) { 17 | for (l = k + 1; l <= N; l++) { 18 | for (m = l + 1; m <= N; m++) { 19 | if (A[i] + A[j] + A[k] + A[l] + A[m] == 1000) { 20 | answer += 1; 21 | } 22 | } 23 | } 24 | } 25 | } 26 | } 27 | 28 | // 출력 29 | printf("%d\n", answer); 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /본문/c/Code_3_04_1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int N, B[100009], R[100009]; 4 | 5 | int main() { 6 | // 입력 7 | scanf("%d", &N); 8 | int i; 9 | for (i = 1; i <= N; i++) { 10 | scanf("%d", &B[i]); 11 | } 12 | for (i = 1; i <= N; i++) { 13 | scanf("%d", &R[i]); 14 | } 15 | 16 | // 답 계산 → 답 출력 17 | double blue = 0.0, red = 0.0; 18 | for (i = 1; i <= N; i++) { 19 | blue += 1.0 * B[i] / N; 20 | red += 1.0 * R[i] / N; 21 | } 22 | printf("%.12lf\n", blue + red); 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /본문/c/Code_3_04_2.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | // 입력 5 | int N, P[59], Q[59]; 6 | scanf("%d", &N); 7 | int i; 8 | for (i = 1; i <= N; i++) { 9 | scanf("%d%d", &P[i], &Q[i]); 10 | } 11 | 12 | // 답 계산 → 답 출력 13 | double answer = 0.0; 14 | for (i = 1; i <= N; i++) { 15 | answer += 1.0 * Q[i] / P[i]; 16 | } 17 | printf("%.12lf\n", answer); 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /본문/c/Code_3_05_1.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() { 5 | int N = 10000; // N은 시행 횟수(적절하게 변경해서 사용해 주세요) 6 | int M = 0; 7 | int i; 8 | for (i = 1; i <= N; i++) { 9 | double px = rand() / (double)RAND_MAX; // 0 이상 1 이하의 랜덤한 숫자 생성 10 | double py = rand() / (double)RAND_MAX; // 0 이상 1 이하의 랜덤한 숫자 생성 11 | // 원점에서의 거리 sqrt(px * px + py * py)가 12 | // 1 이하라면 원 안에 있는 것이므로, 조건을 "px * px + py * py <= 1"로 설정 13 | if (px * px + py * py <= 1.0) M += 1; 14 | } 15 | printf("%.12lf\n", 4.0 * M / N); 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /본문/c/Code_3_06_2.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int N, A[200009]; 4 | 5 | int main() { 6 | // 입력 7 | scanf("%d", &N); 8 | int i, j; 9 | for (i = 1; i <= N; i++) { 10 | scanf("%d", &A[i]); 11 | } 12 | 13 | // 선택 정렬 14 | for (i = 1; i <= N - 1; i++) { 15 | int min_position = i; 16 | int min_value = A[i]; 17 | for (j = i + 1; j <= N; j++) { 18 | if (A[j] < min_value) { 19 | min_position = j; // min_position는 최솟값의 인덱스(1~N) 20 | min_value = A[j]; // min_value는 현재 시점에서 최소값 21 | } 22 | } 23 | // 다음 3줄로 A[i]와 A[min_position]를 스왑 24 | int temp = A[i]; 25 | A[i] = A[min_position]; 26 | A[min_position] = temp; 27 | } 28 | 29 | // 출력 30 | for (int i = 1; i <= N; i++) { 31 | printf("%d\n", A[i]); 32 | } 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /본문/c/Code_3_06_3.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int func(int N) { 4 | if (N == 1) { 5 | return 1; // 이처럼 재귀 함수를 끝내는 경우를 "베이스 케이스"라고 부릅니다. 6 | } 7 | return func(N - 1) * N; 8 | } 9 | 10 | int main() { 11 | int N; 12 | scanf("%d", &N); 13 | printf("%d\n", func(N)); 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /본문/c/Code_3_06_4.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | long long GCD(long long A, long long B) { 4 | if (B == 0) { 5 | return A; // 베이스 케이스 6 | } 7 | return GCD(B, A % B); 8 | } 9 | 10 | int main() { 11 | long long A, B; 12 | scanf("%lld%lld", &A, &B); 13 | printf("%lld\n", GCD(A, B)); 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /본문/c/Code_3_06_5.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int N, A[109]; 4 | 5 | int solve(int l, int r) { 6 | if (r - l == 1) return A[l]; 7 | int m = (l + r) / 2; // 구간 [l, r)의 중앙을 기준으로 분할 정복 8 | int s1 = solve(l, m); // s1는 A[l]+...+A[m-1]의 합계 9 | int s2 = solve(m, r); // s2는 A[m]+...+A[r-1]의 합계 10 | return s1 + s2; 11 | } 12 | 13 | int main() { 14 | // 입력 15 | scanf("%d", &N); 16 | int i; 17 | for (i = 1; i <= N; i++) { 18 | scanf("%d", &A[i]); 19 | } 20 | 21 | // 재귀함수 호출 → 답 출력 22 | int answer = solve(1, N + 1); 23 | printf("%d\n", answer); 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /본문/c/Code_3_06_6.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int func(int N) { 4 | return func(N - 1) * N; 5 | } 6 | 7 | int main() { 8 | int N; 9 | scanf("%d", &N); 10 | printf("%d\n", func(N)); 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /본문/c/Code_3_07_1.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int N, H[100009], dp[100009]; 5 | 6 | int main() { 7 | // 입력 8 | scanf("%d", &N); 9 | int i; 10 | for (i = 1; i <= N; i++) { 11 | scanf("%d", &H[i]); 12 | } 13 | 14 | // 동적계획법 → 답 출력 15 | for (i = 1; i <= N; i++) { 16 | if (i == 1) dp[i] = 0; 17 | if (i == 2) dp[i] = abs(H[i - 1] - H[i]); 18 | if (i >= 3) { 19 | int v1 = dp[i - 1] + abs(H[i - 1] - H[i]); // 1개 이전 발판에서 점프할 때 20 | int v2 = dp[i - 2] + abs(H[i - 2] - H[i]); // 2개 이전 발판에서 점프할 때 21 | dp[i] = (v1 < v2 ? v1 : v2); // v1과 v2 중에서 작은 값 22 | } 23 | } 24 | printf("%d\n", dp[N]); 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /본문/c/Code_3_07_2.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int N, dp[54]; 4 | 5 | int main() { 6 | // 입력 7 | scanf("%d", &N); 8 | 9 | // 동적계획법 → 답 출력 10 | int i; 11 | for (i = 0; i <= N; i++) { 12 | if (i <= 1) dp[i] = 1; 13 | else dp[i] = dp[i - 1] + dp[i - 2]; 14 | } 15 | printf("%d\n", dp[N]); 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /본문/c/Code_4_02_1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int N, A[100009], B[100009]; 4 | int Q, L[100009], R[100009]; 5 | 6 | int main() { 7 | // 입력 8 | scanf("%d%d", &N, &Q); 9 | int i, j; 10 | for (i = 1; i <= N; i++) { 11 | scanf("%d", &A[i]); 12 | } 13 | for (j = 1; j <= Q; j++) { 14 | scanf("%d%d", &L[j], &R[j]); 15 | } 16 | 17 | // 누적합 구하기 18 | B[0] = 0; 19 | for (i = 1; i <= N; i++) { 20 | B[i] = B[i - 1] + A[i]; 21 | } 22 | 23 | // 답 출력 24 | for (j = 1; j <= Q; j++) { 25 | printf("%d\n", B[R[j]] - B[L[j] - 1]); 26 | } 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /본문/c/Code_4_02_2.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int N, B[100009]; 4 | int Q, L[100009], R[100009], X[100009]; 5 | 6 | int main() { 7 | // 입력 8 | scanf("%d%d", &N, &Q); 9 | int i; 10 | for (i = 1; i <= Q; i++) { 11 | scanf("%d%d%d", &L[i], &R[i], &X[i]); 12 | } 13 | 14 | // 계차 계산하기 15 | for (i = 1; i <= Q; i++) { 16 | B[L[i]] += X[i]; 17 | B[R[i] + 1] -= X[i]; 18 | } 19 | 20 | // 답 출력 21 | for (i = 2; i <= N; i++) { 22 | if (B[i] > 0) printf("<"); 23 | if (B[i] == 0) printf("="); 24 | if (B[i] < 0) printf(">"); 25 | } 26 | printf("\n"); 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /본문/c/Code_4_03_1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | double r = 2.0; // √2를 구할 것이므로 5 | double a = 2.0; // 초깃값은 적당한 수를 할당합니다. 6 | int i; 7 | for (i = 1; i <= 5; i++) { 8 | // 점 (a, f(a))의 x 좌표와 y 좌표를 구합니다. 9 | double japyo_x = a; 10 | double japyo_y = a * a; 11 | 12 | // 접선의 기울기 구하기[y = (jupseon_a)x + jupseon_b] 13 | double jupseon_a = 2.0 * japyo_x; 14 | double jupseon_b = japyo_y - jupseon_a * japyo_x; 15 | 16 | // 다음 a의 값 next_a를 구합니다. 17 | double next_a = (r - jupseon_b) / jupseon_a; 18 | printf("Step #%d: a = %.12lf -> %.12lf\n", i, a, next_a); 19 | a = next_a; 20 | } 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /본문/c/Code_4_04_1.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int N; bool prime[100000009]; 5 | 6 | int main() { 7 | // 입력 → 배열 초기화 8 | scanf("%d", &N); 9 | int i, x; 10 | for (i = 2; i <= N; i++) { 11 | prime[i] = true; 12 | } 13 | 14 | // 에라토스테네스의 체 15 | for (i = 2; i * i <= N; i++) { 16 | if (prime[i] == true) { 17 | for (x = 2 * i; x <= N; x += i) { 18 | prime[x] = false; 19 | } 20 | } 21 | } 22 | 23 | // N 이하의 소수를 오름차순으로 출력 24 | for (i = 2; i <= N; i++) { 25 | if (prime[i] == true) { 26 | printf("%d\n", i); 27 | } 28 | } 29 | 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /본문/c/Code_4_06_1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int N, a[10000009]; 4 | 5 | int main() { 6 | scanf("%d", &N); 7 | a[1] = 1; a[2] = 1; 8 | int i; 9 | for (i = 3; i <= N; i++) { 10 | a[i] = a[i - 1] + a[i - 2]; 11 | } 12 | printf("%d\n", a[N] % 1000000007); 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /본문/c/Code_4_06_2.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int N, a[10000009]; 4 | 5 | int main() { 6 | scanf("%d", &N); 7 | a[1] = 1; a[2] = 1; 8 | int i; 9 | for (i = 3; i <= N; i++) { 10 | a[i] = (a[i - 1] + a[i - 2]) % 1000000007; 11 | } 12 | printf("%d\n", a[N] % 1000000007); 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /본문/c/Code_4_06_3.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | long long a, b; 5 | scanf("%lld%lld", &a, &b); 6 | const long long MOD = 1000000007; 7 | long long answer = 1; // a의 0 제곱은 1이므로, Answer=1로 초기화해둡니다. 8 | long long i; 9 | for (i = 1; i <= b; i++) { 10 | answer = (answer * a) % MOD; 11 | } 12 | printf("%lld\n", answer); 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /본문/c/Code_4_06_4.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | long long modpow(long long a, long long b, long long m) { 4 | // 반복제곱법(p에는 a^1, a^2, a^4, a^8, ...가 할당됨) 5 | long long p = a, answer = 1; 6 | int i; 7 | for (i = 0; i < 30; i++) { 8 | if ((b & (1 << i)) != 0) { 9 | answer = (answer * p) % m; 10 | } 11 | p = (p * p) % m; 12 | } 13 | return answer; 14 | } 15 | 16 | int main() { 17 | const long long MOD = 1000000007; 18 | long long a, b; 19 | scanf("%lld%lld", &a, &b); 20 | printf("%lld\n", modpow(a, b, MOD)); 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /본문/c/Code_5_02_1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | long long N; 5 | scanf("%lld", &N); 6 | if (N % 4 == 1) printf("2\n"); 7 | if (N % 4 == 2) printf("4\n"); 8 | if (N % 4 == 3) printf("8\n"); 9 | if (N % 4 == 0) printf("6\n"); 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /본문/c/Code_5_02_2.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | long long N; 5 | scanf("%lld", &N); 6 | if (N % 4 == 0) { 7 | printf("Second\n"); // 후수필승 8 | } 9 | else { 10 | printf("First\n"); // 선수필승 11 | } 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /본문/c/Code_5_03_1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int N; 5 | scanf("%d", &N); 6 | if (N % 2 == 0) { 7 | printf("Yes\n"); 8 | } 9 | else { 10 | printf("No\n"); 11 | } 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /본문/c/Code_5_03_2.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | // 입력 5 | int N, K, A[59]; 6 | scanf("%d%d", &N, &K); 7 | int i; 8 | for (i = 1; i <= N; i++) { 9 | scanf("%d", &A[i]); 10 | } 11 | 12 | // 수열 요소의 총합 sum 구하기 13 | int sum = 0; 14 | for (i = 1; i <= N; i++) { 15 | sum += A[i]; 16 | } 17 | 18 | // 답 출력 19 | if (sum % 2 != K % 2) { 20 | printf("No\n"); 21 | } 22 | else if (sum > K) { 23 | printf("No\n"); 24 | } 25 | else { 26 | printf("Yes\n"); 27 | } 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /본문/c/Code_5_04_1.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int minimum(int a, int b) { 5 | return (a < b ? a : b); 6 | } 7 | 8 | int maximum(int a, int b) { 9 | return (a > b ? a : b); 10 | } 11 | 12 | int main() { 13 | // 입력 14 | int N, K; 15 | scanf("%d%d", &N, &K); 16 | 17 | // 사건B의 개수를 yeosagun(여사건) 변수로 셉니다. 18 | long long yeosagun = 0; 19 | int a, b, c; 20 | for (a = 1; a <= N; a++) { 21 | int l = maximum(a - (K - 1), 1); // b, c의 탐색 범위 하한 l 22 | int r = minimum(a + (K - 1), N); // b, c의 탐색 범위 상한 r 23 | for (b = l; b <= r; b++) { 24 | for (c = l; c <= r; c++) { 25 | if (abs(b - c) <= K - 1) yeosagun += 1; 26 | } 27 | } 28 | } 29 | 30 | // 답 출력 31 | printf("%lld\n", (long long)N * N * N - yeosagun); 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /본문/c/Code_5_05_1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | // 입력 5 | long long a, b, c, d; 6 | scanf("%lld%lld%lld%lld", &a, &b, &c, &d); 7 | 8 | // 답 구하기(answer는 일단 나올 수 없는 값을 설정) 9 | long long answer = -(1LL << 62); 10 | if (answer < a * c) answer = a * c; 11 | if (answer < a * d) answer = a * d; 12 | if (answer < b * c) answer = b * c; 13 | if (answer < b * d) answer = b * d; 14 | 15 | // 출력 16 | printf("%lld\n", answer); 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /본문/c/Code_5_06_1.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | // 작은 문제 t를 푸는 함수 5 | bool small_problem(int A, int B, int t) { 6 | int cl = (A + t - 1) / t; // A÷t의 소수점 아래를 자릅니다. 7 | int cr = B / t; // B÷t의 소수점 아래를 자릅니다. 8 | if (cr - cl >= 1) return true; 9 | return false; 10 | } 11 | 12 | int main() { 13 | int A, B; 14 | scanf("%d%d", &A, &B); 15 | int answer = 0; 16 | int i; 17 | for (i = 1; i <= B; i++) { 18 | if (small_problem(A, B, i) == true) { 19 | answer = i; 20 | } 21 | } 22 | printf("%d\n", answer); 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /본문/c/Code_5_07_1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int N; long long A[200009]; 4 | 5 | int main() { 6 | // 입력 7 | scanf("%d", &N); 8 | int i; 9 | for (i = 1; i <= N; i++) { 10 | scanf("%lld", &A[i]); 11 | } 12 | 13 | // 답 구하기 → 답 출력 14 | long long answer = 0; 15 | for (i = 1; i <= N; i++) { 16 | answer += A[i] * (-N + 2LL * i - 1LL); 17 | } 18 | printf("%lld\n", answer); 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /본문/c/Code_5_09_1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | // 입력 5 | long long N; 6 | scanf("%lld", &N); 7 | 8 | // 지불 방법 시뮬레이션 9 | long long answer = 0; 10 | while (N >= 10000) { 11 | N -= 10000; 12 | answer += 1; 13 | } 14 | while (N >= 5000) { 15 | N -= 5000; 16 | answer += 1; 17 | } 18 | while (N >= 1) { 19 | N -= 1000; 20 | answer += 1; 21 | } 22 | 23 | // 답 출력 24 | printf("%lld\n", answer); 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /본문/c/Code_5_10_1.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() { 5 | long long a, b, c; 6 | scanf("%lld%lld%lld", &a, &b, &c); 7 | if (sqrt(a) + sqrt(b) < sqrt(c)) { 8 | printf("Yes\n"); 9 | } 10 | else { 11 | printf("No\n"); 12 | } 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /본문/c/Code_5_10_2.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | long long a, b, c; 5 | scanf("%lld%lld%lld", &a, &b, &c); 6 | if (c - a - b < 0LL) { 7 | printf("No\n"); 8 | } 9 | else if (4LL * a * b < (c - a - b) * (c - a - b)) { 10 | printf("Yes\n"); 11 | } 12 | else { 13 | printf("No\n"); 14 | } 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /본문/c/Code_5_10_3.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | // 입력 5 | int N; long long X, Y; 6 | scanf("%d%lld%lld", &N, &X, &Y); 7 | 8 | // 4개의 정수 (a, b, c, d)를 전체 탐색 → 답 출력 9 | int a, b, c, d; 10 | for (a = 1; a <= N; a++) { 11 | for (b = a; b <= N; b++) { 12 | for (c = b; c <= N; c++) { 13 | for (d = c; d <= N; d++) { 14 | if (a + b + c + d == X && 1LL * a * b * c * d == Y) { 15 | printf("Yes\n"); 16 | return 0; // 프로그램 실행을 종료합니다. 17 | } 18 | } 19 | } 20 | } 21 | } 22 | printf("No\n"); // 하나도 찾지 못했다면 No를 출력 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /본문/c/Code_5_10_4.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | // 입력 5 | int N; char S[500009]; 6 | scanf("%d%s", &N, S); 7 | 8 | // (의 수에서 )의 수를 뺀 값을 depth에 할당합니다. 9 | // 중간에 depth가 음수가 된다면, 곧바로 답이 No입니다. 10 | int depth = 0; 11 | for (int i = 0; i < N; i++) { 12 | if (S[i] == '(') depth += 1; 13 | if (S[i] == ')') depth -= 1; 14 | if (depth < 0) { 15 | printf("No\n"); 16 | return 0; 17 | } 18 | } 19 | 20 | // 마지막으로 depth = 0[(와 )의 수가 같음]을 추가적으로 판정합니다. 21 | if (depth == 0) { 22 | printf("Yes\n"); 23 | } 24 | else { 25 | printf("No\n"); 26 | } 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /본문/cpp/Code_2_01_1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int N; 6 | cin >> N; // 입력 부분 7 | cout << 5 + N << endl; // 출력 부분 8 | return 0; 9 | } 10 | -------------------------------------------------------------------------------- /본문/cpp/Code_2_01_2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int A[4]; 6 | cin >> A[1] >> A[2] >> A[3]; // 입력 부분 7 | cout << A[1] + A[2] + A[3] << endl; // 출력 부분 8 | return 0; 9 | } 10 | -------------------------------------------------------------------------------- /본문/cpp/Code_2_01_3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int N, A[59]; 6 | int Answer = 0; 7 | cin >> N; 8 | for (int i = 1; i <= N; i++) { 9 | cin >> A[i]; 10 | Answer += A[i]; 11 | } 12 | cout << Answer << endl; 13 | return 0; 14 | } -------------------------------------------------------------------------------- /본문/cpp/Code_2_01_4.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int N; 6 | string Answer = ""; // string은 문자열 자료형 7 | 8 | int main() { 9 | cin >> N; // 입력 부분 10 | while (N >= 1) { 11 | // N % 2는 N를 2로 나눈 나머지(예: N = 13의 경우 1) 12 | // N / 2는 N를 2로 나눈 몫(예:N = 13의 경우 6) 13 | if (N % 2 == 0) Answer = "0" + Answer; 14 | if (N % 2 == 1) Answer = "1" + Answer; 15 | N = N / 2; 16 | } 17 | cout << Answer << endl; // 출력 부분 18 | return 0; 19 | } -------------------------------------------------------------------------------- /본문/cpp/Code_2_02_1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | // 사칙연산 7 | printf("%d\n", 869 + 120); // 989 8 | printf("%d\n", 869 - 120); // 749 9 | printf("%d\n", 869 * 120); // 104280 10 | printf("%d\n", 869 / 120); // 7(정수 부분만 출력하므로 주의) 11 | 12 | // 나머지(mod) 13 | printf("%d\n", 8 % 5); // 3 14 | printf("%d\n", 869 % 120); // 29 15 | 16 | // 절댓값(abs) 17 | printf("%d\n", abs(-45)); // 45 18 | printf("%d\n", abs(15)); // 15 19 | 20 | // 제곱(pow) 21 | printf("%d\n", (int)pow(10.0, 2.0)); // 100 22 | printf("%d\n", (int)pow(3.0, 4.0)); // 81 23 | 24 | // 루트(sqrt) 25 | printf("%.5lf\n", sqrt(4.0)); // 2.00000 26 | printf("%.5lf\n", sqrt(2.0)); // 1.41421 27 | return 0; 28 | } -------------------------------------------------------------------------------- /본문/cpp/Code_2_02_2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int a, b; 6 | cin >> a >> b; // a와 b를 입력 7 | cout << (a & b) << endl; // a AND b의 값을 출력 8 | cout << (a | b) << endl; // a OR b의 값을 출력 9 | cout << (a ^ b) << endl; // a XOR b의 값을 출력 10 | return 0; 11 | } -------------------------------------------------------------------------------- /본문/cpp/Code_2_03_1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int cnt = 1000; 5 | 6 | int func1() { 7 | return 2021; 8 | } 9 | 10 | int func2(int pos) { 11 | cnt += 1; 12 | return cnt + pos; 13 | } 14 | 15 | int main() { 16 | cout << func1() << endl; // 2021 17 | cout << func2(500) << endl; // 1501 18 | cout << func2(500) << endl; // 1502 19 | return 0; 20 | } -------------------------------------------------------------------------------- /본문/cpp/Code_2_04_1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int N; 6 | cin >> N; 7 | cout << 2 * N + 3 << endl; 8 | return 0; 9 | } -------------------------------------------------------------------------------- /본문/cpp/Code_2_04_2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | // 입력 6 | int N, X, Y; 7 | cin >> N >> X >> Y; 8 | 9 | // 답 구하기 10 | int cnt = 0; 11 | for (int i = 1; i <= N; i++) { 12 | if (i % X == 0 || i % Y == 0) cnt++; // mod 계산은 2.2절 참고 13 | } 14 | 15 | // 출력 16 | cout << cnt << endl; 17 | return 0; 18 | } -------------------------------------------------------------------------------- /본문/cpp/Code_2_04_3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int N, S; 5 | long long Answer = 0; 6 | 7 | int main() { 8 | // 입력 9 | cin >> N >> S; 10 | 11 | // 답 구하기 12 | for (int i = 1; i <= N; i++) { 13 | for (int j = 1; j <= N; j++) { 14 | if (i + j <= S) Answer += 1; 15 | } 16 | } 17 | 18 | // 출력 19 | cout << Answer << endl; 20 | return 0; 21 | } -------------------------------------------------------------------------------- /본문/cpp/Code_2_06_1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | long long N, S, A[61]; 5 | 6 | int main() { 7 | cin >> N >> S; 8 | for (int i = 1; i <= N; i++) cin >> A[i]; 9 | 10 | // 모든 패턴 탐색: (1LL< 2 | using namespace std; 3 | 4 | int main() { 5 | long long N; 6 | cin >> N; 7 | for (long long i = 1; i * i <= N; i++) { 8 | if (N % i != 0) continue; 9 | cout << i << endl; // i를 약수로 추가 10 | if (i != N / i) { 11 | cout << N / i << endl; // i ≠ N/i라면, N/i도 약수에 추가 12 | } 13 | } 14 | return 0; 15 | } -------------------------------------------------------------------------------- /본문/cpp/Code_3_02_1.cpp: -------------------------------------------------------------------------------- 1 | // 양의 정수 A와 B의 최대 공약수를 리턴하는 함수 2 | // GCD는 Greatest Common Divisor(최대 공약수)의 약자 3 | long long GCD(long long A, long long B) { 4 | long long Answer = 0; 5 | for (long long i = 1; i <= min(A, B); i++) { 6 | if (A % i == 0 && B % i == 0) Answer = i; 7 | } 8 | return Answer; 9 | } -------------------------------------------------------------------------------- /본문/cpp/Code_3_02_2.cpp: -------------------------------------------------------------------------------- 1 | // GCD는 Greatest Common Divisor(최대 공약수)의 약자 2 | long long GCD(long long A, long long B) { 3 | while (A >= 1 && B >= 1) { 4 | if (A < B) B = B % A; // A < B라면, 큰 수를 b로 변경합니다. 5 | else A = A % B; // A >= B라면 큰 수를 a로 변경합니다. 6 | } 7 | if (A >= 1) return A; 8 | return B; 9 | } -------------------------------------------------------------------------------- /본문/cpp/Code_3_03_1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int N, A[109]; 5 | int Answer = 0; 6 | 7 | int main() { 8 | // 입력 9 | cin >> N; 10 | for (int i = 1; i <= N; i++) cin >> A[i]; 11 | 12 | // 5개의 카드 번호(i, j, k, l, m)를 전체 탐색 13 | for (int i = 1; i <= N; i++) { 14 | for (int j = i + 1; j <= N; j++) { 15 | for (int k = j + 1; k <= N; k++) { 16 | for (int l = k + 1; l <= N; l++) { 17 | for (int m = l + 1; m <= N; m++) { 18 | if (A[i] + A[j] + A[k] + A[l] + A[m] == 1000) Answer += 1; 19 | } 20 | } 21 | } 22 | } 23 | } 24 | 25 | // 답 출력 26 | cout << Answer << endl; 27 | return 0; 28 | } -------------------------------------------------------------------------------- /본문/cpp/Code_3_04_1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int N, B[100009], R[100009]; 5 | 6 | int main() { 7 | // 입력 8 | cin >> N; 9 | for (int i = 1; i <= N; i++) cin >> B[i]; 10 | for (int i = 1; i <= N; i++) cin >> R[i]; 11 | 12 | // 답 계산 → 답 출력 13 | double Blue = 0.0, Red = 0.0; 14 | for (int i = 1; i <= N; i++) { 15 | Blue += 1.0 * B[i] / N; 16 | Red += 1.0 * R[i] / N; 17 | } 18 | printf("%.12lf\n", Blue + Red); 19 | return 0; 20 | } -------------------------------------------------------------------------------- /본문/cpp/Code_3_04_2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int N, P[59], Q[59]; 5 | double Answer = 0.0; 6 | 7 | int main() { 8 | // 입력 9 | cin >> N; 10 | for (int i = 1; i <= N; i++) cin >> P[i] >> Q[i]; 11 | 12 | // 답 계산 → 답 출력 13 | for (int i = 1; i <= N; i++) { 14 | Answer += 1.0 * Q[i] / P[i]; 15 | } 16 | printf("%.12lf\n", Answer); 17 | return 0; 18 | } -------------------------------------------------------------------------------- /본문/cpp/Code_3_05_1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int N = 10000; // N은 시행 횟수(적절하게 변경해서 사용해 주세요) 6 | int M = 0; 7 | for (int i = 1; i <= N; i++) { 8 | double px = rand() / (double)RAND_MAX; // 0 이상 1 이하의 랜덤한 숫자 생성 9 | double py = rand() / (double)RAND_MAX; // 0 이상 1 이하의 랜덤한 숫자 생성 10 | // 원점에서의 거리 sqrt(px * px + py * py)가 11 | // 1 이하라면 원 안에 있는 것이므로, 조건을 "px * px + py * py <= 1"로 설정 12 | if (px * px + py * py <= 1.0) M += 1; 13 | } 14 | printf("%.12lf\n", 4.0 * M / N); 15 | return 0; 16 | } -------------------------------------------------------------------------------- /본문/cpp/Code_3_06_1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int N, A[200009]; 6 | 7 | int main() { 8 | // 입력(예를 들어 N=5, A[1]=3, A[2]=1, A[3]=4, A[4]=1, A[5]=5를 입력한 경우) 9 | cin >> N; 10 | for (int i = 1; i <= N; i++) cin >> A[i]; 11 | 12 | // 정렬(반개구간 [1, N+1)을 정렬하므로, 매개변수에 A+1, A+N+1을 지정합니다). 13 | // sort 함수로 배열의 내용이 [3,1,4,1,5]에서 [1,1,3,4,5]로 바뀝니다. 14 | sort(A + 1, A + N + 1); 15 | 16 | // 출력(1, 1, 3, 4, 5 순서로 출력됨) 17 | for (int i = 1; i <= N; i++) cout << A[i] << endl; 18 | return 0; 19 | } -------------------------------------------------------------------------------- /본문/cpp/Code_3_06_2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int N, A[200009]; 5 | 6 | int main() { 7 | // 입력 8 | cin >> N; 9 | for (int i = 1; i <= N; i++) cin >> A[i]; 10 | 11 | // 선택 정렬 12 | for (int i = 1; i <= N - 1; i++) { 13 | int Min = i, Min_Value = A[i]; 14 | for (int j = i + 1; j <= N; j++) { 15 | if (A[j] < Min_Value) { 16 | Min = j; // Min은 최솟값의 인덱스(1~N) 17 | Min_Value = A[j]; // min_value는 현재 시점에서 최소값 18 | } 19 | } 20 | swap(A[i], A[Min]); 21 | } 22 | 23 | // 출력 24 | for (int i = 1; i <= N; i++) cout << A[i] << endl; 25 | return 0; 26 | } -------------------------------------------------------------------------------- /본문/cpp/Code_3_06_3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int func(int N) { 5 | if (N == 1) return 1; // 이처럼 재귀 함수를 끝내는 경우를 "베이스 케이스"라고 부릅니다. 6 | return func(N - 1) * N; 7 | } 8 | 9 | int main() { 10 | int N; 11 | cin >> N; 12 | cout << func(N) << endl; 13 | return 0; 14 | } -------------------------------------------------------------------------------- /본문/cpp/Code_3_06_4.cpp: -------------------------------------------------------------------------------- 1 | long long GCD(long long A, long long B) { 2 | if (B == 0) return A; // 베이스 케이스 3 | return GCD(B, A % B); 4 | } -------------------------------------------------------------------------------- /본문/cpp/Code_3_06_5.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int N, A[109]; 5 | 6 | int solve(int l, int r) { 7 | if (r - l == 1) return A[l]; 8 | int m = (l + r) / 2; // 구간 [l, r)의 중앙을 기준으로 분할 정복 9 | int s1 = solve(l, m); // s1는 A[l]+...+A[m-1]의 합계 10 | int s2 = solve(m, r); // s2는 A[m]+...+A[r-1]의 합계 11 | return s1 + s2; 12 | } 13 | 14 | int main() { 15 | // 입력 16 | cin >> N; 17 | for (int i = 1; i <= N; i++) cin >> A[i]; 18 | 19 | // 재귀함수 호출 → 답 출력 20 | int Answer = solve(1, N + 1); 21 | cout << Answer << endl; 22 | return 0; 23 | } -------------------------------------------------------------------------------- /본문/cpp/Code_3_06_6.cpp: -------------------------------------------------------------------------------- 1 | int func(int N) { 2 | return func(N - 1) * N; 3 | } -------------------------------------------------------------------------------- /본문/cpp/Code_3_07_1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int N, H[100009], dp[100009]; 7 | 8 | int main() { 9 | // 입력 10 | cin >> N; 11 | for (int i = 1; i <= N; i++) cin >> H[i]; 12 | 13 | // 동적계획법 → 답 출력 14 | for (int i = 1; i <= N; i++) { 15 | if (i == 1) dp[i] = 0; 16 | if (i == 2) dp[i] = abs(H[i - 1] - H[i]); 17 | if (i >= 3) { 18 | int v1 = dp[i - 1] + abs(H[i - 1] - H[i]); // 1개 이전 발판에서 점프할 때 19 | int v2 = dp[i - 2] + abs(H[i - 2] - H[i]); // 2개 이전 발판에서 점프할 때 20 | dp[i] = min(v1, v2); 21 | } 22 | } 23 | cout << dp[N] << endl; 24 | return 0; 25 | } -------------------------------------------------------------------------------- /본문/cpp/Code_3_07_2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int N, dp[54]; 5 | 6 | int main() { 7 | // 입력 8 | cin >> N; 9 | 10 | // 동적계획법 → 답 출력 11 | for (int i = 0; i <= N; i++) { 12 | if (i <= 1) dp[i] = 1; 13 | else dp[i] = dp[i - 1] + dp[i - 2]; 14 | } 15 | cout << dp[N] << endl; 16 | return 0; 17 | } -------------------------------------------------------------------------------- /본문/cpp/Code_3_08_1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | long long N, X, A[1000009]; 6 | 7 | int main() { 8 | // 입력 9 | cin >> N >> X; 10 | for (int i = 1; i <= N; i++) cin >> A[i]; 11 | 12 | // 배열 정렬 13 | sort(A + 1, A + N + 1); 14 | 15 | // 이진탐색 16 | int left = 1, right = N; 17 | while (left <= right) { 18 | int mid = (left + right) / 2; // 탐색 범위 중앙으로 분할하기 19 | if (A[mid] == X) { cout << "Yes" << endl; return 0; } 20 | if (A[mid] > X) right = mid - 1; // 탐색 범위를 앞부분으로 변경 21 | if (A[mid] < X) left = mid + 1; // 탐색 범위를 뒷부분으로 변경 22 | } 23 | 24 | // 더 이상 탐색할 것이 없는 경우도 No를 출력 25 | cout << "No" << endl; 26 | return 0; 27 | } -------------------------------------------------------------------------------- /본문/cpp/Code_4_02_1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int N, A[100009], B[100009]; 5 | int Q, L[100009], R[100009]; 6 | 7 | int main() { 8 | // 입력 → 누적합 구하기 9 | cin >> N >> Q; 10 | for (int i = 1; i <= N; i++) cin >> A[i]; 11 | for (int j = 1; j <= Q; j++) cin >> L[j] >> R[j]; 12 | B[0] = 0; 13 | for (int i = 1; i <= N; i++) B[i] = B[i - 1] + A[i]; 14 | 15 | // 답 출력 16 | for (int j = 1; j <= Q; j++) cout << B[R[j]] - B[L[j] - 1] << endl; 17 | return 0; 18 | } -------------------------------------------------------------------------------- /본문/cpp/Code_4_02_2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int N, B[100009]; 5 | int Q, L[100009], R[100009], X[100009]; 6 | 7 | int main() { 8 | // 입력/계차 계산하기 9 | cin >> N >> Q; 10 | for (int i = 1; i <= Q; i++) { 11 | cin >> L[i] >> R[i] >> X[i]; 12 | B[L[i]] += X[i]; 13 | B[R[i] + 1] -= X[i]; 14 | } 15 | 16 | // 답 출력 17 | for (int i = 2; i <= N; i++) { 18 | if (B[i] > 0) cout << "<"; 19 | if (B[i] == 0) cout << "="; 20 | if (B[i] < 0) cout << ">"; 21 | } 22 | cout << endl; 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /본문/cpp/Code_4_03_1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | double r = 2.0; // √2를 구할 것이므로 6 | double a = 2.0; // 초깃값은 적당한 수를 할당합니다. 7 | 8 | for (int i = 1; i <= 5; i++) { 9 | // 점 (a, f(a))의 x 좌표와 y 좌표를 구합니다. 10 | double japyo_x = a; 11 | double japyo_y = a * a; 12 | 13 | // 접선의 기울기 구하기[y = (jupseon_a)x + jupseon_b] 14 | double jupseon_a = 2.0 * japyo_x; 15 | double jupseon_b = japyo_y - jupseon_a * japyo_x; 16 | 17 | // 다음 a의 값 next_a를 구합니다. 18 | double next_a = (r - jupseon_b) / jupseon_a; 19 | printf("Step #%d: a = %.12lf -> %.12lf\n", i, a, next_a); 20 | a = next_a; 21 | } 22 | return 0; 23 | } -------------------------------------------------------------------------------- /본문/cpp/Code_4_04_1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int N; 5 | bool prime[100000009]; 6 | 7 | int main() { 8 | // 입력 → 배열 초기화 9 | cin >> N; 10 | for (int i = 2; i <= N; i++) prime[i] = true; 11 | 12 | // 에라토스테네스의 체 13 | for (int i = 2; i * i <= N; i++) { 14 | if (prime[i] == true) { 15 | for (int x = 2 * i; x <= N; x += i) prime[x] = false; 16 | } 17 | } 18 | 19 | // N 이하의 소수를 오름차순으로 출력 20 | for (int i = 2; i <= N; i++) { 21 | if (prime[i] == true) cout << i << endl; 22 | } 23 | return 0; 24 | } -------------------------------------------------------------------------------- /본문/cpp/Code_4_05_1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int N, M, A[100009], B[100009]; 6 | vector G[100009]; // G[i]는 정점i와 인접한 정점 리스트 7 | 8 | int main() { 9 | // 입력 10 | cin >> N >> M; 11 | for (int i = 1; i <= M; i++) { 12 | cin >> A[i] >> B[i]; 13 | G[A[i]].push_back(B[i]); // 정점 A[i]와 인접한 정점으로 B[i]를 추가 14 | G[B[i]].push_back(A[i]); // 정점 B[i]와 인접한 정점으로 A[i]를 추가 15 | } 16 | 17 | // 출력(G[i].size()는 정점 i에 인접한 정점 리스트의 크기이므로 "차수"입니다). 18 | for (int i = 1; i <= N; i++) { 19 | cout << i << ": {"; 20 | for (int j = 0; j < G[i].size(); j++) { 21 | if (j >= 1) cout << ","; // 쉼표로 구분해서 출력 22 | cout << G[i][j]; // // G[i].get(j)는 정점i와 인접한 정점 중에서 j번째 정점 23 | } 24 | cout << "}" << endl; 25 | } 26 | return 0; 27 | } -------------------------------------------------------------------------------- /본문/cpp/Code_4_06_1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int N, a[10000009]; 5 | 6 | int main() { 7 | cin >> N; 8 | a[1] = 1; a[2] = 1; 9 | for (int i = 3; i <= N; i++) a[i] = a[i - 1] + a[i - 2]; 10 | cout << a[N] % 1000000007 << endl; 11 | return 0; 12 | } -------------------------------------------------------------------------------- /본문/cpp/Code_4_06_2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int N, a[10000009]; 5 | 6 | int main() { 7 | cin >> N; 8 | a[1] = 1; a[2] = 1; 9 | for (int i = 3; i <= N; i++) a[i] = (a[i - 1] + a[i - 2]) % 1000000007; 10 | cout << a[N] % 1000000007 << endl; 11 | return 0; 12 | } -------------------------------------------------------------------------------- /본문/cpp/Code_4_06_3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const long long mod = 1000000007; 5 | long long a, b, Answer = 1; // a의 0 제곱은 1이므로, Answer=1로 초기화해둡니다. 6 | 7 | int main() { 8 | cin >> a >> b; 9 | for (int i = 1; i <= b; i++) { 10 | Answer = (Answer * a) % mod; 11 | } 12 | cout << Answer << endl; 13 | return 0; 14 | } -------------------------------------------------------------------------------- /본문/cpp/Code_4_06_4.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | long long modpow(long long a, long long b, long long m) { 5 | // 반복제곱법(p에는 a^1, a^2, a^4, a^8, ...가 할당됨) 6 | long long p = a, Answer = 1; 7 | for (int i = 0; i < 30; i++) { 8 | if ((b & (1 << i)) != 0) { Answer *= p; Answer %= m; } 9 | p *= p; p %= m; 10 | } 11 | return Answer; 12 | } 13 | 14 | const long long mod = 1000000007; 15 | long long a, b; 16 | 17 | int main() { 18 | cin >> a >> b; 19 | cout << modpow(a, b, mod) << endl; 20 | return 0; 21 | } -------------------------------------------------------------------------------- /본문/cpp/Code_5_02_1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | long long N; 6 | cin >> N; 7 | if (N % 4 == 1) cout << "2" << endl; 8 | if (N % 4 == 2) cout << "4" << endl; 9 | if (N % 4 == 3) cout << "8" << endl; 10 | if (N % 4 == 0) cout << "6" << endl; 11 | return 0; 12 | } -------------------------------------------------------------------------------- /본문/cpp/Code_5_02_2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | long long N; 6 | cin >> N; 7 | if (N % 4 == 0) cout << "Second" << endl; // 후수필승 8 | else cout << "First" << endl; // 선수필승 9 | return 0; 10 | } -------------------------------------------------------------------------------- /본문/cpp/Code_5_03_1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int N; 6 | cin >> N; 7 | if (N % 2 == 0) cout << "Yes" << endl; 8 | else cout << "No" << endl; 9 | return 0; 10 | } -------------------------------------------------------------------------------- /본문/cpp/Code_5_03_2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int N, K, A[59]; 5 | int sum = 0; // A[1] + A[2] + ... + A[N]의 값 6 | 7 | int main() { 8 | // 입력 → 수열 요소의 총합 sum 구하기 9 | cin >> N >> K; 10 | for (int i = 1; i <= N; i++) cin >> A[i]; 11 | for (int i = 1; i <= N; i++) sum += A[i]; 12 | 13 | // 답 출력 14 | if (sum % 2 != K % 2) cout << "No" << endl; 15 | else if (sum > K) cout << "No" << endl; 16 | else cout << "Yes" << endl; 17 | return 0; 18 | } -------------------------------------------------------------------------------- /본문/cpp/Code_5_04_1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int main() { 7 | // 입력 8 | int N, K; 9 | cin >> N >> K; 10 | 11 | // 사건B의 개수를 yeosagun(여사건) 변수로 세고 → 답 출력 12 | long long yeosagun = 0; 13 | for (int a = 1; a <= N; a++) { 14 | for (int b = max(1, a - (K - 1)); b <= min(N, a + (K - 1)); b++) { 15 | for (int c = max(1, a - (K - 1)); c <= min(N, a + (K - 1)); c++) { 16 | if (abs(b - c) <= K - 1) yeosagun += 1; 17 | } 18 | } 19 | } 20 | cout << (long long)N * N * N - yeosagun << endl; 21 | return 0; 22 | } -------------------------------------------------------------------------------- /본문/cpp/Code_5_05_1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | long long a, b, c, d; 7 | cin >> a >> b >> c >> d; 8 | cout << max({ a * c, a * d, b * c, b * d }) << endl; 9 | return 0; 10 | } -------------------------------------------------------------------------------- /본문/cpp/Code_5_06_1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int A, B, Answer = 0; 5 | 6 | // 작은 문제 t를 푸는 함수 7 | bool shou_mondai(int t) { 8 | int cl = (A + t - 1) / t; // A÷t의 소수점 아래를 자릅니다. 9 | int cr = B / t; // B÷t의 소수점 아래를 자릅니다. 10 | if (cr - cl >= 1) return true; 11 | return false; 12 | } 13 | 14 | int main() { 15 | cin >> A >> B; 16 | for (int i = 1; i <= B; i++) { 17 | if (shou_mondai(i) == true) Answer = i; 18 | } 19 | cout << Answer << endl; 20 | return 0; 21 | } -------------------------------------------------------------------------------- /본문/cpp/Code_5_07_1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | long long N, A[200009], Answer = 0; 5 | 6 | int main() { 7 | // 입력 8 | cin >> N; 9 | for (int i = 1; i <= N; i++) cin >> A[i]; 10 | 11 | // 답 구하기 → 답 출력 12 | for (int i = 1; i <= N; i++) Answer += A[i] * (-N + 2LL * i - 1LL); 13 | cout << Answer << endl; 14 | return 0; 15 | } -------------------------------------------------------------------------------- /본문/cpp/Code_5_09_1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | // 입력 6 | long long N, Answer = 0; 7 | cin >> N; 8 | 9 | // 지불 방법 시뮬레이션 → 답 출력 10 | while (N >= 10000) { N -= 10000; Answer += 1; } 11 | while (N >= 5000) { N -= 5000; Answer += 1; } 12 | while (N >= 1) { N -= 1000; Answer += 1; } 13 | cout << Answer << endl; 14 | return 0; 15 | } -------------------------------------------------------------------------------- /본문/cpp/Code_5_09_2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int N, L[2009], R[2009]; 6 | int Current_Time = 0, Answer = 0; // Current_Time은 현재 시각(직전에 보았던 영화의 종료 시각) 7 | 8 | int main() { 9 | // 입력 10 | cin >> N; 11 | for (int i = 1; i <= N; i++) cin >> L[i] >> R[i]; 12 | 13 | // 영화 선택 시뮬레이션 14 | // 볼 수 있는 영화 중 종료 시간 최솟값 min_endtime은 일단 1000000(INF로 설정)처럼 나올 수 없는 값으로 설정합니다. 15 | while (true) { 16 | int min_endtime = 1000000; 17 | for (int i = 1; i <= N; i++) { 18 | if (L[i] < Current_Time) continue; 19 | min_endtime = min(min_endtime, R[i]); 20 | } 21 | if (min_endtime == 1000000) break; 22 | Current_Time = min_endtime; 23 | Answer += 1; 24 | } 25 | 26 | // 답 출력 27 | cout << Answer << endl; 28 | return 0; 29 | } -------------------------------------------------------------------------------- /본문/cpp/Code_5_10_1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | long long a, b, c; 7 | cin >> a >> b >> c; 8 | if (sqrt(a) + sqrt(b) < sqrt(c)) cout << "Yes" << endl; 9 | else cout << "No" << endl; 10 | return 0; 11 | } -------------------------------------------------------------------------------- /본문/cpp/Code_5_10_2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | long long a, b, c; 6 | cin >> a >> b >> c; 7 | if (c - a - b < 0LL) cout << "No" << endl; 8 | else if (4LL * a * b < (c - a - b) * (c - a - b)) cout << "Yes" << endl; 9 | else cout << "No" << endl; 10 | return 0; 11 | } -------------------------------------------------------------------------------- /본문/cpp/Code_5_10_3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | long long N, X, Y; 5 | 6 | int main() { 7 | // 입력 8 | cin >> N >> X >> Y; 9 | 10 | // 4개의 정수 (a, b, c, d)를 전체 탐색 → 답 출력 11 | for (int a = 1; a <= N; a++) { 12 | for (int b = a; b <= N; b++) { 13 | for (int c = b; c <= N; c++) { 14 | for (int d = c; d <= N; d++) { 15 | if (a + b + c + d == X && 1LL * a * b * c * d == Y) { 16 | cout << "Yes" << endl; 17 | return 0; // 프로그램 실행을 종료합니다. 18 | } 19 | } 20 | } 21 | } 22 | } 23 | cout << "No" << endl; // 하나도 찾지 못했다면 No를 출력 24 | return 0; 25 | } -------------------------------------------------------------------------------- /본문/cpp/Code_5_10_4.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int N, depth = 0; 6 | string S; 7 | 8 | int main() { 9 | // 입력 10 | cin >> N >> S; 11 | 12 | // (의 수에서 )의 수를 뺀 값을 depth에 할당합니다. 13 | // 중간에 depth가 음수가 된다면, 곧바로 답이 No입니다. 14 | for (int i = 0; i < N; i++) { 15 | if (S[i] == '(') depth += 1; 16 | if (S[i] == ')') depth -= 1; 17 | if (depth < 0) { 18 | cout << "No" << endl; 19 | return 0; 20 | } 21 | } 22 | 23 | // 마지막으로 depth = 0[(와 )의 수가 같음]을 추가적으로 판정합니다. 24 | if (depth == 0) cout << "Yes" << endl; 25 | else cout << "No" << endl; 26 | return 0; 27 | } -------------------------------------------------------------------------------- /본문/java/Code_2_01_1.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Code_2_01_1 { 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | int N = sc.nextInt(); // 입력 부분 7 | System.out.println(5 + N); // 출력 부분 8 | } 9 | }; 10 | -------------------------------------------------------------------------------- /본문/java/Code_2_01_2.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Code_2_01_2 { 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | int[] A = new int[4]; 7 | // 입력 부분 8 | A[1] = sc.nextInt(); 9 | A[2] = sc.nextInt(); 10 | A[3] = sc.nextInt(); 11 | // 출력 부분 12 | System.out.println(A[1] + A[2] + A[3]); 13 | } 14 | }; 15 | -------------------------------------------------------------------------------- /본문/java/Code_2_01_3.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Code_2_01_3 { 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | int N = sc.nextInt(); 7 | int[] A = new int[N + 1]; 8 | int answer = 0; 9 | for (int i = 1; i <= N; i++) { 10 | A[i] = sc.nextInt(); 11 | answer += A[i]; 12 | } 13 | System.out.println(answer); 14 | } 15 | }; 16 | -------------------------------------------------------------------------------- /본문/java/Code_2_01_4.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Code_2_01_4 { 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | int N = sc.nextInt(); // 입력 부분 7 | String answer = ""; // String은 문자열 자료형 8 | while (N >= 1) { 9 | // N % 2는 N를 2로 나눈 나머지(예: N = 13의 경우 1) 10 | // N / 2는 N를 2로 나눈 몫(예:N = 13의 경우 6) 11 | if (N % 2 == 0) { 12 | answer = "0" + answer; 13 | } 14 | if (N % 2 == 1) { 15 | answer = "1" + answer; 16 | } 17 | N = N / 2; 18 | } 19 | System.out.println(answer); // 출력 부분 20 | } 21 | }; 22 | -------------------------------------------------------------------------------- /본문/java/Code_2_02_2.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Code_2_02_2 { 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | int a = sc.nextInt(); // a 입력 7 | int b = sc.nextInt(); // b 입력 8 | System.out.println(a & b); // a AND b의 값을 출력 9 | System.out.println(a | b); // a OR b의 값을 출력 10 | System.out.println(a ^ b); // a XOR b의 값을 출력 11 | } 12 | }; 13 | -------------------------------------------------------------------------------- /본문/java/Code_2_03_1.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Code_2_03_1 { 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | System.out.println(func1()); // "2021"이라고 출력 7 | System.out.println(func2(500)); // "1501"이라고 출력 8 | System.out.println(func2(500)); // "1502"라고 출력 9 | } 10 | static int cnt = 1000; 11 | static int func1() { 12 | return 2021; 13 | } 14 | static int func2(int pos) { 15 | cnt += 1; 16 | return cnt + pos; 17 | } 18 | }; 19 | -------------------------------------------------------------------------------- /본문/java/Code_2_04_1.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Code_2_04_1 { 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | int N = sc.nextInt(); 7 | System.out.println(2 * N + 3); 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /본문/java/Code_2_04_2.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Code_2_04_2 { 4 | public static void main(String[] args) { 5 | // 입력 6 | Scanner sc = new Scanner(System.in); 7 | int N = sc.nextInt(); 8 | int X = sc.nextInt(); 9 | int Y = sc.nextInt(); 10 | 11 | // 답 구하기 12 | int cnt = 0; 13 | for (int i = 1; i <= N; i++) { 14 | // mod 계산은 2.2절 참고 15 | if (i % X == 0 || i % Y == 0) { 16 | cnt++; 17 | } 18 | } 19 | 20 | // 출력 21 | System.out.println(cnt); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /본문/java/Code_2_04_3.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Code_2_04_3 { 4 | public static void main(String[] args) { 5 | // 입력 6 | Scanner sc = new Scanner(System.in); 7 | int N = sc.nextInt(); 8 | int S = sc.nextInt(); 9 | 10 | // 답 구하기 11 | int cnt = 0; 12 | for (int i = 1; i <= N; i++) { 13 | for (int j = 1; j <= N; j++) { 14 | if (i + j <= S) { 15 | cnt++; 16 | } 17 | } 18 | } 19 | 20 | // 출력 21 | System.out.println(cnt); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /본문/java/Code_3_01_1.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Code_3_01_1 { 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | long N = sc.nextLong(); 7 | boolean answer = isPrime(N); 8 | if (answer == true) { 9 | System.out.println("prime"); 10 | } 11 | else { 12 | System.out.println("not prime"); 13 | } 14 | } 15 | static boolean isPrime(long N) { 16 | // 2 이상의 정수 N을 매개변수로 받고, N이 소수라면 True를, 17 | // 아니라면 False를 리턴하는 함수 18 | for (long i = 2; i <= N - 1; i++) { 19 | if (N % i == 0) { 20 | return false; // N이 i으로 나누어지는 경우, 이 시점에서 소수가 아니라는 것을 알 수 있음 21 | } 22 | } 23 | return true; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /본문/java/Code_3_01_2.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Code_3_01_2 { 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | long N = sc.nextLong(); 7 | boolean answer = isPrime(N); 8 | if (answer == true) { 9 | System.out.println("prime"); 10 | } 11 | else { 12 | System.out.println("not prime"); 13 | } 14 | } 15 | static boolean isPrime(long N) { 16 | // 2 이상의 정수 N을 매개변수로 받고, N이 소수라면 True를, 17 | // 아니라면 False를 리턴하는 함수 18 | for (long i = 2; i * i <= N; i++) { 19 | if (N % i == 0) { 20 | return false; 21 | } 22 | } 23 | return true; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /본문/java/Code_3_01_3.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Code_3_01_3 { 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | long N = sc.nextLong(); 7 | for (long i = 1; i * i <= N; i++) { 8 | if (N % i == 0) { 9 | System.out.println(i); // i를 약수로 추가 10 | if (i != N / i) { 11 | System.out.println(N / i); // i ≠ N/i라면, N/i도 약수에 추가 12 | } 13 | } 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /본문/java/Code_3_02_1.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Code_3_02_1 { 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | long A = sc.nextLong(); 7 | long B = sc.nextLong(); 8 | System.out.println(GCD(A, B)); 9 | } 10 | static long GCD(long A, long B) { 11 | // 양의 정수 A와 B의 최대 공약수를 리턴하는 함수 12 | // GCD는 Greatest Common Divisor(최대 공약수)의 약자 13 | long ans = 0; 14 | for (long i = 1; i <= A && i <= B; i++) { 15 | if (A % i == 0 && B % i == 0) { 16 | ans = i; 17 | } 18 | } 19 | return ans; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /본문/java/Code_3_02_2.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Code_3_02_2 { 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | long A = sc.nextLong(); 7 | long B = sc.nextLong(); 8 | System.out.println(GCD(A, B)); 9 | } 10 | static long GCD(long A, long B) { 11 | // 양의 정수 A와 B의 최대 공약수를 리턴하는 함수 12 | // GCD는 Greatest Common Divisor(최대 공약수)의 약자 13 | while (A >= 1 && B >= 1) { 14 | if (A < B) { 15 | B %= A; // A < B라면, 큰 수를 b로 변경합니다. 16 | } 17 | else { 18 | A %= B; // A >= B라면 큰 수를 a로 변경합니다. 19 | } 20 | } 21 | if (A >= 1) { 22 | return A; 23 | } 24 | return B; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /본문/java/Code_3_03_1.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Code_3_03_1 { 4 | public static void main(String[] args) { 5 | // 입력 6 | Scanner sc = new Scanner(System.in); 7 | int N = sc.nextInt(); 8 | int[] A = new int[N + 1]; 9 | for (int i = 1; i <= N; i++) { 10 | A[i] = sc.nextInt(); 11 | } 12 | 13 | // 5개의 카드 번호(i, j, k, l, m)를 전체 탐색 14 | int answer = 0; 15 | for (int i = 1; i <= N; i++) { 16 | for (int j = i + 1; j <= N; j++) { 17 | for (int k = j + 1; k <= N; k++) { 18 | for (int l = k + 1; l <= N; l++) { 19 | for (int m = l + 1; m <= N; m++) { 20 | if (A[i] + A[j] + A[k] + A[l] + A[m] == 1000) { 21 | answer++; 22 | } 23 | } 24 | } 25 | } 26 | } 27 | } 28 | 29 | // 출력 30 | System.out.println(answer); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /본문/java/Code_3_04_1.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Code_3_04_1 { 4 | public static void main(String[] args) { 5 | // 입력 6 | Scanner sc = new Scanner(System.in); 7 | int N = sc.nextInt(); 8 | int[] B = new int[N + 1]; 9 | int[] R = new int[N + 1]; 10 | for (int i = 1; i <= N; i++) { 11 | B[i] = sc.nextInt(); 12 | } 13 | for (int i = 1; i <= N; i++) { 14 | R[i] = sc.nextInt(); 15 | } 16 | 17 | // 답 계산/출력 18 | double blue = 0.0, red = 0.0; 19 | for (int i = 1; i <= N; i++) { 20 | blue += (double)B[i] / N; 21 | red += (double)R[i] / N; 22 | } 23 | System.out.format("%.12f\n", blue + red); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /본문/java/Code_3_04_2.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Code_3_04_2 { 4 | public static void main(String[] args) { 5 | // 입력 6 | Scanner sc = new Scanner(System.in); 7 | int N = sc.nextInt(); 8 | int[] P = new int[N + 1]; 9 | int[] Q = new int[N + 1]; 10 | for (int i = 1; i <= N; i++) { 11 | P[i] = sc.nextInt(); 12 | Q[i] = sc.nextInt(); 13 | } 14 | 15 | // 답 계산/출력 16 | double answer = 0.0; 17 | for (int i = 1; i <= N; i++) { 18 | answer += (double)Q[i] / P[i]; 19 | } 20 | System.out.format("%.12f\n", answer); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /본문/java/Code_3_05_1.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Code_3_05_1 { 4 | public static void main(String[] args) { 5 | Random rd = new Random(); 6 | int N = 10000; // N은 시행 횟수(적절하게 변경해서 사용해 주세요) 7 | int M = 0; 8 | for (int i = 1; i <= N; i++) { 9 | double px = rd.nextDouble(); // 0 이상, 1 미만의 랜덤한 수 생성 10 | double py = rd.nextDouble(); // 0 이상, 1 미만의 랜덤한 수 생성 11 | // 원점에서의 거리 sqrt(px * px + py * py)가 12 | // 1이어야 하므로, 양쪽에 제곱해서 px * px + py * py <= 1을 확인 13 | if (px * px + py * py <= 1.0) { 14 | M++; 15 | } 16 | } 17 | System.out.format("%.12f\n", 4.0 * M / N); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /본문/java/Code_3_06_1.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Code_3_06_1 { 4 | public static void main(String[] args) { 5 | // 입력(예를 들어서 N = 5, A = [3, 1, 4, 1, 5]를 입력하면) 6 | Scanner sc = new Scanner(System.in); 7 | int N = sc.nextInt(); 8 | int[] A = new int[N]; 9 | for (int i = 1; i <= N; i++) { 10 | A[i - 1] = sc.nextInt(); 11 | } 12 | 13 | // 배열A 전체를 정렬 14 | Arrays.sort(A); 15 | 16 | // 출력(1, 1, 3, 4, 5 순서로 출력됨) 17 | for (int i = 1; i <= N; i++) { 18 | System.out.println(A[i - 1]); 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /본문/java/Code_3_06_3.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Code_3_06_3 { 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | int N = sc.nextInt(); 7 | System.out.println(func(N)); 8 | } 9 | static int func(int N) { 10 | if (N == 1) { 11 | return 1; // 이처럼 재귀 함수를 끝내는 경우를 "베이스 케이스"라고 부릅니다. 12 | } 13 | return func(N - 1) * N; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /본문/java/Code_3_06_4.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Code_3_06_4 { 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | long A = sc.nextLong(); 7 | long B = sc.nextLong(); 8 | System.out.println(GCD(A, B)); 9 | } 10 | static long GCD(long A, long B) { 11 | if (B == 0) { 12 | return A; // 베이스 케이스 13 | } 14 | return GCD(B, A % B); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /본문/java/Code_3_06_5.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Code_3_06_5 { 4 | static int N; 5 | static int[] A; 6 | public static void main(String[] args) { 7 | // 입력 8 | Scanner sc = new Scanner(System.in); 9 | N = sc.nextInt(); 10 | A = new int[N + 1]; 11 | for (int i = 1; i <= N; i++) { 12 | A[i] = sc.nextInt(); 13 | } 14 | 15 | // 재귀함수 호출 → 답 출력 16 | int answer = solve(1, N + 1); 17 | System.out.println(answer); 18 | } 19 | static int solve(int l, int r) { 20 | if (r - l == 1) { 21 | return A[l]; 22 | } 23 | int m = (l + r) / 2; // 구간 [l, r)의 중앙을 기준으로 분할 정복 24 | int s1 = solve(l, m); // s1는 A[l] + ... + A[m-1]의 합계를 계산 25 | int s2 = solve(m, r); // s2는 A[m] + ... + A[r-1]는 합계를 계산 26 | return s1 + s2; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /본문/java/Code_3_06_6.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Code_3_06_6 { 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | int N = sc.nextInt(); 7 | int answer = func(N); 8 | System.out.println(answer); 9 | } 10 | static int func(int N) { 11 | // func(N) → func(N-1) → ... → func(0) → func(-1) → func(-2) → ...처럼 무한하게 호출되어서, 프로그램이 정상적으로 작동하지 않습니다. 12 | return func(N - 1) * N; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /본문/java/Code_3_07_1.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Code_3_07_1 { 4 | public static void main(String[] args) { 5 | // 입력 6 | Scanner sc = new Scanner(System.in); 7 | int N = sc.nextInt(); 8 | int[] H = new int[N + 1]; 9 | for (int i = 1; i <= N; i++) { 10 | H[i] = sc.nextInt(); 11 | } 12 | 13 | // 동적계획법 14 | int[] dp = new int[N + 1]; 15 | dp[1] = 0; 16 | for (int i = 2; i <= N; i++) { 17 | if (i == 2) { 18 | dp[i] = Math.abs(H[i - 1] - H[i]); 19 | } 20 | if (i >= 3) { 21 | int v1 = dp[i - 1] + Math.abs(H[i - 1] - H[i]); // 1개 이전 발판에서 점프할 때 22 | int v2 = dp[i - 2] + Math.abs(H[i - 2] - H[i]); // 2개 이전 발판에서 점프할 때 23 | dp[i] = Math.min(v1, v2); 24 | } 25 | } 26 | 27 | // 답 출력 28 | System.out.println(dp[N]); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /본문/java/Code_3_07_2.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Code_3_07_2 { 4 | public static void main(String[] args) { 5 | // 입력 6 | Scanner sc = new Scanner(System.in); 7 | int N = sc.nextInt(); 8 | 9 | // 동적계획법 10 | int[] dp = new int[N + 1]; 11 | for (int i = 0; i <= N; i++) { 12 | if (i <= 1) { 13 | dp[i] = 1; 14 | } 15 | else { 16 | dp[i] = dp[i - 1] + dp[i - 2]; 17 | } 18 | } 19 | 20 | // 답 출력 21 | System.out.println(dp[N]); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /본문/java/Code_4_03_1.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Code_4_03_1 { 4 | public static void main(String[] args) { 5 | double r = 2.0; // √2를 구할 것이므로 6 | double a = 2.0; // 초깃값은 적당한 수를 할당합니다. 7 | for (int i = 1; i <= 5; i++) { 8 | // 점 (a, f(a))의 x 좌표와 y 좌표를 구합니다. 9 | double japyo_x = a; 10 | double japyo_y = a * a; 11 | 12 | // 접선 식 y = jupseon_a * x + jupseon_b를 구합니다. 13 | double jupseon_a = 2.0 * japyo_x; 14 | double jupseon_b = japyo_y - jupseon_a * japyo_x; 15 | 16 | // 다음 a의 값 next_a를 구합니다. 17 | double next_a = (r - jupseon_b) / jupseon_a; 18 | System.out.format("Step #%d: a = %.12f -> %.12f\n", i, a, next_a); 19 | a = next_a; 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /본문/java/Code_4_04_1.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Code_4_04_1 { 4 | public static void main(String[] args) { 5 | // 입력 → 배열 prime 초기화 6 | Scanner sc = new Scanner(System.in); 7 | int N = sc.nextInt(); 8 | boolean[] prime = new boolean[N + 1]; 9 | for (int i = 2; i <= N; i++) { 10 | prime[i] = true; 11 | } 12 | 13 | // 에라토스테네스의 체 14 | for (int i = 2; i * i <= N; i++) { 15 | if (prime[i] == true) { 16 | for (int x = 2 * i; x <= N; x += i) { 17 | prime[x] = false; 18 | } 19 | } 20 | } 21 | 22 | // N 이하의 소수를 오름차순으로 출력 23 | for (int i = 2; i <= N; i++) { 24 | if (prime[i] == true) { 25 | System.out.println(i); 26 | } 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /본문/java/Code_4_06_1.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Code_4_06_1 { 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | int N = sc.nextInt(); 7 | int[] a = new int[N + 1]; 8 | a[1] = 1; a[2] = 1; 9 | for (int i = 3; i <= N; i++) { 10 | a[i] = a[i - 1] + a[i - 2]; 11 | } 12 | System.out.println(a[N] % 1000000007); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /본문/java/Code_4_06_2.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Code_4_06_2 { 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | int N = sc.nextInt(); 7 | int[] a = new int[N + 1]; 8 | a[1] = 1; a[2] = 1; 9 | for (int i = 3; i <= N; i++) { 10 | a[i] = (a[i - 1] + a[i - 2]) % 1000000007; 11 | } 12 | System.out.println(a[N] % 1000000007); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /본문/java/Code_4_06_3.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Code_4_06_3 { 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | final long MOD = 1000000007; 7 | long a = sc.nextLong(); 8 | long b = sc.nextLong(); 9 | long answer = 1; // a의 0은 1이므로, answer = 1로 초기화 10 | for (long i = 1; i <= b; i++) { 11 | answer = (answer * a) % MOD; 12 | } 13 | System.out.println(answer); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /본문/java/Code_4_06_4.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Code_4_06_4 { 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | final long MOD = 1000000007; 7 | long a = sc.nextLong(); 8 | long b = sc.nextLong(); 9 | long answer = modpow(a, b, MOD); 10 | System.out.println(answer); 11 | } 12 | static long modpow(long a, long b, long m) { 13 | // 반복제곱법(p에는 a^1, a^2, a^4, a^8, ...가 할당됨) 14 | long p = a, answer = 1; 15 | for (int i = 0; i < 30; i++) { 16 | if ((b & (1 << i)) != 0) { 17 | answer = (answer * p) % m; 18 | } 19 | p = (p * p) % m; 20 | } 21 | return answer; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /본문/java/Code_5_02_1.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Code_5_02_1 { 4 | public static void main(String[] args) { 5 | // 주의: 다음 프로그램은 N >= 1에서 정상적으로 동작합니다.N=0일 때는 답이 "1"이므로, 조건 분기를 따로 나누어서 출력하면 됩니다. 6 | Scanner sc = new Scanner(System.in); 7 | long N = sc.nextLong(); 8 | if (N % 4 == 1) { 9 | System.out.println(2); 10 | } 11 | if (N % 4 == 2) { 12 | System.out.println(4); 13 | } 14 | if (N % 4 == 3) { 15 | System.out.println(8); 16 | } 17 | if (N % 4 == 0) { 18 | System.out.println(6); 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /본문/java/Code_5_02_2.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Code_5_02_2 { 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | long N = sc.nextLong(); 7 | if (N % 4 == 0) { 8 | System.out.println("Second"); // 후수필승 9 | } 10 | else { 11 | System.out.println("First"); // 선수필승 12 | } 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /본문/java/Code_5_03_1.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Code_5_03_1 { 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | int N = sc.nextInt(); 7 | if (N % 2 == 0) { 8 | System.out.println("Yes"); 9 | } 10 | else { 11 | System.out.println("No"); 12 | } 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /본문/java/Code_5_03_2.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Code_5_03_2 { 4 | public static void main(String[] args) { 5 | // 입력 → 수열 요소의 총합 sum 구하기 6 | Scanner sc = new Scanner(System.in); 7 | int N = sc.nextInt(); 8 | int K = sc.nextInt(); 9 | int[] A = new int[N + 1]; 10 | for (int i = 1; i <= N; i++) { 11 | A[i] = sc.nextInt(); 12 | } 13 | int sum = 0; 14 | for (int i = 1; i <= N; i++) { 15 | sum += A[i]; 16 | } 17 | 18 | // 답 출력 19 | if (sum % 2 != K % 2) { 20 | System.out.println("No"); 21 | } 22 | else if (sum > K) { 23 | System.out.println("No"); 24 | } 25 | else { 26 | System.out.println("Yes"); 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /본문/java/Code_5_04_1.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Code_5_04_1 { 4 | public static void main(String[] args) { 5 | // 입력 6 | Scanner sc = new Scanner(System.in); 7 | int N = sc.nextInt(); 8 | int K = sc.nextInt(); 9 | // 사건B의 개수를 yeosagun(여사건) 변수로 셉니다. 10 | long yeosagun = 0; 11 | for (int a = 1; a <= N; a++) { 12 | int l = Math.max(1, a - (K - 1)); // b, c의 탐색 범위 하한 l 13 | int r = Math.min(N, a + (K - 1)); // b, c의 탐색 범위 상한 r 14 | for (int b = l; b <= r; b++) { 15 | for (int c = l; c <= r; c++) { 16 | if (Math.abs(b - c) <= K - 1) { 17 | yeosagun += 1; 18 | } 19 | } 20 | } 21 | } 22 | // 답 출력 23 | long answer = (long)N * N * N - yeosagun; 24 | System.out.println(answer); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /본문/java/Code_5_05_1.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Code_5_05_1 { 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | long a = sc.nextLong(); 7 | long b = sc.nextLong(); 8 | long c = sc.nextLong(); 9 | long d = sc.nextLong(); 10 | long answer = Collections.max(Arrays.asList(a * c, a * d, b * c, b * d)); 11 | System.out.println(answer); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /본문/java/Code_5_06_1.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Code_5_06_1 { 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | int A = sc.nextInt(); 7 | int B = sc.nextInt(); 8 | int answer = 0; 9 | for (int i = 1; i <= B; i++) { 10 | if (shouMondai(A, B, i) == true) { 11 | answer = i; 12 | } 13 | } 14 | System.out.println(answer); 15 | } 16 | static boolean shouMondai(int A, int B, int t) { 17 | // 작은 문제 t를 푸는 함수 18 | int cl = (A + t - 1) / t; // A÷t의 소수점 아래를 자릅니다. 19 | int cr = B / t; // B÷t의 소수점 아래를 자릅니다. 20 | return (cr - cl >= 1); // cr - cl >= 1이라면 true, 그렇지 않은 경우 false를 리턴 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /본문/java/Code_5_07_1.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Code_5_07_1 { 4 | public static void main(String[] args) { 5 | // 입력 6 | Scanner sc = new Scanner(System.in); 7 | int N = sc.nextInt(); 8 | int[] A = new int[N + 1]; 9 | for (int i = 1; i <= N; i++) { 10 | A[i] = sc.nextInt(); 11 | } 12 | // 답 구하기 13 | long answer = 0; 14 | for (int i = 1; i <= N; i++) { 15 | answer += (long)A[i] * (-N + 2 * i - 1); 16 | } 17 | 18 | // 답 출력 19 | System.out.println(answer); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /본문/java/Code_5_09_1.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Code_5_09_1 { 4 | public static void main(String[] args) { 5 | // 입력 6 | Scanner sc = new Scanner(System.in); 7 | long N = sc.nextLong(); 8 | 9 | // 지불 방법 시뮬레이션 10 | long answer = 0; 11 | while (N >= 10000) { 12 | N -= 10000; // 10000원 이상이라면 10000원으로 지불할 수 있는 만큼 지불 13 | answer += 1; 14 | } 15 | while (N >= 5000) { 16 | N -= 5000; // 5000원 이상이라면 5000원으로 지불할 수 있는 만큼 지불 17 | answer += 1; 18 | } 19 | while (N >= 1) { 20 | N -= 1000; // 남은 금액은 1000원으로 지불 21 | answer += 1; 22 | } 23 | 24 | // 답 출력 25 | System.out.println(answer); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /본문/java/Code_5_10_1.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Code_5_10_1 { 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | long a = sc.nextLong(); 7 | long b = sc.nextLong(); 8 | long c = sc.nextLong(); 9 | if (Math.sqrt(a) + Math.sqrt(b) < Math.sqrt(c)) { 10 | System.out.println("Yes"); 11 | } 12 | else { 13 | System.out.println("No"); 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /본문/java/Code_5_10_2.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Code_5_10_2 { 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | long a = sc.nextLong(); 7 | long b = sc.nextLong(); 8 | long c = sc.nextLong(); 9 | if (c - a - b < 0) { 10 | System.out.println("No"); 11 | } 12 | else if (4 * a * b < (c - a - b) * (c - a - b)) { 13 | System.out.println("Yes"); 14 | } 15 | else { 16 | System.out.println("No"); 17 | } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /본문/java/Code_5_10_3.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Code_5_10_3 { 4 | public static void main(String[] args) { 5 | // 입력 6 | Scanner sc = new Scanner(System.in); 7 | long N = sc.nextLong(); 8 | long X = sc.nextLong(); 9 | long Y = sc.nextLong(); 10 | 11 | // 4개의 정수 (a, b, c, d)를 전체 탐색 12 | boolean flag = false; 13 | for (int a = 1; a <= N; a++) { 14 | for (int b = a; b <= N; b++) { 15 | for (int c = b; c <= N; c++) { 16 | for (int d = c; d <= N; d++) { 17 | if (a + b + c + d == X && (long)a * b * c * d == Y) { 18 | flag = true; // 답을 찾았다면 flag를 true로 변경합니다. 19 | } 20 | } 21 | } 22 | } 23 | } 24 | 25 | // 답 출력 26 | if (flag == true) { 27 | System.out.println("Yes"); 28 | } 29 | else { 30 | System.out.println("No"); 31 | } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /본문/java/Code_5_10_4.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Code_5_10_4 { 4 | public static void main(String[] args) { 5 | // 입력 6 | Scanner sc = new Scanner(System.in); 7 | int N = sc.nextInt(); 8 | String S = sc.next(); 9 | 10 | // (의 수에서 )의 수를 뺀 값을 depth에 할당합니다. 11 | // 중간에 depth가 음수가 된다면, 곧바로 답이 No입니다. 12 | int depth = 0; 13 | boolean flag = true; 14 | for (int i = 0; i < N; i++) { 15 | if (S.charAt(i) == '(') { 16 | depth += 1; 17 | } 18 | if (S.charAt(i) == ')') { 19 | depth -= 1; 20 | } 21 | if (depth < 0) { 22 | flag = false; 23 | } 24 | } 25 | 26 | // 마지막으로 depth = 0[(와 )의 수가 같음]을 추가적으로 판정합니다. 27 | if (flag == true && depth == 0) { 28 | System.out.println("Yes"); 29 | } 30 | else { 31 | System.out.println("No"); 32 | } 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /본문/python/Code_2_01_1.py: -------------------------------------------------------------------------------- 1 | N = int(input()) 2 | print(5 + N) -------------------------------------------------------------------------------- /본문/python/Code_2_01_2.py: -------------------------------------------------------------------------------- 1 | A = map(int, input().split()) 2 | print(sum(A)) 3 | -------------------------------------------------------------------------------- /본문/python/Code_2_01_3.py: -------------------------------------------------------------------------------- 1 | N = int(input()) 2 | A = list(map(int, input().split())) 3 | Answer = 0 4 | 5 | for item in A: 6 | Answer += item 7 | 8 | print(Answer) -------------------------------------------------------------------------------- /본문/python/Code_2_01_4.py: -------------------------------------------------------------------------------- 1 | N = int(input()) # 입력 부분 2 | answer = "" 3 | 4 | while N >= 1: 5 | # N % 2는 N를 2로 나눈 나머지(예: N = 13의 경우 1) 6 | # N // 2는 N를 2로 나눈 때의 정수 부분(몫)(예: N = 13의 경우 6) 7 | if N % 2 == 0: 8 | answer = "0" + answer 9 | if N % 2 == 1: 10 | answer = "1" + answer 11 | N = N // 2 12 | 13 | print(answer) # 출력 부분 14 | -------------------------------------------------------------------------------- /본문/python/Code_2_02_1.py: -------------------------------------------------------------------------------- 1 | # 사칙 연산 2 | print(869 + 120) # 989 3 | print(869 - 120) # 749 4 | print(869 * 120) # 104280 5 | print(869 / 120) # 7.241666666666666 6 | print(869 // 120) # 7 7 | 8 | # 나머지(mod) 9 | print(8 % 5) # 3 10 | print(869 % 120) # 29 11 | 12 | # 절댓값(abs) 13 | print(abs(-45)) # 45 14 | print(abs(15)) # 15 15 | 16 | # 제곱 17 | print(10 ** 2) # 100 18 | print(3 ** 4) # 81 19 | 20 | # 루트(sqrt) 21 | import math 22 | print("{:.5f}".format(math.sqrt(4.0))) # 2.00000 23 | print("{:.5f}".format(math.sqrt(2.0))) # 1.41421 24 | -------------------------------------------------------------------------------- /본문/python/Code_2_02_2.py: -------------------------------------------------------------------------------- 1 | a, b = map(int, input().split()) 2 | print(a & b) # a AND b의 값을 출력 3 | print(a | b) # a OR b의 값을 출력 4 | print(a ^ b) # a XOR b의 값을 출력 5 | -------------------------------------------------------------------------------- /본문/python/Code_2_03_1.py: -------------------------------------------------------------------------------- 1 | cnt = 1000 2 | 3 | def func1(): 4 | return 2021 5 | 6 | def func2(pos): 7 | global cnt # 전역 변수 cnt를 함수 내부에서 사용 8 | cnt += 1 9 | return cnt + pos 10 | 11 | print(func1()) # "2021"이라고 출력 12 | print(func2(500)) # "1501"이라고 출력 13 | print(func2(500)) # "1502"라고 출력 14 | -------------------------------------------------------------------------------- /본문/python/Code_2_04_1.py: -------------------------------------------------------------------------------- 1 | N = int(input()) 2 | print(2 * N + 3) 3 | -------------------------------------------------------------------------------- /본문/python/Code_2_04_2.py: -------------------------------------------------------------------------------- 1 | # 입력 2 | N, X, Y = map(int, input().split()) 3 | 4 | # 답 구하기 5 | cnt = 0 6 | for i in range(1, N + 1): 7 | # mod 계산은 2.2절 참고 8 | if i % X == 0 or i % Y == 0: 9 | cnt += 1 10 | 11 | # 출력 12 | print(cnt) 13 | -------------------------------------------------------------------------------- /본문/python/Code_2_04_3.py: -------------------------------------------------------------------------------- 1 | # 입력 2 | N, S = map(int, input().split()) 3 | 4 | # 답 구하기 5 | answer = 0 6 | for i in range(1, N + 1): 7 | for j in range(1, N + 1): 8 | if i + j <= S: 9 | answer += 1 10 | 11 | # 출력 12 | print(answer) 13 | -------------------------------------------------------------------------------- /본문/python/Code_2_06_1.py: -------------------------------------------------------------------------------- 1 | # 입력 2 | N, S = map(int, input().split()) 3 | A = list(map(int, input().split())) 4 | 5 | # 모든 패턴 탐색: (1<= 1 and B >= 1: 5 | if A < B: 6 | B = B % A # A < B라면, 큰 수를 b로 변경합니다. 7 | else: 8 | A = A % B # A >= B라면 큰 수를 a로 변경합니다. 9 | if A >= 1: 10 | return A 11 | return B 12 | 13 | A, B = map(int, input().split()) 14 | print(GCD(A, B)) 15 | -------------------------------------------------------------------------------- /본문/python/Code_3_03_1.py: -------------------------------------------------------------------------------- 1 | # 입력 2 | N = int(input()) 3 | A = list(map(int, input().split())) 4 | 5 | # 5개의 카드 번호(i, j, k, l, m)를 전체 탐색 6 | answer = 0 7 | for i in range(0, N): 8 | for j in range(i + 1, N): 9 | for k in range(j + 1, N): 10 | for l in range(k + 1, N): 11 | for m in range(l + 1, N): 12 | if A[i] + A[j] + A[k] + A[l] + A[m] == 1000: 13 | answer += 1 14 | 15 | # 출력 16 | print(answer) 17 | -------------------------------------------------------------------------------- /본문/python/Code_3_04_1.py: -------------------------------------------------------------------------------- 1 | # 입력 2 | N = int(input()) 3 | B = list(map(int, input().split())) 4 | R = list(map(int, input().split())) 5 | 6 | # 답 계산 7 | blue = 0.0 8 | red = 0.0 9 | for i in range(N): 10 | blue += B[i] // N 11 | red += R[i] // N 12 | 13 | # 출력 14 | print("{:.12f}".format(blue + red)) 15 | -------------------------------------------------------------------------------- /본문/python/Code_3_04_2.py: -------------------------------------------------------------------------------- 1 | # 입력 2 | N = int(input()) 3 | P = [ None ] * N 4 | Q = [ None ] * N 5 | for i in range(N): 6 | P[i], Q[i] = map(int, input().split()) 7 | 8 | # 답 계산 9 | answer = 0 10 | for i in range(N): 11 | answer += Q[i] / P[i] 12 | 13 | # 출력 14 | print("{:.12f}".format(answer)) 15 | -------------------------------------------------------------------------------- /본문/python/Code_3_05_1.py: -------------------------------------------------------------------------------- 1 | import random 2 | 3 | N = 10000 # N은 시행 횟수(적절하게 변경해서 사용해 주세요) 4 | M = 0 5 | for i in range(N): 6 | px = random.random() # 0 이상, 1 미만의 랜덤한 수 생성 7 | py = random.random() # 0 이상, 1 미만의 랜덤한 수 생성 8 | # 원점에서의 거리 sqrt(px * px + py * py)가 9 | # 1이어야 하므로, 양쪽에 제곱해서 px * px + py * py <= 1을 확인 10 | if px * px + py * py <= 1.0: 11 | M += 1 12 | 13 | print("{:.12f}".format(4 * M / N)) 14 | -------------------------------------------------------------------------------- /본문/python/Code_3_06_1.py: -------------------------------------------------------------------------------- 1 | # 입력(예를 들어서 N = 5, A = [3, 1, 4, 1, 5]를 입력하면) 2 | N = int(input()) 3 | A = list(map(int, input().split())) 4 | 5 | # 배열A 전체를 정렬 6 | A.sort() 7 | 8 | # 출력(1, 1, 3, 4, 5 순서로 출력됨) 9 | for i in range(N): 10 | print(A[i]) 11 | -------------------------------------------------------------------------------- /본문/python/Code_3_06_2.py: -------------------------------------------------------------------------------- 1 | # 입력 2 | N = int(input()) 3 | A = list(map(int, input().split())) 4 | 5 | # 선택 정렬 6 | for i in range(N - 1): 7 | min_position = i 8 | min_value = A[i] 9 | for j in range(i + 1, N): 10 | if A[j] < min_value: 11 | min_position = j # min_position는 최솟값의 인덱스(0~N-1) 12 | min_value = A[j] # min_value는 현재 시점에서의 최솟값 13 | # A[i]과 A[min_position]을 스왑 14 | A[i], A[min_position] = A[min_position], A[i] 15 | 16 | # 출력 17 | for i in range(N): 18 | print(A[i]) 19 | -------------------------------------------------------------------------------- /본문/python/Code_3_06_3.py: -------------------------------------------------------------------------------- 1 | def func(N): 2 | if N == 1: 3 | return 1 # 이처럼 재귀 함수를 끝내는 경우를 "베이스 케이스"라고 부릅니다. 4 | return func(N - 1) * N 5 | 6 | N = int(input()) 7 | print(func(N)) 8 | 9 | # 파이썬은 함수를 호출과 관련된 스택 개수에 제한이 있습니다. 10 | # 기본적으로는 1000이므로, 재귀적으로 함수를 1000번 호출하면 오류가 발생합 니다. 11 | # 이 제한은 sys.getrecursionlimit()으로 확인할 수 있으며, sys.setrecursionlimit(depth)으로 변경할 수 있습니다. 12 | # 추가적으로 이는 sys 모듈의 함수이므로, import sys로 모듈을 읽어 들여야 사용할 수 있습니다. 13 | -------------------------------------------------------------------------------- /본문/python/Code_3_06_4.py: -------------------------------------------------------------------------------- 1 | def GCD(A, B): 2 | if B == 0: 3 | return A # 베이스 케이스 4 | return GCD(B, A % B) 5 | 6 | A, B = map(int, input().split()) 7 | print(GCD(A, B)) 8 | -------------------------------------------------------------------------------- /본문/python/Code_3_06_5.py: -------------------------------------------------------------------------------- 1 | def solve(l, r, A): 2 | if r - l == 1: 3 | return A[l] 4 | m = (l + r) // 2 # 구간 [l, r)의 중앙을 기준으로 분할 정복 5 | s1 = solve(l, m, A) # s1는 A[l] + ... + A[m-1]의 합계를 계산 6 | s2 = solve(m, r, A) # s2는 A[m] + ... + A[r-1]는 합계를 계산 7 | return s1 + s2 8 | 9 | # 입력 10 | N = int(input()) 11 | A = list(map(int, input().split())) 12 | 13 | # 재귀함수 호출 → 답 출력 14 | answer = solve(0, N, A) 15 | print(answer) 16 | -------------------------------------------------------------------------------- /본문/python/Code_3_06_6.py: -------------------------------------------------------------------------------- 1 | def func(N): 2 | # func(N) → func(N-1) → ... → 3 | # func(0) → func(-1) → func(-2) → ...처럼 무한하게 호출되어서 4 | # 프로그램이 정상적으로 작동하지 않습니다. 5 | return func(N - 1) * N 6 | 7 | N = int(input()) 8 | print(func(N)) 9 | -------------------------------------------------------------------------------- /본문/python/Code_3_07_1.py: -------------------------------------------------------------------------------- 1 | # 입력 2 | N = int(input()) 3 | H = list(map(int, input().split())) 4 | 5 | # 동적계획법 6 | dp = [ None ] * N 7 | dp[0] = 0 8 | for i in range(1, N): 9 | if i == 1: 10 | dp[i] = abs(H[i - 1] - H[i]) 11 | if i >= 2: 12 | v1 = dp[i - 1] + abs(H[i - 1] - H[i]) # 1개 이전 발판에서 점프할 때 13 | v2 = dp[i - 2] + abs(H[i - 2] - H[i]) # 2개 이전 발판에서 점프할 때 14 | dp[i] = min(v1, v2) 15 | 16 | # 답 출력 17 | print(dp[N - 1]) 18 | -------------------------------------------------------------------------------- /본문/python/Code_3_07_2.py: -------------------------------------------------------------------------------- 1 | # 입력 2 | N = int(input()) 3 | 4 | # 동적계획법 5 | dp = [ None ] * (N + 1) 6 | for i in range(N + 1): 7 | if i <= 1: 8 | dp[i] = 1 9 | else: 10 | dp[i] = dp[i - 1] + dp[i - 2] 11 | 12 | # 답 출력 13 | print(dp[N]) 14 | -------------------------------------------------------------------------------- /본문/python/Code_3_07_3.py: -------------------------------------------------------------------------------- 1 | # 입력 2 | N, W = map(int, input().split()) 3 | w = [ None ] * N 4 | v = [ None ] * N 5 | for i in range(N): 6 | w[i], v[i] = map(int, input().split()) 7 | 8 | # 배열 초기화 9 | INF = 10 ** 18 10 | dp = [ [ None ] * (W + 1) for i in range(N + 1) ] 11 | dp[0][0] = 0 12 | for i in range(1, W + 1): 13 | dp[0][i] = -INF 14 | 15 | # 동적계획법 16 | for i in range(1, N + 1): 17 | for j in range(0, W + 1): 18 | if j < w[i - 1]: 19 | # j < w[i-1]라면, 방법A로 선택 20 | dp[i][j] = dp[i - 1][j] 21 | else: 22 | # j >= w[i-1]라면 방법A와 방법B 중에 큰 것으로 선택 23 | dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - w[i - 1]] + v[i - 1]) 24 | 25 | # 답을 계산하고 출력 26 | answer = max(dp[N]) 27 | print(answer) 28 | -------------------------------------------------------------------------------- /본문/python/Code_3_08_1.py: -------------------------------------------------------------------------------- 1 | # 입력 2 | N, X = map(int, input().split()) 3 | A = list(map(int, input().split())) 4 | 5 | # 배열 정렬 6 | A.sort() 7 | 8 | # 이진 탐색 9 | answer = "No" 10 | left, right = 0, N - 1 11 | while left <= right: 12 | mid = (left + right) // 2 13 | if A[mid] == X: 14 | answer = "Yes" 15 | break 16 | if A[mid] > X: 17 | # 탐색 범위를 앞부분으로 변경 18 | right = mid - 1 19 | if A[mid] < X: 20 | # 탐색 범위를 뒷부분으로 변경 21 | left = mid + 1 22 | 23 | # 답 출력 24 | print(answer) 25 | -------------------------------------------------------------------------------- /본문/python/Code_4_02_1.py: -------------------------------------------------------------------------------- 1 | N, Q = map(int, input().split()) 2 | A = list(map(int, input().split())) 3 | L = [ None ] * Q 4 | R = [ None ] * Q 5 | for j in range(Q): 6 | L[j], R[j] = map(int, input().split()) 7 | 8 | B = [ None ] * (N + 1) 9 | B[0] = 0 10 | for i in range(N): 11 | B[i + 1] = B[i] + A[i] 12 | 13 | for j in range(Q): 14 | print(B[R[j]] - B[L[j] - 1]) 15 | -------------------------------------------------------------------------------- /본문/python/Code_4_02_2.py: -------------------------------------------------------------------------------- 1 | # 입력 2 | N, Q = map(int, input().split()) 3 | L = [ None ] * Q 4 | R = [ None ] * Q 5 | X = [ None ] * Q 6 | for i in range(Q): 7 | L[i], R[i], X[i] = map(int, input().split()) 8 | 9 | # 계차 계산하기 10 | B = [ 0 ] * (N + 2) 11 | for i in range(Q): 12 | B[L[i]] += X[i] 13 | B[R[i] + 1] -= X[i] 14 | 15 | # 답을 계산하고 출력 16 | answer = "" 17 | for i in range(2, N + 1): 18 | if B[i] > 0: 19 | answer += "<" 20 | if B[i] == 0: 21 | answer += "=" 22 | if B[i] < 0: 23 | answer += ">" 24 | print(answer) 25 | -------------------------------------------------------------------------------- /본문/python/Code_4_03_1.py: -------------------------------------------------------------------------------- 1 | r = 2.0 # √2를 구할 것이므로 2 | a = 2.0 # 초깃값은 적당한 수를 할당합니다. 3 | repeats = 5 4 | 5 | for i in range(1, repeats + 1): 6 | # 점 (a, f(a))의 x 좌표와 y 좌표를 구합니다. 7 | japyo_x, japyo_y = a, a * a 8 | 9 | # 접선 식 y = jupseon_a * x + jupseon_b를 구합니다. 10 | jupseon_a = 2.0 * japyo_x 11 | jupseon_b = japyo_y - jupseon_a * japyo_x 12 | 13 | # 다음 a의 값 next_a를 구합니다. 14 | next_a = (r - jupseon_b) / jupseon_a 15 | print("Step #{}: a = {:.12f} -> {:.12f}".format(i, a, next_a)) 16 | a = next_a 17 | -------------------------------------------------------------------------------- /본문/python/Code_4_04_1.py: -------------------------------------------------------------------------------- 1 | # 입력 → 배열 prime 초기화 2 | N = int(input()) 3 | prime = [ True ] * (N + 1) 4 | 5 | # 에라토스테네스의 체 6 | LIMIT = int(N ** 0.5) 7 | for i in range(2, LIMIT + 1): 8 | if prime[i] == True: 9 | # x = 2i, 3i, 4i, ...로 N 이하의 범위에서 반복 10 | for j in range(2 * i, N + 1, i): 11 | prime[j] = False 12 | 13 | # N 이하의 소수를 오름차순으로 출력 14 | for i in range(2, N + 1): 15 | if prime[i] == True: 16 | print(i) 17 | -------------------------------------------------------------------------------- /본문/python/Code_4_05_1.py: -------------------------------------------------------------------------------- 1 | # 입력 2 | N, M = map(int, input().split()) 3 | A = [ None ] * M 4 | B = [ None ] * M 5 | for i in range(M): 6 | A[i], B[i] = map(int, input().split()) 7 | 8 | # 인접리스트 작성 9 | G = [ list() for i in range(N + 1) ] # G[i]는 정점i와 인접한 정점 리스트 10 | for i in range(M): 11 | G[A[i]].append(B[i]) # 정점 A[i]와 인접한 정점으로 B[i]를 추가 12 | G[B[i]].append(A[i]) # 정점 B[i]와 인접한 정점으로 A[i]를 추가 13 | 14 | # 출력(len(G[i])은 정점i와 인접한 정점 리스트의 크기 = 차수) 15 | for i in range(1, N + 1): 16 | output = str(i) + ": {" 17 | for j in range(len(G[i])): 18 | if j >= 1: 19 | output += "," 20 | output += str(G[i][j]) # G[i][j]는 정점i와 인접한 정점 중 j+1번째인 것 21 | output += "}" 22 | print(output) 23 | -------------------------------------------------------------------------------- /본문/python/Code_4_05_3.py: -------------------------------------------------------------------------------- 1 | import queue 2 | 3 | # 입력 4 | N, M = map(int, input().split()) 5 | A = [ None ] * M 6 | B = [ None ] * M 7 | for i in range(M): 8 | A[i], B[i] = map(int, input().split()) 9 | 10 | # 인접리스트 작성 11 | G = [ list() for i in range(N + 1) ] 12 | for i in range(M): 13 | G[A[i]].append(B[i]) 14 | G[B[i]].append(A[i]) 15 | 16 | # 너비 우선 탐색 초기화(dist[i] = -1일 때 아직 도달하지 않은 흰색 정점으로 취급) 17 | dist = [ -1 ] * (N + 1) 18 | Q = queue.Queue() 19 | dist[1] = 0 20 | Q.put(1) # Q에 1을 추가(조작1) 21 | 22 | # 너비 우선 탐색 23 | while not Q.empty(): 24 | pos = Q.get() # Q의 앞을 확인하고, 이를 꺼냄(조작2, 3) 25 | for nex in G[pos]: 26 | if dist[nex] == -1: 27 | dist[nex] = dist[pos] + 1 28 | Q.put(nex) # Q에 nex를 추가(조작1) 29 | 30 | # 정점1에서 각 정점까지의 최단 거리를 출력 31 | for i in range(1, N + 1): 32 | print(dist[i]) 33 | -------------------------------------------------------------------------------- /본문/python/Code_4_06_1.py: -------------------------------------------------------------------------------- 1 | N = int(input()) 2 | 3 | a = [ None ] * (N + 1) 4 | a[1], a[2] = 1, 1 5 | for i in range(3, N + 1): 6 | a[i] = a[i - 1] + a[i - 2] 7 | 8 | print(a[N] % 1000000007) -------------------------------------------------------------------------------- /본문/python/Code_4_06_2.py: -------------------------------------------------------------------------------- 1 | N = int(input()) 2 | 3 | a = [ None ] * (N + 1) 4 | a[1], a[2] = 1, 1 5 | for i in range(3, N + 1): 6 | a[i] = (a[i - 1] + a[i - 2]) % 1000000007 7 | 8 | print(a[N] % 1000000007) -------------------------------------------------------------------------------- /본문/python/Code_4_06_3.py: -------------------------------------------------------------------------------- 1 | MOD = 1000000007 2 | 3 | a, b = map(int, input().split()) 4 | 5 | answer = 1 # a의 0은 1이므로, answer = 1로 초기화 6 | for i in range(b): 7 | answer = (answer * a) % MOD 8 | 9 | print(answer) 10 | -------------------------------------------------------------------------------- /본문/python/Code_4_06_4.py: -------------------------------------------------------------------------------- 1 | # 반복제곱법(p에는 a**1, a**2, a**4, a**8, ...가 할당됨) 2 | def modpow(a, b, m): 3 | p = a 4 | answer = 1 5 | for i in range(30): 6 | if (b & (1 << i)) != 0: 7 | answer = (answer * p) % m 8 | p = (p * p) % m 9 | return answer 10 | 11 | MOD = 1000000007 12 | 13 | a, b = map(int, input().split()) 14 | print(modpow(a, b, MOD)) 15 | 16 | # 추가 17 | # 참고적으로 Python에는 "a**b"를 m으로 나눈 나머지"를 반복제곱법으로 구하는 함수가 18 | # 표준 라이브러리에 pow(a, b, m)로 구현되어 있습니다. 19 | # 이와 관련된 내용은 Code_4_06_4_extra.py를 참고해주세요. 20 | -------------------------------------------------------------------------------- /본문/python/Code_4_06_4_extra.py: -------------------------------------------------------------------------------- 1 | # pow(a, b, m)는 a**b을 m로 나눈 나머지를 리턴하는 함수 2 | a, b = map(int, input().split()) 3 | print(pow(a, b, 1000000007)) 4 | -------------------------------------------------------------------------------- /본문/python/Code_4_06_5.py: -------------------------------------------------------------------------------- 1 | # 반복제곱법(p에는 a**1, a**2, a**4, a**8, ...가 할당됨) 2 | def modpow(a, b, m): 3 | p = a 4 | answer = 1 5 | for i in range(30): 6 | if (b & (1 << i)) != 0: 7 | answer = (answer * p) % m 8 | p = (p * p) % m 9 | return answer 10 | 11 | # division(a, b, m)은 a÷b mod m를 리턴하는 함수 12 | def division(a, b, m): 13 | return (a * modpow(b, m - 2, m)) % m 14 | 15 | MOD = 1000000007 16 | 17 | # 입력 18 | X, Y = map(int, input().split()) 19 | 20 | # 이항계수의 분자와 분모 구하기(과정1 / 과정2) 21 | bunja, bunmo = 1, 1 22 | for i in range(1, X + Y + 1): 23 | bunja = (bunja * i) % MOD 24 | for i in range(1, X + 1): 25 | bunmo = (bunmo * i) % MOD 26 | for i in range(1, Y + 1): 27 | bunmo = (bunmo * i) % MOD 28 | 29 | # 답 구하기(과정3) 30 | print(division(bunja, bunmo, MOD)) 31 | -------------------------------------------------------------------------------- /본문/python/Code_5_02_1.py: -------------------------------------------------------------------------------- 1 | # 주의: 다음 프로그램은 N >= 1에서 정상적으로 동작합니다. 2 | # N=0일 때는 답이 "1"이므로, 조건 분기를 따로 나누어서 출력하면 됩니다. 3 | 4 | N = int(input()) 5 | if N % 4 == 1: 6 | print(2) 7 | if N % 4 == 2: 8 | print(4) 9 | if N % 4 == 3: 10 | print(8) 11 | if N % 4 == 0: 12 | print(6) 13 | -------------------------------------------------------------------------------- /본문/python/Code_5_02_2.py: -------------------------------------------------------------------------------- 1 | N = int(input()) 2 | if N % 4 == 0: 3 | print("Second") 4 | else: 5 | print("First") 6 | -------------------------------------------------------------------------------- /본문/python/Code_5_03_1.py: -------------------------------------------------------------------------------- 1 | N = int(input()) 2 | if N % 2 == 0: 3 | print("Yes") 4 | else: 5 | print("No") 6 | -------------------------------------------------------------------------------- /본문/python/Code_5_03_2.py: -------------------------------------------------------------------------------- 1 | # 입력 → 배열 요소의 총합 S를 구함 2 | N, K = map(int, input().split()) 3 | A = list(map(int, input().split())) 4 | S = sum(A) 5 | 6 | # 답 출력 7 | if S % 2 != K % 2: 8 | print("No") 9 | elif S > K: 10 | print("No") 11 | else: 12 | print("Yes") 13 | -------------------------------------------------------------------------------- /본문/python/Code_5_04_1.py: -------------------------------------------------------------------------------- 1 | # 입력 2 | N, K = map(int, input().split()) 3 | 4 | # 사건B의 개수를 yeosagun(여사건) 변수로 셉니다. 5 | yeosagun = 0 6 | for a in range(1, N + 1): 7 | l = max(1, a - (K - 1)) # b, c의 탐색 범위 하한 l 8 | r = min(N, a + (K - 1)) # b, c의 탐색 범위 상한 r 9 | for b in range(l, r + 1): 10 | for c in range(l, r + 1): 11 | if abs(b - c) <= K - 1: 12 | yeosagun += 1 13 | 14 | # 답 출력 15 | print(N ** 3 - yeosagun) 16 | -------------------------------------------------------------------------------- /본문/python/Code_5_05_1.py: -------------------------------------------------------------------------------- 1 | a, b, c, d = map(int, input().split()) 2 | print(max(a * c, a * d, b * c, b * d)) 3 | -------------------------------------------------------------------------------- /본문/python/Code_5_06_1.py: -------------------------------------------------------------------------------- 1 | # 작은 문제 t를 푸는 함수 2 | def small_problem(A, B, t): 3 | cl = (A + t - 1) // t # A÷t의 소수점 아래를 자릅니다. 4 | cr = B // t # B÷t의 소수점 아래를 자릅니다. 5 | if cr - cl >= 1: 6 | return True 7 | else: 8 | return False 9 | 10 | A, B = map(int, input().split()) 11 | for i in range(1, B + 1): 12 | if small_problem(A, B, i) == True: 13 | answer = i 14 | print(answer) 15 | -------------------------------------------------------------------------------- /본문/python/Code_5_07_1.py: -------------------------------------------------------------------------------- 1 | # 입력 2 | N = int(input()) 3 | A = list(map(int, input().split())) 4 | 5 | # 답 구하기 6 | answer = 0 7 | for i in range(N): 8 | # i = 0부터 시작하므로, "-N + 2 * i - 1"가 아니라, "-N + 2 * i + 1"를 곱했음 9 | answer += A[i] * (-N + 2 * i + 1) 10 | 11 | # 답 출력 12 | print(answer) 13 | -------------------------------------------------------------------------------- /본문/python/Code_5_09_1.py: -------------------------------------------------------------------------------- 1 | N = int(input()) 2 | 3 | answer = 0 4 | 5 | # 10000원 이상이라면 10000원으로 지불할 수 있는 만큼 지불 6 | while N >= 10000: 7 | N -= 10000 8 | answer += 1 9 | 10 | # 5000원 이상이라면 5000원으로 지불할 수 있는 만큼 지불 11 | while N >= 5000: 12 | N -= 5000 13 | answer += 1 14 | 15 | # 남은 금액은 1000원으로 지불 16 | while N >= 1: 17 | N -= 1000 18 | answer += 1 19 | 20 | print(answer) 21 | -------------------------------------------------------------------------------- /본문/python/Code_5_09_2.py: -------------------------------------------------------------------------------- 1 | # 입력 2 | N = int(input()) 3 | L = [ None ] * N 4 | R = [ None ] * N 5 | for i in range(N): 6 | L[i], R[i] = map(int, input().split()) 7 | 8 | # 영화 선택 시뮬레이션 9 | # 볼 수 있는 영화 중 종료 시간 최솟값 min_endtime은 일단 1000000(INF로 설정)처럼 나올 수 없는 값으로 설정합니다. 10 | INF = 1000000 11 | current_time = 0 # current_time는 현재 시각(직전에 보았던 영화의 종료 시각) 12 | answer = 0 13 | while True: 14 | min_endtime = INF 15 | for i in range(N): 16 | if L[i] >= current_time: 17 | min_endtime = min(min_endtime, R[i]) 18 | if min_endtime == INF: 19 | break 20 | current_time = min_endtime 21 | answer += 1 22 | 23 | # 답 출력 24 | print(answer) 25 | -------------------------------------------------------------------------------- /본문/python/Code_5_10_1.py: -------------------------------------------------------------------------------- 1 | import math 2 | 3 | a, b, c = map(int, input().split()) 4 | 5 | if math.sqrt(a) + math.sqrt(b) < math.sqrt(c): 6 | print("Yes") 7 | else: 8 | print("No") 9 | -------------------------------------------------------------------------------- /본문/python/Code_5_10_2.py: -------------------------------------------------------------------------------- 1 | a, b, c = map(int, input().split()) 2 | if c - a - b < 0: 3 | print("No") 4 | elif 4 * a * b < (c - a - b) * (c - a - b): 5 | print("Yes") 6 | else: 7 | print("No") 8 | -------------------------------------------------------------------------------- /본문/python/Code_5_10_3.py: -------------------------------------------------------------------------------- 1 | # 입력 2 | N, X, Y = map(int, input().split()) 3 | 4 | # 4개의 정수 (a, b, c, d)를 전체 탐색 5 | flag = False 6 | for a in range(1, N + 1): 7 | for b in range(a, N + 1): 8 | for c in range(b, N + 1): 9 | for d in range(c, N + 1): 10 | if a + b + c + d == X and a * b * c * d == Y: 11 | flag = True # 답을 찾았다면 flag를 True로 변경합니다. 12 | 13 | # 답 출력 14 | if flag == True: 15 | print("Yes") 16 | else: 17 | print("No") 18 | 19 | # 파이썬 등의 실행 속도가 느린 프로그래밍 언어는 {N+3}_C_4가지 탐색하는 방법으로도 20 | # 실행 시간 제한을 맞추지 못할 수 있습니다. 21 | # 따라서 O(N)으로 푸는 방법을 활용해야 할 수 있습니다. 22 | # 이와 관련된 내용은 Code_5_10_3_extra.py를 참고해 주세요. -------------------------------------------------------------------------------- /본문/python/Code_5_10_3_extra.py: -------------------------------------------------------------------------------- 1 | N, X, Y = map(int, input().split()) 2 | 3 | if Y > N ** 4: 4 | print("No") 5 | else: 6 | d = [] 7 | for i in range(1, N + 1): 8 | if Y % i == 0: 9 | d.append(i) 10 | divcnt = len(d) 11 | flag = False 12 | for i in range(0, divcnt): 13 | for j in range(i, divcnt): 14 | for k in range(j, divcnt): 15 | for l in range(k, divcnt): 16 | if d[i] + d[j] + d[k] + d[l] == X and d[i] * d[j] * d[k] * d[l] == Y: 17 | flag = True 18 | if flag == True: 19 | print("Yes") 20 | else: 21 | print("No") 22 | 23 | # 이 프로그램의 복잡도는 O(N)입니다. 24 | # 이유를 꼭 생각해보세요. 25 | -------------------------------------------------------------------------------- /본문/python/Code_5_10_4.py: -------------------------------------------------------------------------------- 1 | # 입력 2 | N = int(input()) 3 | S = input() 4 | 5 | # (의 수에서 )의 수를 뺀 값을 depth에 할당합니다. 6 | # 중간에 depth가 음수가 된다면, 곧바로 답이 No입니다. 7 | depth = 0 8 | flag = True 9 | for i in range(N): 10 | if S[i] == '(': 11 | depth += 1 12 | if S[i] == ')': 13 | depth -= 1 14 | if depth < 0: 15 | flag = False 16 | 17 | # 마지막으로 depth = 0[(와 )의 수가 같음]을 추가적으로 판정합니다. 18 | if flag == True and depth == 0: 19 | print("Yes") 20 | else: 21 | print("No") 22 | --------------------------------------------------------------------------------