본문 바로가기

프로그래밍/알고리즘 풀이69

[알고리즘] 백준 10828번: 스택 JAVA import java.io.*; import java.util.Arrays; class MyStack{ int []stack; int size; StringBuilder sb; MyStack(){ stack=new int[100000]; size=0; sb = new StringBuilder(); } void sendMethod(String[] strArr){ switch (strArr[0]) { case "push": push(strArr[1]); break; case "pop": pop(); break; case "size": size(); break; case "empty": empty(); break; case "top": top(); break; } } void push(String num){ .. 2020. 12. 25.
[알고리즘] 백준 10815번: 숫자 카드 JAVA import java.io.*; import java.util.Arrays; /** * 숫자 카드는 정수 하나가 적혀져 있는 카드이다. * 상근이는 숫자 카드 N개를 가지고 있다. * 정수 M개가 주어졌을 때, * 이 수가 적혀있는 숫자 카드를 상근이가 가지고 있는지 아닌지를 구하는 프로그램을 작성하시오. * */ //정렬 + 이분탐색 public class Main { public static void main(String[] args) throws Exception{ int N,M=0; BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); N = Integer.parseInt(br.readLine()); String[]st.. 2020. 12. 21.
[알고리즘] 백준 2751번: 수 정렬하기 2 Java import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringBuilder sb = new StringBuilder(); int N = Integer.parseInt(br.readLine()); ArrayList list = new ArrayList(); for(int i = 0; i.. 2020. 10. 26.
[알고리즘] 퀵 정렬 Java 퀵 정렬은 가장 빠른 정렬 알고리즘 중의 하나로 시간 복잡도는 평균적으로 O(nlog n)입니다. 퀵 정렬 예시 public class Main { public static void main(String[] args) { int data[] = {1,5,9,7,6,4,23}; quickSort(data, 0, data.length - 1); for(int i=0; i pivot) right--; if(left 2020. 10. 26.
[알고리즘] 백준 2750번 수 찾기 Java import java.io.*; import java.util.*; public class Main { //N개의 수가 주어졌을 때, 이를 오름차순으로 정렬하는 프로그램을 작성하시오. public static void main(String[] args) throws Exception{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(br.readLine()); int []num=new int[n]; for(int i=0;i 2020. 8. 28.
[알고리즘] 백준 1920번 수 찾기 Java 시간초과 된 소스입니다. import java.util.*; public class Main { public static void main(String[] args) { Scanner scan=new Scanner(System.in); int n=Integer.parseInt(scan.nextLine()); String strArray[]=scan.nextLine().split(" "); int m=Integer.parseInt(scan.nextLine()); String searchArray[]=scan.nextLine().split(" "); int [] result=new int [m]; for(int i=0;i 2020. 8. 25.
[알고리즘] 백준 2884번 알람 시계 Java 다중 if로 코딩하였습니다. import java.util.*; public class Main { public static void main(String[] args) { Scanner scan=new Scanner(System.in); String alarm=scan.nextLine(); String [] strArray=alarm.split(" "); int h= Integer.parseInt(strArray[0]); int m = Integer.parseInt(strArray[1]); wakeUp(h,m); } public static void wakeUp(int h, int m) { if(m>=45) { System.out.println(h+" "+(m-45)); }else if(m0) { Sy.. 2020. 8. 18.
[알고리즘] 백준 14681번 사분면 고르기 Java 다중 if를 사용하여 코딩하였습니다. import java.util.*; public class Study01 { public static void main(String[] args) { Scanner scan=new Scanner(System.in); int x=scan.nextInt(); int y=scan.nextInt(); selectQuadrant(x,y); } public static void selectQuadrant(int x, int y) { if(x>0&&y>0) { System.out.println(1); }else if(x>0&&y 2020. 8. 18.
[알고리즘] 백준 2753번 윤년 Java import java.util.Scanner; public class Main { public static void main(String[] args) { // 인자를 받는다. Scanner scan= new Scanner(System.in); String InputNum=scan.nextLine(); int year=Integer.parseInt(InputNum); //윤년은 연도가 4의 배수이면서, 100의 배수가 아닐 때 또는 400의 배수일 때이다. if((year%4==0&&year%100!=0)||year%400==0){ System.out.println(1); }else{ System.out.println(0); } } } 2020. 6. 29.
[알고리즘] 백준 9498번 시험 성적 Java 다중 if 문을 사용하여 작성하였습니다. import java.util.Scanner; public class Main { public static void main(String[] args) { // 인자를 받는다. Scanner scan= new Scanner(System.in); String InputNum=scan.nextLine(); int score=Integer.parseInt(InputNum); if(score>=90) System.out.println("A"); else if(score >=80) System.out.println("B"); else if(score >=70) System.out.println("C"); else if(score >=60) System.out.println.. 2020. 6. 29.
[알고리즘] 백준 1330번 두 수 비교하기 Java import java.util.Scanner; public class Main { public static void main(String[] args) { // 인자를 받는다. Scanner scan= new Scanner(System.in); String InputNum=scan.nextLine(); String []Num=InputNum.split(" "); int A=Integer.parseInt(Num[0]); int B=Integer.parseInt(Num[1]); System.out.println(anwser(A,B)); } //비교해서 출력값을 내주는 함수 public static String anwser(int A, int B){ if(A>B){ return ">"; }else if(A 2020. 6. 26.
[알고리즘] 백준 2588번 곱셉 JAVA import java.util.Scanner; public class Main { public static void main(String[] args) { // 인자를 받는다. Scanner scan= new Scanner(System.in); int a=scan.nextInt(); int b=scan.nextInt(); //각 자리수의 숫자를 구한다. int b3=(b/100)%10; int b2=(b/10)%10; int b1=b%10; //각 자릿수의 a를 곱하면 원하는 값이 나온다. int answer1=b1*a; int answer2=b2*a; int answer3=b3*a; System.out.println(answer1); System.out.println(answer2); System.ou.. 2020. 6. 25.
[알고리즘] 백준 10757번 큰 수 A+B Java 자바는 큰수를 BigInteger 클래스로 정의 하였습니다. math api를 사용하여 작성하였습니다. import java.util.Scanner; import java.math.*; public class Main { public static void sum(BigInteger A, BigInteger B){ System.out.println(A.add(B)); } public static void main(String[] args) { Scanner scan=new Scanner(System.in); String ab=scan.nextLine(); String strab[]=ab.split(" "); BigInteger A=new BigInteger(strab[0]); BigInteger B=new.. 2020. 6. 24.
[알고리즘] 유클리드 호제법을 이용하여 최대 공약수를 구하는 알고리즘 유클리드 호제법을 이용하여 최대 공약수를 구하는 알고리즘 유클리드 호제법 예시 1071과 1029의 최대공약수를 구하면, 1071은 1029로 나누어떨어지지 않기 때문에, 1071을 1029로 나눈 나머지를 구한다. ≫ 42 1029는 42로 나누어떨어지지 않기 때문에, 1029를 42로 나눈 나머지를 구한다. ≫ 21 42는 21로 나누어떨어진다. 따라서, 최대공약수는 21이다. (위키백과 출처) 유클리드 호제법 알고리즘은 재귀함수를 사용하여 구현한다. public class study01 { //최대공약수를 구하는 알고리즘 static int gcd(int x, int y) { if(y==0) return x; else return gcd(y,x%y); } public static void main(.. 2020. 4. 28.
[알고리즘] 재귀알고리즘 재귀 알고리즘 재귀 함수란 함수 자신을 다시 호출하여 작업을 수행하는 방식의 함수를 말합니다. 반복문을 사용하여 구현하며 무한루프에 빠지지 않게 종료조건이 꼭 들어가 있어야합니다. 팩토리얼 구하기 예제 import java.util.*; public class study01 { static int factorial(int n) { if(n>0) return n * factorial(n-1); else return 1; } public static void main(String[] args) { Scanner stdInt = new Scanner(System.in); System.out.println("정수를 입력하세요."); int x=stdInt.nextInt(); System.out.println(x.. 2020. 4. 27.
[알고리즘] 이진검색 이진 검색 알고리즘(binary search algorithm)이란 오름차순으로 정렬된 리스트에서 특정한 값의 위치를 찾는 알고리즘이다. 처음 중간의 값을 임의의 값으로 선택하여, 그 값과 찾고자 하는 값의 크고 작음을 비교하는 방식을 채택하고 있다. 처음 선택한 중앙값이 만약 찾는 값보다 크면 그 값은 새로운 최댓값이 되며, 작으면 그 값은 새로운 최솟값이 된다. 검색 원리상 정렬된 리스트에만 사용할 수 있다는 단점이 있지만, 검색이 반복될 때마다 목표값을 찾을 확률은 두 배가 되므로 속도가 빠르다는 장점이 있다. (위키백과 출처) 이진 검색은 검색을 반복할때마다 검색 범위가 반으로 줄어서 검색에 필요한 평균 횟수는 log n 입니다. 이진 검색은 정렬이 되어 있음을 가정합니다. import java... 2020. 4. 26.
반응형