
📋문제 정리
- 입력받은 N이 연속되는 자연수의 합으로 표현되는지 경우의수를 구하라
🎯풀이
- 연속되는 자연수의 합이니까 N이하의 숫자로만 이루어져있다.
- N이하의 숫자를 사용하고 두개의 포인터 start,end를 0에 위치한다.
- 포인터 사이의 숫자를 더하여 N보다 작으면 end 크면 start를 증가시킨다.
- N이랑 같다면 cnt와 end를 증가시킨다. start가 N과 같아지면 끝낸다.
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.StringTokenizer;
import java.util.Arrays;
public class Main {
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static StringTokenizer st;
static StringBuilder sb = new StringBuilder();
public static void main(String[] args) throws IOException {
st = new StringTokenizer(br.readLine());
int n = Integer.parseInt(st.nextToken());
int start = 0;
int end = 0;
int sum = 0;
int cnt = 0;
while(start!=n){
if(sum<n){
end++;
sum+=end;
}
else if(sum>n){
sum -= start;
start++;
}
else{
cnt++;
sum+=end++;
}
}
if(n!=1)
sb.append(cnt+1);
else
sb.append(cnt);
System.out.println(sb);
br.close();
}
}