티스토리 뷰

728x90

* 본 포스팅은 개인공부를 위한 오답노트입니다.

* This posting is an incorrect answer note for personal study.


최대공약수와 최소공배수

시간 제한 메모리 제한 제출 정답 맞힌 사람 정답 비율
1 초 128 MB 102100 58793 47777 57.830%

문제

두 개의 자연수를 입력받아 최대 공약수와 최소 공배수를 출력하는 프로그램을 작성하시오.

입력

첫째 줄에는 두 개의 자연수가 주어진다. 이 둘은 10,000이하의 자연수이며 사이에 한 칸의 공백이 주어진다.

출력

첫째 줄에는 입력으로 주어진 두 수의 최대공약수를, 둘째 줄에는 입력으로 주어진 두 수의 최소 공배수를 출력한다.

예제 입력 1

24 18

예제 출력 1

6
72

Result

import java.util.Scanner;
public class Main {	
	static int gcd = 0;
	static int lcm = 0;
	static int r = 0;
	
	public static  void setGcd(int a, int b) {
		getGcd(a,b);
		setLcm(gcd,a,b);
	}
	public static void getGcd(int a, int b) {
		if(b==0) {
			System.out.println("Enter valid int");
		} else { 
			while(a%b>0) {
				r=a%b;
				a=b;
				b=r;
			} 
			gcd = b;
		}
		System.out.println(gcd );	
		}
	
	public static void setLcm(int gcd, int a, int b) {
		System.out.println((a*b)/gcd);
	}
	public static void main(String[] args) {
	Scanner sc = new Scanner(System.in);
	int a = sc.nextInt();
	int b = sc.nextInt();
	setGcd(a,b);
	}
}

 


Review

 

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Main2 {
	public static void main(String[] args) throws IOException {
	BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
	StringTokenizer st = new StringTokenizer(br.readLine()," ");
	
	int a = Integer.parseInt(st.nextToken());
	int b = Integer.parseInt(st.nextToken());
	
	int d = gcd(a,b);
	
	System.out.println(d);
	System.out.println(a*b/d);
	
	//최대공약수 반복문
	}
	private static int gcd(int a, int b) {

		while(b!=0) {//while문의 조건: !=0. 즉, b=0이 되는 순간 a값(GCD)리턴
			int r = a%b;
			a=b;
			b=r;
		}
	return a;
	}
}

BufferedReader

  1. Scanner와 BufferedReader의 차이점은? 왜 BufferedReader를 사용할까?
  • Scanner Class
    • a simple text scanner that chan parse primitive types and strings. It internally uses regular expressions to read different types.
    • Scanner are of particular use such as reading primitive data type directly and is also used for regular expressions.
  • Buffered Reader
    • On the other hand, BufferedReader class reads text from a character-input stream, buffering characters so as to provide for the efficient reading of the sequence of characters.
    • BufferedReader is a lot faster than Scanner because it buffers the character so I don’t have to access the file each time I want to read a char from it.
    • “Reading”
      • If I don’t need to parse something, then reading is sufficient.
    → 굳이 분석할 필요가 없다면, Buffered Reader를 사용하는 것만으로 충분함.

 

BufferedReader vs Scanner in Java

  1. A scanner is a much more powerful utility than BufferedReader. It can parse the user input and read an int, short, byte, float, long, and double apart from String. On the other hand, BufferedReader can only read String in Java
  2. BuffredReader has a significantly large buffer (8KB) than Scanner (1KB), which means if you are reading long String from a file, you should use BufferedReader but for short input and input other than String, you can use Scanner class.

StringTokenizer

  1. What is StringTokenizer Class?

StringTokenizer class in Java is used to break a string into tokens.

 

Example:

Input : if string --> "hello geeks" and Delimiter is " ", then
Output:  tokens are "hello" and "geeks".

 

Example:

Input : String --> is "hello geeks"and Delimiter is " ", then
Output:Tokens --> "hello", " " and "geeks".

 

Example:

Syantax:
StringTokenizer st1 = new StringTokenizer( "2+3-1*8/4", "+*-/");
Input : String --> is "2+3-1*8/4" and Delimiters are +,*,-,/
Output: Tokens --> "2","3","1","8","4".

 

'코딩 테스트' 카테고리의 다른 글

시험 성적  (0) 2023.09.26
사분면 고르기  (0) 2023.09.21
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/05   »
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
글 보관함