View
[BJ] 2003λ²: μλ€μ ν©2 (Java)
μ± μ½λ κ°μ 2022. 8. 30. 21:44λ°μν
λ¬Έμ
Nκ°μ μλ‘ λ μμ΄ A[1], A[2], β¦, A[N] μ΄ μλ€. μ΄ μμ΄μ iλ²μ§Έ μλΆν° jλ²μ§Έ μκΉμ§μ ν© A[i] + A[i+1] + β¦ + A[j-1] + A[j]κ° Mμ΄ λλ κ²½μ°μ μλ₯Ό ꡬνλ νλ‘κ·Έλ¨μ μμ±νμμ€.
μ λ ₯
첫째 μ€μ N(1 β€ N β€ 10,000), M(1 β€ M β€ 300,000,000)μ΄ μ£Όμ΄μ§λ€. λ€μ μ€μλ A[1], A[2], β¦, A[N]μ΄ κ³΅λ°±μΌλ‘ λΆλ¦¬λμ΄ μ£Όμ΄μ§λ€. κ°κ°μ A[x]λ 30,000μ λμ§ μλ μμ°μμ΄λ€.
μΆλ ₯
첫째 μ€μ κ²½μ°μ μλ₯Ό μΆλ ₯νλ€.
μμ μ λ ₯ 1
4 2
1 1 1 1
μμ μΆλ ₯ 1
3
π€¦ββοΈ My Solution
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
static int N, M, total;
static int arr[];
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
N = Integer.parseInt(st.nextToken());
M = Integer.parseInt(st.nextToken());
arr = new int[N];
st = new StringTokenizer(br.readLine());
for (int i = 0; i < N; i++) {
arr[i] = Integer.parseInt(st.nextToken());
}
total = 0;
sum();
System.out.println(total);
}
static public void sum() {
boolean t = true;
while (t) {
for (int i = 0; i < N; i++) {
if (arr[i] == M) {
total++;
continue;
} else {
int sum2 = 0;
sum2 = arr[i];
for (int j = i + 1; j < N; j++) {
sum2 += arr[j];
if (sum2 > M) {
sum2 = 0;
break;
}
if (sum2 == M) {
total++;
sum2 = 0;
break;
}
}
}
}
t = false;
}
}
}
λ°μν
'μκ³ λ¦¬μ¦ > μ½λ©ν μ€νΈ' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[SWEA] 3124: μ΅μ μ€ν¨λ νΈλ¦¬(Java) (0) | 2022.08.30 |
---|---|
[BJ] 1182λ²: λΆλΆμμ΄μ ν© (0) | 2022.08.30 |
[BJ]10974λ²: λͺ¨λ μμ΄(Java) (0) | 2022.08.30 |
[BJ] 1987λ²: μνλ²³(Java) (0) | 2022.08.30 |
[BJ] 1206: DFSμ BFS(Java) (0) | 2022.08.30 |
reply