[JAVA | 백준 15596] 정수 N개의 합
· 테크· Java
Java
백준 15596번 문항 www.acmicpc.net/problem/15596
문제
정수 n개가 주어졌을 때, n개의 합을 구하는 함수를 작성하시오.
Java: long sum(int[] a); (클래스 이름: Test)
-
a: 합을 구해야 하는 정수 n개가 저장되어 있는 배열 (0 ≤ a[i] ≤ 1,000,000, 1 ≤ n ≤ 3,000,000)
-
리턴값: a에 포함되어 있는 정수 n개의 합
풀이
for문으로 정수 n 까지의 수를 int 배열에 대입하고 대입된 배열을 한 개씩 더하는 메서드를 만든다.
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
public class function_15596 {
int[] a = new int[1000001];
public static long sum(int[] a) {
long ans = 0, n=0;
if(1<=n&&n<=3000000) {
for (int i=1; i<=n; i++) {
a[i-0]=i;
}
}
for (int i=0; i<a.length; i++) {
ans += a[i];
}
return ans;
}
}
|
cs |
Comments
No comments yet. Be the first!
319 posts in 테크
- 368Supabase 프로젝트 복사하기 (Restore to a New Project)NEW
- 341Migrating from Permanent Access Tokens to Token Exchange — Why Order Matters
- 326Startup & Product Glossary: Terms Every Solo Founder Should Know
- 325Context Management — How I Do It Now
- 324Claude Code Routines vs Cowork Schedule — What's the Difference?
1–5 / 319