문제


사전 구상

  • 자연수 n 이 주어지면 n을 2로 나눠서 나머지가 0이면 짝수 출력! 아니면 홀수 출력

package lv_0;

import java.util.Scanner;

public class oddeven {

	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();

		int result = n % 2;
		
		if (result == 0) {
			System.out.printf("%d is even", n);
		} else {
			System.out.printf("%d is odd", n);
		}

	}

}

 

결과


https://github.com/Beast-IT/Programmers.git

 

GitHub - Beast-IT/Programmers: 코딩테스트 연습!

코딩테스트 연습! Contribute to Beast-IT/Programmers development by creating an account on GitHub.

github.com

Github에 들어가시면 원본 코드 보실 수 있습니다.

✅  문제


✅ 사전 구상

  • System.in.read(); 를 사용해서 바이트 데이터를 읽어서 int값으로 저장.

  • 아스키코드값을 보면 A~Z(65~90), a~z(97~122) 대소문자 간의 32 차이가 나는 것을 볼 수 있음.
  • 조건문으로 데이터 값이 '65~90' 범위면 '+32'를 해주고 나머지 범위는 '-32'를 해주면 될꺼같음.

✅ 코드구현

 

package lv_0;

import java.io.IOException;

public class changetheupperandlowercase {

	public static void main(String[] args) throws IOException {

		int[] str = new int[7];

		for (int i = 0; i < 7; i++) {
			str[i] = System.in.read();
		}

		for (int j = 0; j < 7; j++) {
			
			if (str[j] >= 65 && 90 >= str[j]) {
				
				str[j] += 32;
				
			} else {
				
				str[j] -= 32;
				
			}
		}

		for (int k = 0; k < 7; k++) {
			System.out.write(str[k]);

		}

		System.out.flush();
	}

}

망했다... 값은 나오는데 문제를 잘 못 읽어서 문자로 받아왔다....문자열로 받아와서 다시 코드수정을 해야겠음... ❌ 


package lv_0;

import java.io.IOException;
import java.util.Scanner;

public class changetheupperandlowercase {

	public static void main(String[] args) throws IOException {

		Scanner sc = new Scanner(System.in);
		String str = sc.nextLine();

		for (int i = 0; i < str.length(); i++) {

			char one = str.charAt(i);// str에서 한문자씩 받아
			int a = one;// a에 ASCII 코드값 저장

			// A~Z -> 65 ~ 90
			// a~z -> 97 ~ 122
			
			//대소문자 변환 
			if (65 <= a && a <= 90) {
				a+=32;
			}else {
				a-=32;
			}
			System.out.write(a);//write를 이용해 코드값을 대소문자로 인코	
		}
		System.out.flush();


	}

}

🏋️ 위 코드로 수정완료!🏋️

 


https://github.com/Beast-IT/Programmers.git

 

GitHub - Beast-IT/Programmers: 코딩테스트 연습!

코딩테스트 연습! Contribute to Beast-IT/Programmers development by creating an account on GitHub.

github.com

Github에 들어가시면 원본 코드 보실 수 있습니다.

입출력 이해하기

1. 다음 코드를 작성하여 실행 한 후, 콘솔에 12 34 65를 입력해서 출력을 확인해보세요.

 

Scanner sc = new Scanner(System.in);

int input = sc.nextInt();

System.out.println(input);

 

2. 위의 입 출력 동작을

System.in.read, System.out.write, System.out.print, System.out.flush와

변수를 자유롭게 이용해서 똑같이 구현해보세요.

즉, 4개의 함수를 이용해서

12 34 65 를 입력하면, (1)과 똑같이 출력이 나오게 구현하세요.

반복문은 사용하지 말구요!

 


✅첫번째 방법

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int input = System.in.read();
int input2 = System.in.read();
int input3 = System.in.read();
int input4 = System.in.read();
int input5 = System.in.read();
int input6 = System.in.read();
int input7 = System.in.read();
int input8 = System.in.read();
 
System.out.write(input);
System.out.write(input2);
System.out.write(input3);
System.out.write(input4);
System.out.write(input5);
System.out.write(input6);
System.out.write(input7);
System.out.write(input8);
System.out.flush();
cs

 

✅결과

 


✅두번째 방법

1
2
3
4
5
6
7
8
9
System.out.write(System.in.read());//write는 byte코드를 입력받으면 시스템 기존값으로 변경해줌!
System.out.write(System.in.read());
System.out.write(System.in.read());
System.out.write(System.in.read());
System.out.write(System.in.read());
System.out.write(System.in.read());
System.out.write(System.in.read());
System.out.write(System.in.read());
System.out.flush();
cs

 

✅결과

 

 

이스케이프(고양이 만들기)

System.out.print()와 ESCAPE 문자를 활용하여 다음을 콘솔에 출력해주세요.

 

\    /\

 )  ( ')

)(  /  )

 \(__)|

 

출력 도구

  • System.out.write('A'); 를 작성후 실행해보세요.
    - 콘솔창에 아무것도 안나옴

  • (1)이 콘솔에 출력되는 코드를 작성해보세요.
    - System.out.flush(); 혹은 System.out.println();

    왜 이런 과정이 필요한지 설명해 보세요
    - write는 입력만 가능한 작업이라서?
      flush기능이 없기 때문에 단독으로 사용하면 출력할 수 없음

 

  • System.out.write(65);를 작성 후 콘솔에 출력 해보세요.
    - System.out.flush();를 함께 사용하였고, A가 출력되었음

    1과 관련하여 설명해 보세요
    - write만으로는 출력이 되지 않아서 flush기능을 추가하였으며,
      문자 ‘A’의 아스키 코드값은 65이기 때문에 65를 출력하면 ‘A’가 나온다

  • 오늘 날짜를 printf를 이용하여 다음 형식으로 콘솔에 출력해보세요.
    YYYY-MM-DD

    System.out.printf(“%d-%02d-%02d”,2023,6,8)

    여기서 02의 의미는
    2: 총 2자리수로 표현하겠다.
    0: 자리수가 비어있을 시 0을 넣어 채워주겠다.



 

부동소수점,고정소수점

 

  • 고정소수점(Fixed Point)

소수점의 위치를 고정시켜 표현

비트를 절반으로 나누어 정수와 소수를 표현하는 방식

구현하기 편하고 연산속도가 빠름

표현 가능한 수가 적고 제한된 부분에서만 사용 가능(소수점 아래 긴 경우 사용 불가)


  • 부동소수점(Float Point)

정수와 소수를 구분하지 않고 두 수를 하나의 공간으로 합쳐서 표현하는 방법

소수점을 고정시키지 않은 표현 방식으로 유연하고 정밀하게 표현 가능


<실수 12.5를 부동소수점으로 표현하는 방법>

실수 12.5 -> 2진수 1100.1 -> 1.1001e3

 

0.5 =  0.1

0.25 =  0.01

0.125 =  0.001




+ Recent posts