티스토리 뷰
문제는 위와 같으며 ascending, desceding 변수를 선언하고 주어진 음계 배열에서 앞 음계가 뒤 음계보다 작으면(오름차순) descending 을 false 로 크면(내림차순) ascending 을 false 로 만듭니다. 결과 출력 시 ascending, descending 중 true 인 값을 출력하고 둘다 false 인 경우 mixed 를 출력하도록 구현하였습니다.
파이썬 코드를 보면 다음과 같습니다.
from sys import stdin
inputs = list(map(int, stdin.readline().split()))
ascending = True
descending = True
for i in range(1, 8):
if inputs[i] > inputs[i - 1]:
descending = False
elif inputs[i] < inputs[i - 1]:
ascending = False
if ascending:
print('ascending')
elif descending:
print('descending')
else:
print('mixed')
자바 코드는 다음과 같습니다.
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] tmp = br.readLine().split(" ");
boolean ascending = true;
boolean descending = true;
for(int i = 1; i < tmp.length; i++) {
if(Integer.parseInt(tmp[i]) > Integer.parseInt(tmp[i - 1])) {
descending = false;
} else if(Integer.parseInt(tmp[i]) < Integer.parseInt(tmp[i - 1])) {
ascending = false;
}
}
if (ascending) {
System.out.println("ascending");
} else if (descending) {
System.out.println("descending");
} else {
System.out.println("mixed");
}
}
}
'알고리즘' 카테고리의 다른 글
[알고리즘 / 백준] 10930 - SHA-256 (0) | 2020.11.08 |
---|---|
[알고리즘 / 백준] 5397 - 키로거 (0) | 2020.11.08 |
[알고리즘 / 백준] 1966 - 프린터 큐 (0) | 2020.11.08 |
[알고리즘 / 백준] 2798 - 블랙잭 (0) | 2020.11.08 |
[알고리즘/백준] 4673 - 셀프 넘버 (0) | 2020.01.14 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- BFS
- 수학
- spring
- 소수
- ionic
- Algorithm
- array
- programmers
- SWIFT
- 조합
- 순열
- CodePipeline
- AWS
- java
- EC2
- permutation
- search
- Dynamic Programming
- cloudfront
- string
- map
- sort
- CodeCommit
- ECR
- DFS
- Combination
- 에라토스테네스의 체
- 프로그래머스
- Baekjoon
- CodeDeploy
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함