코딩하는딩굴 2023. 12. 12. 10:49

코딩도장

Multiples of 3 and 5

public static void main(String[] args) {
    ArrayList<Integer> list = new ArrayList<>();

    for (int i = 1; i < 1000; i++) {
        if (i % 3 == 0){
            list.add(i);
        } else if (i % 5 == 0) {
            list.add(i);
        }
    }
    int sum = 0;
    for (int i : list) {
        sum += i;
    }
    System.out.println(sum);
}

 

이렇게 풀었는데 그냥 i 값을 합하면 되는 것을..

 

탭을 공백문자로 바꾸기

public static void main(String[] args) {
    String s = "\t asdasdasd";
    s = s.replace("\t", "    ");
    System.out.println(s);
}

 

문자열을 제거한 뒤 숫자만 반환

public static void main(String[] args) {
    String s = "1w627r00o00p00";
    String answer = "";
    for (int i = 0; i < s.length(); i++) {
        if (Character.isDigit(s.charAt(i))) {
            answer += s.charAt(i);
        }
    }
    System.out.println(answer);
}

 

2진법으로 자연수 나타내기

public static void main(String[] args) {
    int temp = 253;
    String binary = "";
    String answer = "";
    while (temp > 0) {
        binary += temp % 2;
        temp /= 2;
    }
    for (int i = binary.length() - 1; i >= 0; i--) {
        answer += binary.charAt(i);
    }
    System.out.println(binary); // 10111111
    System.out.println(answer); // 11111101
    System.out.println(Integer.toBinaryString(253)); // 11111101
}

 

ASCII Art N

public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    System.out.println("홀수 입력");
    int input = sc.nextInt();

    for (int i = 0; i < input; i++) {
        System.out.print("N"); // 첫 글자 출력
        if (input == 1) { // input 이 1 이면 나머지 명령 스킵
            continue;
        }
        for (int j = i - 1; j > 0; j--) { // N 출력 후 공백 칸의 개수
            System.out.print(" ");
        }
        if (i == 0) { // 두번째 N 출력 조건, i 가 0 이면 N 출력하지 않음
            //System.out.print("");
        } else if(i != input - 1) { // i 가 input - 1 이면 N 을 출력하지 않음, 마지막 줄 출력 X
            System.out.print("N");
        }
        for (int j = 0; j < input - 2 - i; j++) { // 두번 째 N 출력 후 공백 갯수
            System.out.print(" ");
        }
        System.out.print("N"); // 마지막 N 출력
        System.out.println(); // 줄바꿈
    }
}

 

Stack Over Flow 에서 간단하게 만들어 준 코드

public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    System.out.println("Type an odd number:");
    int input = sc.nextInt();

    if (input % 2 == 0) {
        System.out.println("Please enter an odd number.");
        return;
    }

    for (int i = 0; i < input; i++) {
        for (int j = 0; j < input; j++) {
            if (j == i || j == 0 || j == input - 1) {
                System.out.print("N");
            } else {
                System.out.print(" ");
            }
        }
        System.out.println();
    }
}

 

배열로 진행해보기

public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    System.out.println("홀수를 입력해주세요");
    int input = sc.nextInt();
    
    while (input % 2 == 0) { // 홀수 체크
        System.out.println("다시 입력해주세요.");
        input = sc.nextInt();
    }

    char[][] chArr = new char[input][input];
    for (int i = 0; i < input; i++) {
        for (int j = 0; j < input; j++) {
            if (j == 0 || j == input - 1 || i == j) {
                chArr[i][j] ='N';
            } else {
                chArr[i][j] = ' ';
            }
        }
    }
    for (char[] c : chArr) {
        System.out.println(c);
    }
}

 

3이 나타나는 시간이 전부 합하면

public static void main(String[] args) {
    int hour = 0;
    int min = 0;
    int time = 0;
    while (true) {
        if (min == 60) {
            min -= 60;
            hour++;
        }
        if (hour == 24) {
            break;
        }
        if (min / 10 == 3 || min % 10 == 3 || hour % 10 == 3) {
            time++;
        }
        min++;
    }
    System.out.println(time * 60);
}