티스토리 뷰
public class Q3 {
public static void main(String[] args) {
Random random = new Random();
int[] arr = new int[random.nextInt(10)];
for (int i = 0; i < arr.length; i ++) {
arr[i] = random.nextInt(100);
System.out.print(arr[i] + " ");
}
int sum = sumOf(arr);
System.out.println("\n배열 요소의 총 합계 : " + sum);
}
static int sumOf(int[] arr) {
int sum = 0;
for (int i = 0; i < arr.length; i++) {
sum += arr[i];
}
return sum;
}
}
(1)
class SumOf_02_03 {
static int sumOf(int[] a) {
int sum = 0;
for (int i = 0; i < a.length; i++)
sum += a[i];
return (sum);
}
public static void main(String[] args) {
Scanner stdIn = new Scanner(System.in);
System.out.print("요솟수는:");
int num = stdIn.nextInt(); // 요솟수
int[] a = new int[num]; // 길이가 num인 배열
for (int i = 0; i < num; i++) {
System.out.print("a[" + i + "] : ");
a[i] = stdIn.nextInt();
}
System.out.println("전 요소의 합계는 " + sumOf(a) + "입니다.");
}
}
(2)
- 그냥 합계 반복문으로 구하는거라 쉬웠음
※참조
Do it! 자료구조와 함께 배우는 알고리즘 입문
'PS > etc' 카테고리의 다른 글
chapter2) 소수 구하기 (0) | 2020.07.09 |
---|---|
chapter2) 10진수 변환 (0) | 2020.07.08 |
chapter2) 배열 역순 정렬 (0) | 2020.07.07 |
chapter1) 숫자 피라미드 찍기 (0) | 2020.07.07 |
chapter1) 피라미드 별 찍기 (0) | 2020.07.07 |
링크
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
TAG
- 모던자바스크립트
- js array
- ㅇㄷㅇㅈ
- 킹수빈닷컴
- JS 딥다이브
- 프로그래머스
- BOJ
- 김영한 JPA
- http
- 패스트캠퍼스 컴퓨터공학 완주반
- 김영한 http
- REST API
- js promise
- 이펙티브자바 아이템60
- HTTP 완벽가이드
- 백준
- js api
- 프로그래머스 SQL
- 이펙티브자바 스터디
- JPA 연관관계 매핑
- GCP
- 가상 면접 사례로 배우는 대규모 시스템 설계 기초
- 이펙티브자바 아이템59
- dreamcoding
- Spring Security
- 백기선 스터디
- 드림코딩
- 이펙티브자바
- HTTP 완벽 가이드
- java
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | |||||
3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 | 29 | 30 |
31 |
글 보관함