반응형
나의 풀이>
class Solution {
public long solution(int a, int b) {
long answer = 0;
if(a>b){
int tem=a;
a=b;
b=tem;
}
for(int i=a;i<=b;i++){
answer+=i;
}
return answer;
}
}
다른 풀이1>
class Solution {
public long solution(int a, int b) {
long answer = 0;
for(int i=((a>b)?b:a);i<=((a>b)?a:b);i++){
answer+=i;
}
return answer;
}
}
다른 풀이2>
class Solution {
public long solution(int a, int b) {
long answer = 0;
for(int i=Math.min(a,b);i<=Math.max(a,b);i++){
answer+=i;
}
return answer;
}
}
반응형
'알고리즘 > 문제풀이' 카테고리의 다른 글
[JAVA]CodeUp - 1031 ~ 1037 (0) | 2020.08.25 |
---|---|
[JAVA]CodeUp - 1027.년월일 입력 받아 형식 바꿔 출력하기 (0) | 2020.08.25 |
[JAVA]CodeUp - 1025.정수 1개 입력받아 나누어 출력하기 (0) | 2020.08.25 |
[JAVA]프로그래머스 - 가운데 글자 가져오기 (0) | 2020.08.24 |
[JAVA]CodeUp-1019.연월일 입력받아 그대로 출력하기 (0) | 2020.08.17 |