(230502)Codeup 문제풀이 / BufferedReader
codeup 1082 ( 16진수 구구단 만들기 )
출제의도
//알파벳으러 입력받은 int타입의 16진수로 바꿀 수 있는지?
// int타입의 16진수를 0123456789ABCDEF로 표현할 수 있는지
public class Codeup1082 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String inputVal = sc.next(); //스트링 값으로 입력 받기.
int iHexVal = Integer.parseInt(inputVal,16); // 문자열로 받은 문자를 int형 16진수로 변환
System.out.println(iHexVal); //B 입력 후 출력 시 11
System.out.printf("%X\n",iHexVal); //int타입의 16진수를 0123456789ABCDEF로 표현. 출력값은 B
for (int i = 1; i < 15; i++) { //1~F까지 돌리기
System.out.printf("%s * %X = %X\n",inputVal, i, iHexVal*i); //%s는 내가 입력한 문자열(고정)
//i가 1~15까지 돌지만 %X로 표현했기 때문에 16진수로 나타남
//int형 16진수 * %X로 표현한 i 곱하기
}
}
}
팩토리얼
public class Factorial {
public static void main(String[] args) {
int n=5;
int answer=1; // answer를 1로 지정하는 것이 핵심 (곱하기 연산이기 때문에 0으로 초기화 하면 안됨)
for(int i=1; i<=n; i++){
answer = answer * i; //1에서 5까지 곱
}
System.out.println(answer);
}
}
역순으로 출력 시 : for(int = n ; i > 0 ; i - -){
코드리뷰 ( pull request )
git branch를 따서 작업한 내용을 pull request를 올리면 같이 코드를 보고 책임을 같이 지는 것
branch push -> 코드리뷰 -> merge
-리뷰어1
-리뷰어2
-...admin
반복문에서 조건식의 활용
1. 무한반복
- true
- 공백으로 놔두는 것
- i > 0 (음수 제외 시 무한 반복)
2. root 만들기
- Math.sqrt()
- i * i <= [숫자] // 7 * 7 = 49 이므로 Math.sqrt()를 사용한 결과와 같다.
package com.example.javaproject2.codeup;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.Scanner;
public class Codeup1084 {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
int cnt=0;
String Line="";
int r = sc.nextInt();
int g = sc.nextInt();
int b = sc.nextInt();
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); //O : 한글자씩 읽음 , B : 한줄씩 읽음
//println , printf -->자동차에 타는 시간
for (int i=0; i<r; i++){
for(int j=0; j<g; j++){
for(int k=0; k<b; k++){
Line += i+" "+j+" "+k+"\n";
cnt++;
}
bw.write(Line);
bw.flush();
}
}
System.out.println(cnt);
}
}
이 코드로 실행하면 결과가
2 2 2
0 0 0
0 0 1
0 0 0
0 0 1
0 1 0
0 1 1
0 0 0
0 0 1
0 1 0
0 1 1
1 0 0
1 0 1
0 0 0
0 0 1
0 1 0
0 1 1
1 0 0
1 0 1
1 1 0
1 1 1
8
이렇게 나오는데 answer이 계속해서 값이 쌓여서 answer을 0으로 한 번 초기화 해줘야한다.
String = "" 위치 상관이 있는건지? Buffered 선언 후 언급해줘야한다, 출력할 문자열
CS
BufferedWriter와 .Println()의 차이
위는 println()
- 매번 출력
아래는 BufferedWriter
- 모아서 출력
- flush()를 했을 때 내보냄
- close()로 닫아줘야 함
- String 변수 선언 후 출력할 문자열을 넣어줄 수 있음
- append()를 이용해 바로 출력 가능
1085번 왜 long이어야 하는지
while문의 어려움
- For문에 비해 예측하기가 어렵다.
- 실행 할 때마다 돌아가는 횟수가 달라진다.
while문 + %연산
- 10으로 나눈 나머지 구할 때 많이 사용 ( % 10 )
int형 자릿수 구하기
public class Codeup1620 {
public static void main(String[] args) {
int n=1234;
int answer =0;
while(n>0){
while(n>0) { //n이 0이상일 동안 반복
answer += n % 10;
n = n / 10;
}
int length = (int)(Math.log10(answer)+1);
if(length > 1){
n = answer;
}else if(length == 1){
break;
}
}
System.out.println(answer);
}
}
입력 값 받는 것 말고 int에 숫자 넣어서 해보려고 int num변수를 선언해줬습니다!.
public class Codeup1620 {
public static void main(String[] args) {
int n=1234;
int answer =0;
while(n>0){
while(n>0) { //n이 0이상일 동안 반복
answer += n % 10; //나머지 저장
n = n / 10; //몫 저장
}
System.out.println("16번줄. (몫)n : "+n+", (합)answer : "+answer);
int length = (int)(Math.log10(answer)+1); //int형 길이 구하기
System.out.println("19번줄 : "+length);
if(length > 1){ //while문을 거친 1234의 자릿수 합 길이가 두자리일 때
n = answer;
System.out.println("22번줄 : "+n);
}else if(length == 1){ //while문을 거친 1234의 자릿수 합 길이가 한자리일때
System.out.println("26 : "+answer); //값을 출력하고
break;// 반복문을 빠져나온다
}
}
System.out.println("for문 탈출");
}
}