본문 바로가기

프로그래밍/자바

[자바] 정올 189 ~ 194 && 215, 216, 237 : 문자열2 - 형성평가 1 ~ 9

728x90
반응형
SMALL

형성평가 1 : 189

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String str = sc.nextLine();
        String[] arr = str.split(" ");
        for(int i = arr.length-1; i >=0; i--) System.out.println(arr[i]);

    }
}

형성평가 2 : 190

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        String[] arr = {"flower", "rose", "lily", "daffodil", "azalea" };
        Scanner sc = new Scanner(System.in);
        char ch = sc.next().charAt(0);
        int count = 0;
        for(String i : arr){
            if((ch == i.charAt(1)) || (ch == i.charAt(2))){
                System.out.println(i);
                count++;
            }

        }
        System.out.println(count);
    }
}

형성평가 3 : 191

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String[] arr = new String[50];
        int i;
        String str;
        for(i = 0; i < 50; i++){
            str = sc.nextLine();
            if(str.equals("0")) break;
            arr[i] = str;
        }
        System.out.println(i);
        for(int j = 0; j < i; j++) if( (j+1) % 2 != 0) System.out.println(arr[j]);
    }

}

형성평가 4 : 192

import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        sc.nextLine();
        String[] arr = new String[n];
        for(int i =  0; i < n; i++) arr[i] = sc.nextLine().trim();
        Arrays.sort(arr);
        for(int i =  0; i < n; i++) System.out.println(arr[i]);
    }
}

형성평가 5 : 193

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String[] arr = new String[5];
        for(int i =0; i < 5; i ++) arr[i] = sc.nextLine();
        String s1 = sc.nextLine(), s2 = sc.nextLine();
        int count = 0;
        for(String i : arr){
            if(i.contains(s1) || i.contains(s2)){
                System.out.println(i);
                count++;
            }
        }
        if(count == 0) System.out.println("none");

    }
}

형성평가6 : 194

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String s1 = sc.next().trim(), s2 = sc.next().trim();
        int i = sc.nextInt();
        String str = s1 + s2;
        System.out.println(str);
        if(s1.length() >= i) System.out.println((s1.substring(0,i)) + (s2.substring(i)));
        else System.out.println((s1+s2).substring(0,i) + (s2.length() > i ? s2.substring(i) : ""));
    }

}

형성평가 7 : 215

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        String input1 = scanner.next();
        String input2 = scanner.next();

        int number1 = parseInteger(input1);
        int number2 = parseInteger(input2);

        int result = number1 * number2;
        System.out.println(result);

        scanner.close();
    }

    private static int parseInteger(String str) {
        StringBuilder numStr = new StringBuilder();

        for (char c : str.toCharArray()) {
            if (Character.isDigit(c)) {
                numStr.append(c);
            } else {
                break; // 정수로 변환 가능한 부분이 끝났을 때 종료
            }
        }

        if (numStr.length() == 0) {
            // 정수로 변환 가능한 부분이 없을 경우 0을 반환
            return 0;
        } else {
            return Integer.parseInt(numStr.toString());
        }
    }
}

형성평가 8 : 216

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String str = "";
        String[] arr = new String[100];
        char[] arr_char;
        int i = 0;
        while(true){
            str = sc.nextLine().trim();
            if(str.equals("END")) break;
             arr[i] = str;
             i++;
        }

        for(int j = 0; j < i; j++){
            arr_char = arr[j].toCharArray();
            for(int k = arr_char.length-1; k >=0; k--) System.out.print(arr_char[k]);
            System.out.println();
        }

    }
}

형성평가 9 : 237

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String str = sc.nextLine().trim();
        String[] arr = str.split("\\s+");
        str = arr[0].concat(arr[1]);
        double format = Double.valueOf(str);
        String from = String.format("%.3f", format);
        String answer = from + arr[2];
        int half = answer.length()/2;

        if(answer.length() %2 == 0){
            System.out.println(answer.substring(0, half));
            System.out.println(answer.substring(half, answer.length()));
        }else{
            System.out.println(answer.substring(0, half+1));
            System.out.println(answer.substring(half+1, answer.length()));
        }


    }
}
728x90
반응형
LIST