๐ŸŽฏ Coding Test

๋ฐฑ์ค€ 2217๋ฒˆ : ๋กœํ”„ (์‹œ๊ฐ„์ดˆ๊ณผ ํ•ด๊ฒฐ๊ณผ์ •)

์—ฐ_์šฐ๋ฆฌ 2021. 10. 25. 03:35
๋ฐ˜์‘ํ˜•


๋ฌธ์ œ ์ดํ•ดํ•˜๊ธฐ

(์˜ˆ์‹œ๊ฐ€ ํ•˜๋‚˜๋ฐ–์— ์—†์–ด์„œ ์ดํ•ดํ•˜๊ธฐ ์–ด๋ ค์› ๋‹ค..)

์„œ๋กœ ๋‹ค๋ฅธ ์ค‘๋Ÿ‰์˜ ๋กœํ”„๋Š” 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();
    }
}

 

์•„.. ์ •๋ ฌ์‹œ๊ฐ„์ด ๋ฌธ์ œ์˜€๋‹ค!

์ •๋ ฌ๋ฐฉ๋ฒ•์˜ ์‹œ๊ฐ„๋ณต์žก๋„๋ฅผ ์ฐพ์•„๋ณด๊ฒŒ ํ•ด์ค€ ๋ฌธ์ œ์˜€๋‹ค

๋‚˜์ค‘์— ์ •๋ฆฌํ•ด๋ด์•ผ๊ฒ ๋‹น

๋ฐ˜์‘ํ˜•
  • ๋„ค์ด๋ฒ„ ๋ธ”๋Ÿฌ๊ทธ ๊ณต์œ ํ•˜๊ธฐ
  • ํŽ˜์ด์Šค๋ถ ๊ณต์œ ํ•˜๊ธฐ
  • ํŠธ์œ„ํ„ฐ ๊ณต์œ ํ•˜๊ธฐ
  • ๊ตฌ๊ธ€ ํ”Œ๋Ÿฌ์Šค ๊ณต์œ ํ•˜๊ธฐ
  • ์นด์นด์˜คํ†ก ๊ณต์œ ํ•˜๊ธฐ