티스토리 뷰
public class Q9 {
// 정수 a,b를 포함하여 그 사이의 모든 정수의 합 구하기
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("두 수를 입력하세요.");
System.out.print("a : ");
int a = scanner.nextInt();
System.out.print("b : ");
int b = scanner.nextInt();
int sum = sumof(a, b);
System.out.println("sum : " + sum);
}
static int sumof(int a, int b) {
int sum = 0;
int max = 0;
int min = 0;
if (a > b) {
max = a;
min = b;
} else {
max = b;
min = a;
}
for (int i = min; min <= max; min++) {
sum += min;
}
return sum;
}
}
class SumOf_01_09 {
static int sumof(int a, int b) {
int min; // a, b의 작은 쪽의 값
int max; // a, b의 큰 쪽의 값
if (a < b) {
min = a;
max = b;
} else {
min = b;
max = a;
}
int sum = 0; // 합
for (int i = min; i <= max; i++)
sum += i;
return (sum);
}
public static void main(String[] args) {
Scanner stdIn = new Scanner(System.in);
System.out.println("a와 b를 포함하여 그 사이의 모든 정수의 합을 구합니다.");
System.out.print("a의 값:");
int a = stdIn.nextInt();
System.out.print("b의 값:");
int b = stdIn.nextInt();
System.out.println("정수 a, b 사이의 모든 정수의 합은 " + sumof(a, b) + "입니다.");
}
}
※참조 Do it! 자료구조와 함께 배우는 알고리즘 입문 교재
'PS > etc' 카테고리의 다른 글
chapter1) 덧셈표 작성 (0) | 2020.07.06 |
---|---|
chapter1) 곱셈표 작성 (0) | 2020.07.06 |
chapter1) 양의 정수 자릿수 구하기 (0) | 2020.07.06 |
chapter1) 두 수의 차 구하기 (0) | 2020.07.06 |
chapter1) 가우스의 덧셈 (0) | 2020.07.06 |
링크
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
TAG
- js promise
- java
- 드림코딩
- HTTP 완벽가이드
- js array
- js api
- 백기선 스터디
- 모던자바스크립트
- http
- 킹수빈닷컴
- BOJ
- 이펙티브자바 스터디
- 이펙티브자바
- 프로그래머스
- JS 딥다이브
- 패스트캠퍼스 컴퓨터공학 완주반
- REST API
- 백준
- dreamcoding
- ㅇㄷㅇㅈ
- 가상 면접 사례로 배우는 대규모 시스템 설계 기초
- Spring Security
- 김영한 http
- 이펙티브자바 아이템59
- 프로그래머스 SQL
- HTTP 완벽 가이드
- JPA 연관관계 매핑
- 이펙티브자바 아이템60
- GCP
- 김영한 JPA
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함