Skip to content

Commit

Permalink
Merge pull request #17 from yjll1019/master
Browse files Browse the repository at this point in the history
알고리즘 기초 수학 문제 풀이
  • Loading branch information
yjll1019 authored Aug 13, 2018
2 parents d1673bb + b58ee42 commit c810de6
Show file tree
Hide file tree
Showing 5 changed files with 188 additions and 0 deletions.
29 changes: 29 additions & 0 deletions math/Code_10430.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package math;

import java.util.Scanner;

/*
* 작성일 : 2018년 07월 25일
* 내 용 : 나머지
* 첫째 줄에 (A+B)%C, 둘째 줄에 (A%C + B%C)%C, 셋째 줄에 (A×B)%C, 넷째 줄에 (A%C × B%C)%C를 출력한다.
*/
public class Code_10430 {

public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);

String[] s = input.nextLine().split(" ");

int A = Integer.parseInt(s[0]);
int B = Integer.parseInt(s[1]);
int C = Integer.parseInt(s[2]);


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);
}

}
39 changes: 39 additions & 0 deletions math/Code_11005.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package math;

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

/*
* 작성일 : 2018년 08월 13일
* 내 용 : 진법 변환2
* 10진법 수 N이 주어지면 이 수를 B진법으로 바꿔 출력.
*/
public class Code_11005 {

public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

String[] s = br.readLine().split(" ");

//10진법을 넘어가는 진법은 알파벳으로 사용 - 숫자도 문자로 사용
int n = Integer.parseInt(s[0]);
int b = Integer.parseInt(s[1]);
int r;
StringBuilder sb = new StringBuilder(); //나머지를 저장하는 sb
while(n!=0) {
r = n%b;
if(r<10) { //0~9도 문자로
sb.append((char)(r+'0'));
}else {
sb.append((char)(r+'A'-10));
}

n = n/b;
}

sb.reverse();
System.out.println(sb);
}
}
36 changes: 36 additions & 0 deletions math/Code_1934.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package math;

import java.util.Scanner;

/*
* 작성일 : 2018년 08월 13일
* 내 용 : 최소공배수
*/
public class Code_1934 {

public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);

int n = sc.nextInt();

int a,b,lcm;

while(n-->0) {
a = sc.nextInt();
b = sc.nextInt();

lcm = a*b/getGCD(b,a%b);
//최소공배수 = a * b / 최대공약수

System.out.println(lcm);
}
}
//최대공약수를 구하는 메소드 getGCD - 유클리드 호제법 이용.
public static int getGCD(int b, int r) {
if(r==0)
return b;
else
return getGCD(r, b%r);
}
}
38 changes: 38 additions & 0 deletions math/Code_2609.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package math;

import java.util.Scanner;

/*
* 작성일 : 2018년 08월 13일
* 내 용 : 최대공약수와 최소공배수
*/
public class Code_2609 {

public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);

int[] arr = new int[2];
//입력되는 두 자연수가 10,000이하이므로 두 수를 곱해도(1억) int의 범위(21억)에 포함된다.

for(int i=0; i<2; ++i) {
arr[i] = sc.nextInt();
}

//최대공약수 : 유클리드 호제법 이용.
int gcd = getGCD(arr[1], arr[0]%arr[1]);

//최소공배수 : 최대공약수 이용.
int lcm = arr[0]*arr[1]/gcd;

System.out.println(gcd+"\n"+lcm);
}

static int getGCD(int b, int r) {
if(r==0)
return b;
else
return getGCD(r, b%r);
}

}
46 changes: 46 additions & 0 deletions math/Code_9613.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package math;

import java.util.Scanner;

/*
* 작성일 : 2018년 08월 13일
* 내 용 : GCD(최대공약수) 합
* n개의 수가 주어지면 모든 쌍의 GCD의 합을 출력.
*/
public class Code_9613 {

public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);

int n = sc.nextInt();

while(n-->0) {
int x = sc.nextInt();

int[] arr = new int[x];

for(int i=0; i<x; ++i)
arr[i] = sc.nextInt();

long sum=0; //최대 공약수의 합을 저장하는 변수

int gcd;

for(int i=0; i<x; ++i) {
for(int j=i+1; j<x; ++j) {
gcd = getGCD(arr[j], arr[i]%arr[j]);
sum+=gcd;
}
}
System.out.println(sum);
}
}
//최대공약수를 구하는 메소드 getGCD - 유클리드 호제법 이용.
public static int getGCD(int b, int r) {
if(r==0)
return b;
else
return getGCD(r, b%r);
}
}

0 comments on commit c810de6

Please sign in to comment.