[백준 2018] 수들의 합 5(투 포인터)
- 💾 알고리즘/LeetCode_백준
- 2023. 5. 17. 17:19
📝 문제
https://www.acmicpc.net/problem/2018
💻 코드
package org.kyhslam.DoItAlgorithm.ch02;
import java.util.Scanner;
public class p2018 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
int startIndex = 1;
int endIndex = 1;
int count = 1;
int sum = 1;
while (endIndex != N) {
if (sum == N) {
endIndex++;
count++;
sum = sum + endIndex;
} else if (sum > N) {
sum = sum - startIndex;
startIndex++;
} else if (sum < N) {
endIndex++;
sum = sum + endIndex;
}
}
System.out.println("count = " + count);
}
}
'💾 알고리즘 > LeetCode_백준' 카테고리의 다른 글
[백준 1253] 좋은 수 구하기(투 포인터) (0) | 2023.05.18 |
---|---|
[백준 1940] 주몽의 명령(투 포인터) (0) | 2023.05.18 |
[백준 10986] 나머지합 구하기 (구간 합) (0) | 2023.05.17 |
[백준 17298] ATM (삽입정렬 사용) (0) | 2022.11.01 |
[백준 1377] 버블 소트 (0) | 2022.11.01 |