๋ฌธ์ ์ดํดํ๊ธฐ
(์์๊ฐ ํ๋๋ฐ์ ์์ด์ ์ดํดํ๊ธฐ ์ด๋ ค์ ๋ค..)
์๋ก ๋ค๋ฅธ ์ค๋์ ๋กํ๋ 1๊ฐ๋ง ์กด์ฌํ๋ค. ํธํ๊ฒ kg์ผ๋ก ์๊ฐํด๋ณด์
10kg๋งํผ ๋ค ์ ์๋ ๋กํ๊ฐ 1๊ฐ, 15kg๋งํผ ๋ค ์ ์๋ ๋กํ๊ฐ 1๊ฐ์ด๋ค.
10kg๋กํ๋ 8kg์ ๋ค ์ ์์ง๋ง 12kg์ ๋ค ์ ์๋ค.
10kg๊ณผ 15kg์ ํจ๊ป ์ฌ์ฉํ ๋,
15kg๋กํ๋ 12kg์ ๋ค ์ ์์ง๋ง, 10kg๋กํ๋ 12kg์ ๋ค ์ ์๋ค!
โถ ๊ฒฐ๊ตญ์ 10kg 2๊ฐ๊น์ง๋ง ๋ค ์ ์๋ค๋ ๊ฒ์ด๋ค.
์๋ฅผ ํ๋ ๋ ๋ค์ด๋ณด์
[8, 12, 5, 20, 4, 30] ์ด 6๊ฐ ๋กํ๊ฐ ์๋ค.
๊ฐ์ฅ ๋์ ์ค๋์ ๋ค๊ธฐ์ํด์ ๊ฐ์ฅ ๋์ ๋กํ๋ถํฐ ์ ํํด์ผํ๋ค
๋ด๋ฆผ์ฐจ์ ์ ๋ ฌ : [30, 20, 12, 8, 5, 4]
30 ๋กํ ์ฌ์ฉ ์ ์ต๋์ค๋์ 30
30+20 ๋กํ ์ฌ์ฉ ์ ์ต๋์ค๋์ 20+20 = 40 โญ์ต๋๊ฐ
30+20+12 ๋กํ ์ฌ์ฉ ์ ์ต๋์ค๋์ 12+12+12 = 36
30+20+12+8 ๋กํ ์ฌ์ฉ ์ ์ต๋์ค๋์ 8+8+8+8 = 32
30+20+12+8+5 ๋กํ ์ฌ์ฉ ์ ์ต๋์ค๋์ 5+5+5+5+5 = 25
30+20+12+8+5+4 ๋กํ ์ฌ์ฉ ์ ์ต๋์ค๋์ 4+4+4+4+4+4 = 26
โถ N๋กํ ์ฌ์ฉ ์ ์ต๋์ค๋์ N*์ ํํ์์
์ฝ๋
import java.util.Scanner;
class Rope {
int ropecount;
int[] ropes;
void scan_input(String inputed) {
InputStream in = new ByteArrayInputStream(inputed.getBytes());
System.setIn(in);
}
void scan() {
Scanner scan = new Scanner(System.in);
ropecount = scan.nextInt();
ropes = new int[ropecount];
for (int i = 0; i < ropes.length; i++) {
ropes[i] = scan.nextInt();
}
scan.close();
}
void process() {
//์ ๋ ฌ
int temp = 0;
for (int i = 1; i < ropes.length; i++) {
for (int j = 0; j < i; j++) {
if(ropes[j] > ropes[i]){
temp = ropes[i];
for (int k = i; k > j; k--) {
ropes[k] = ropes[k-1];
}
ropes[j] = temp;
}
}
}
//์ต๋๊ฐ ํ์ธ
int max = ropes[0] * (ropes.length);
for (int i = 1; i < ropes.length; i++) {
ropes[i] = ropes[i] * (ropes.length-i);
if(max < ropes[i]){
max = ropes[i];
}
}
System.out.println(max);
}
}
public class Main {
public static void main(String[] args) {
Rope r = new Rope();
r.scan();
r.process();
}
}
์ ์ฝ๋์ ๊ฒฐ๊ณผ๋
ํน์ Scanner๋๋ฌธ์ธ๊ฐ.. ํด์ ๋ค์ ํ์ด๋ดค๋ค
import java.io.*;
import java.util.Scanner;
import java.util.StringTokenizer;
class Rope {
int ropecount;
int[] ropes;
void scan_input(String inputed) {
InputStream in = new ByteArrayInputStream(inputed.getBytes());
System.setIn(in);
}
void scan() throws IOException {
// Scanner scan = new Scanner(System.in);
// ropecount = scan.nextInt();
// ropes = new int[ropecount];
// for (int i = 0; i < ropes.length; i++) {
// ropes[i] = scan.nextInt();
// }
// scan.close();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
ropecount = Integer.parseInt(st.nextToken());
ropes = new int[ropecount];
for (int i = 0; i < ropes.length; i++) {
st = new StringTokenizer(br.readLine());
ropes[i] = Integer.parseInt(st.nextToken());
}
}
void process() {
//์ ๋ ฌ
int temp = 0;
for (int i = 1; i < ropes.length; i++) {
for (int j = 0; j < i; j++) {
if(ropes[j] > ropes[i]){
temp = ropes[i];
for (int k = i; k > j; k--) {
ropes[k] = ropes[k-1];
}
ropes[j] = temp;
}
}
}
//์ต๋๊ฐ ํ์ธ
int max = ropes[0] * (ropes.length);
for (int i = 1; i < ropes.length; i++) {
ropes[i] = ropes[i] * (ropes.length-i);
if(max < ropes[i]){
max = ropes[i];
}
}
System.out.println(max);
}
}
public class Main {
public static void main(String[] args) {
Rope r = new Rope();
try {
r.scan();
} catch (IOException e) {
e.printStackTrace();
}
r.process();
}
}
๊ณผ์ฐ ๊ฒฐ๊ณผ๋?!
์ด์ ์์ํด๋ณผ์์๋๊ฑด ์ ๋ ฌ์๊ฐ ๋ฟ์ด๋ค....
์ ๋ ฌํ๋ ๊ณผ์ ์ด ๋ฌธ์ ์ ์๋๋ผ๊ณ ์๊ฐํ๋๋ฐ..?????
ํ์ธ์ ์ํด Scanner๋ก ๋ค์ ๋๋ ค๋ณด๊ฒ ๋ค
package BJ211024_2217;
import java.io.*;
import java.util.Arrays;
import java.util.Scanner;
import java.util.StringTokenizer;
class Rope {
int ropecount;
int[] ropes;
void scan_input(String inputed) {
InputStream in = new ByteArrayInputStream(inputed.getBytes());
System.setIn(in);
}
void scan() throws IOException {
Scanner scan = new Scanner(System.in);
ropecount = scan.nextInt();
ropes = new int[ropecount];
for (int i = 0; i < ropes.length; i++) {
ropes[i] = scan.nextInt();
}
scan.close();
// BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
// StringTokenizer st = new StringTokenizer(br.readLine());
//
// ropecount = Integer.parseInt(st.nextToken());
// ropes = new int[ropecount];
// for (int i = 0; i < ropes.length; i++) {
// st = new StringTokenizer(br.readLine());
// ropes[i] = Integer.parseInt(st.nextToken());
// }
}
void process() {
//์ ๋ ฌ
// int temp = 0;
// //System.out.println(Arrays.toString(ropes));
// for (int i = 1; i < ropes.length; i++) {
// for (int j = 0; j < i; j++) { //System.out.print("key="+ropes[i]+" vs ropes["+j+"]="+ropes[j]);
// if(ropes[j] > ropes[i]){ //System.out.print(" insert ");
// temp = ropes[i];
// for (int k = i; k > j; k--) {
// ropes[k] = ropes[k-1];
// }
// ropes[j] = temp;
// }
// //System.out.println();
// //System.out.println(Arrays.toString(ropes));
// }
// //System.out.println();
// }
Arrays.sort(ropes);
int max = ropes[0] * (ropes.length);
for (int i = 1; i < ropes.length; i++) {
ropes[i] = ropes[i] * (ropes.length-i);
if(max < ropes[i]){
max = ropes[i];
}
}
System.out.println(max);
}
}
public class Main {
public static void main(String[] args) {
Rope r = new Rope();
r.scan();
r.process();
}
}
์.. ์ ๋ ฌ์๊ฐ์ด ๋ฌธ์ ์๋ค!
์ ๋ ฌ๋ฐฉ๋ฒ์ ์๊ฐ๋ณต์ก๋๋ฅผ ์ฐพ์๋ณด๊ฒ ํด์ค ๋ฌธ์ ์๋ค
๋์ค์ ์ ๋ฆฌํด๋ด์ผ๊ฒ ๋น
'๐ฏ Coding Test' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
๋ฐฑ์ค 1026๋ฒ : ๋ณด๋ฌผ (0) | 2021.10.27 |
---|---|
๋ฐฑ์ค 1946๋ฒ : ์ ์ ์ฌ์ (0) | 2021.10.26 |
๋ฐฑ์ค 2839๋ฒ : ์คํ ๋ฐฐ๋ฌ (2) | 2021.10.23 |
๋ฐฑ์ค 19941๋ฒ : ํ๋ฒ๊ฑฐ ๋ถ๋ฐฐ (0) | 2021.10.21 |
๋ฐฑ์ค 9009๋ฒ : ํผ๋ณด๋์น (0) | 2021.10.20 |