Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[01주차 이은지] 백준1105_팔 #5

Merged
merged 1 commit into from
Oct 21, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions 01주차/이은지/BaekJoon1105.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// 1105번 : L <= n <= R 을 만족하는 자연수 n 중에 8이 가장 적게 들어있는 수의 8의 개수 구하기
// L R 입력 ( L <= R <= 2,000,000,000
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;

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

// 8이 들어가는 경우 -> L과 R이 공통 자릿수에 같은 숫자(ex 8800 8812)를 갖고잇는데 그 숫자가 8인 경우
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String LR = br.readLine(); // 개행문자를 기준으로 입력받는 것
// StringTokenizer st = new StringTokenizer(" ");
// st.nextToken();
String[] arr = LR.split(" ");
String L = arr[0];
String R = arr[1];
Comment on lines +15 to +17
Copy link
Member

@hepheir hepheir Oct 14, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

오호, 숫자로 변환하지 않고 바로 문자열로서 처리하셨군요

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

넵 카운트만 하면되기도 하고 문자열로 처리하면 배열 처리도 쉬울 것 같아서 문자열로 했슴다!!


// 조건 1 -> 같은 자리수여야함 다른 자리수면 개수 무조건 0 -> 길이
if(L.length() != R.length()){
System.out.println(0);
} else {
int count = 0;
for( int i = 0; i < L.length()&&L.charAt(i)==R.charAt(i); i++){
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

c 스타일의 for문에선 이렇게 높은 수준의 코드를 구현할 일이 없어서 잘 몰랐는데, 당연하지만 for문의 조건 검사절 (중간)에서도 and 연산이 가능하군요!! 잘 생각하지 못했던 부분인데 다음 구현에 참고하겠습니다 🙉

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

charAt에 대해 잘 몰랐는데 덕분에 하나 배워갑니다

// 조건2 각 문자의 자릿수가 8로 같으면 카운트 추가
// String.charAt(i) 문자열에서 한문자씩 반환
if( R.charAt(i)=='8' ){
count++;
}
}
System.out.println(count);

}

}
}
Loading