수학 1

sua·2022년 10월 10일
0

Baekjoon

목록 보기
155/161
post-thumbnail

10430번 나머지

문제



풀이

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int a = sc.nextInt();
        int b = sc.nextInt();
        int c = sc.nextInt();

        System.out.println((a + b) % c);
        System.out.println(((a % c) + (b % c)) % c);
        System.out.println((a * b) % c);
        System.out.println(((a % c) * (b % c)) % c);

        sc.close();
    }
}



2609번 최대공약수와 최소공배수

문제



풀이

최소공배수는 최대공약수와 각수를 최대공약수로 나눈 몫의 곱을 곱한 값이다.

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int a = sc.nextInt();
        int b = sc.nextInt();

        int tmp = 0; // 최대공약수
        
        if(b > a) {
            tmp = a;
            a = b;
            b = tmp;
        }
        
        for(int i = b; i > 0; i--) {
            if((a % i == 0) && (b % i == 0)) {
                tmp = i;
                break;
            }
        }
        
        System.out.println(tmp);
        System.out.println(tmp * (a / tmp) * (b / tmp)); // 최소공배수
        
        sc.close();
    }
}



1934번 최소공배수

문제



풀이

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        
        for (int i = 0; i < n; i++) {
            int a = sc.nextInt();
            int b = sc.nextInt();

            int gcd = gcd(a, b);
            System.out.println(a * (b / gcd));
        }
        
        sc.close();
    }

    static int gcd (int a, int b) {
        int r = a % b;
        if (r == 0) {
            return b;
        }
        return gcd(b, r);
    }
}



1978번 소수 찾기

문제



풀이

import java.util.Scanner;

public class Main {
    static boolean isPrime(int m) {
        if(m == 1) {
            return false;
        }
        
        int l = 2;
        while(l < m) {
            if(m % l == 0) {
                return false;
            }
            l++;
        }
        
        return true;
    } 
    
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int n = sc.nextInt();
        int cnt = 0;

        for(int i = 0; i < n; i++) {
            int m = sc.nextInt();
            if(isPrime(m)) {
                cnt++;
            }
        }
        System.out.println(cnt);

        sc.close();
    }
}



1929번 소수 구하기

문제



풀이

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        
        int a = sc.nextInt();
        int b = sc.nextInt();
        
        boolean arr[] = new boolean[b + a];
        arr[0] = true;
        arr[1] = true;
        
        for(int i = 2; i <= Math.sqrt(b + 1); i++) {
            for(int j = i * i; j < b + 1; j += i) {
                arr[j] = true;
            }
        }
        
        for(int i = a; i < b + 1; i++) {
            if(arr[i] == false) {
                System.out.println(i);
            }
        }
        
        sc.close();
    }
}



10872번 팩토리얼

문제


풀이

import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		System.out.println(fact(sc.nextInt()));
		sc.close();
	}

	public static int fact(int n) {
		if (n <= 1)
			return 1;
		else
			return fact(n - 1) * n;
	}

}



1676번 팩토리얼 0의 개수

문제


풀이

소인수분해를 해서 2와 5가 존재할 때 뒷자리는 0으로 끝난다.
-> 5의 배수마다 0의 개수가 증가함
-> 5로 나누면서 누적합을 구해주면 됨

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int count = 0;

        while(n >= 5) {
            count += n / 5;
            n /= 5;
        }
        System.out.println(count);
        
        sc.close();
    }
}
profile
가보자고

0개의 댓글