728x90
주어진 N개의 수에서 가능한 두 수의 곱 중, 가장 큰 단조 증가 수를 찾는 문제이다.
먼저 가능한 두 수의 곱을 이중 for문으로 찾고, 그 수가 단조 증가하는지를 찾으면 된다.
단조 증가 여부를 찾기 위해 정수형 숫자를 String으로 바꿔주고, 각 자리수를 돌며 앞의 자리수가 뒤의 자리수보다 클 경우 false를 return하였다.
또한, 단조 증가하는 수가 없을 경우 -1을 출력하기 위해, result의 초기값을 -1로 설정하였다.
package SWEA0811;
import java.util.Scanner;
public class SWEA_6190_단조증가 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int T = sc.nextInt();
StringBuilder sb = new StringBuilder();
for (int t = 0; t < T; t++) {
int N = sc.nextInt();
int[] arr = new int[N];
for (int i = 0; i < N; i++)
arr[i] = sc.nextInt();
int result = -1;
for (int i = 0; i < N - 1; i++) {
for (int j = i + 1; j < N; j++) {
int tmp = arr[i] * arr[j];
if (tmp > result && isIncrease(tmp))
result = tmp;
}
}
sb.append("#" + (t + 1) + " " + result + "\n");
}
System.out.print(sb.toString());
}
static boolean isIncrease(int num) {
String str = Integer.toString(num);
if (num < 10)
return true;
for (int i = 0; i < str.length() - 1; i++) {
if (str.charAt(i) > str.charAt(i + 1))
return false;
}
return true;
}
}
728x90
'문제 풀이 > SWEA' 카테고리의 다른 글
[SWEA] 1210 - Ladder (자바/Java) (0) | 2022.08.11 |
---|---|
[SWEA] 5432 - 쇠막대기 자르기 (자바/Java) (0) | 2022.08.11 |
[SWEA] 1954 - 달팽이 숫자 (자바/Java) (0) | 2022.08.10 |
[SWEA] 1208 - Flattern (자바/Java) 배열, 우선순위 큐(힙) (0) | 2022.08.09 |
[SWEA] 4789 - 성공적인 공연 기획 (자바 / Java) (0) | 2022.08.09 |