본문 바로가기

알고리즘19

백준 2485 가로수 자바 가로수 문제 보러가기 문제 설명 자바 코드 import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.Arrays; import java.util.Stack; public class Main { static int[] tree; public static void main(String[] args) throws Exception{ BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int N=Integer.parseInt(br.readLine()); tree=new int[N]; for(int i=0;i 2021. 6. 3.
백준 15886번 치킨 배달 자바 치킨 배달 문제 보러가기 문제 설명 자바 코드 import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.List; import java.util.StringTokenizer; public class Main { static int N; static int M; static BufferedReader br; static int result; static boolean[] chk; static List chickenPoint; static List housePoint; public static void main(String[] args) throws Exception{ .. 2021. 5. 27.
[알고리즘] 백준 1874번: 스택 수열 JAVA import java.io.*; import java.util.*; public class Main { public static void main(String[] args) throws Exception { int N = 0; BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); boolean resultBool=true; N = Integer.parseInt(br.readLine()); Stack stack =new Stack(); Stack resultStack =new Stack(); StringBuilder sb = new StringBuilder(); for(int i=N;i>0;i--){ stack.push(i); .. 2021. 4. 12.
[알고리즘] 백준 11650, 11651번: 좌표 정렬하기, 좌료 정렬하기2 JAVA 백준 11650, 11651번 좌표정렬하기, 좌표정렬하기2 Collection.sort 와 comare 메소드를 람다식으로 작성하여 구현하였습니다. import java.io.*; import java.util.*; public class Main { public static void main(String[] args) throws Exception{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n=Integer.parseInt(br.readLine()); List list=new ArrayList(); for(int i=0;i { return (s1[0]!=s2[0])?Integer.compare(s1[0],.. 2021. 1. 14.
[알고리즘] 백준 1181번: 단어정렬 JAVA import java.io.*; import java.util.*; public class Main { public static void main(String[] args) throws Exception{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n=Integer.parseInt(br.readLine()); Set strSet = new HashSet(); for(int i=0;i{ return (s1.length()-s2.length()!=0)?Integer.compare(s1.length(),s2.length()):s1.compareTo(s2); }); for (String str : strArray) .. 2021. 1. 7.
[알고리즘] 백준 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.
[알고리즘] 백준 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.
[알고리즘] 백준 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.
반응형