728x90
문제
입력된 숫자를 나열하여 가장 최소시간 구하기
문제 링크 : https://www.acmicpc.net/problem/11399
원리
첫 번째 * N
두 번째 * N-1
세 번째 * N-2
...
N번째 * 1
이를 모두 합한 값이 최소가 되도록 구하시오
풀이방법
1. ArrayList로 입력을 받는다.
2. ArrayList를 sort한다
3. ArrayList의 size만큼 반복하며,
1번째 * ArrayList.size()
+ 2번째 * (ArrayList.size() -1)
...
+ N번째 * 1
하여 합산 후 출력
나의 코드
import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int T = Integer.parseInt(br.readLine());
StringTokenizer st = new StringTokenizer(br.readLine(), " ");
ArrayList<Integer> list = new ArrayList<>();
for(int i=0; i<T ; i++) list.add(Integer.parseInt(st.nextToken()));
Collections.sort(list);
int time = 0;
int num = 0;
for(int i = list.size(); i>0 ; i--) {
time += (list.get(num) * i);
num++;
}
System.out.println(time);
}
}
728x90
'알고리즘 저장소 (일반방식과 나만의 풀이) > JAVA' 카테고리의 다른 글
| [백준] no7576: 토마토 (0) | 2023.02.20 |
|---|---|
| [백준] no1931: 회의실 배정 (0) | 2023.02.20 |
| [백준] no1697: 숨바꼭질 (0) | 2023.02.19 |
| [백준] no5525: IOIOI (0) | 2023.02.17 |
| [백준] no16928: 뱀과 사다리 게임 (0) | 2023.02.16 |