본문 바로가기

Study/algorithm

(10)
백준 자바 알고리즘 [4344 : 평균은 넘겠지] Scanner sc = new Scanner(System.in); int test = sc.nextInt(); //케이스 수 int num = sc.nextInt(); //학생 수 int[] s = new int[num]; double avg = 0; //평균 for (int i = 0; i avg){ avgS++; } } System.out.printf("%.3f",..
백준 자바 알고리즘 [8958 : OX퀴즈] Scanner sc = new Scanner(System.in); int num = sc.nextInt(); String[] str = new String[num]; for (int i = 0; i < num; i++) { int count = 0, sum = 0; str[i] = sc.next(); for (int j =0; j < str[i].length(); j++) { if (str[i].charAt(j) == 'o') sum += ++count; else count = 0; } System.out.println(sum); } sc.close(); string배열을 이용해서 해결. str[0]에 'oooxxooxx' 이렇게 들어가 있다면 charAt()으로 각 인덱스의 값을 구할 수 있다. 그 인..
백준 자바 알고리즘 [나머지] Scanner sc = new Scanner(System.in); int[] number = new int[10]; for (int i = 0; i < 9; i++) { number[i] = sc.nextInt(); number[i]%=42; } sc.close(); IntStream stream = Arrays.stream(number); System.out.println(stream.distinct().count()); int배열 생성. for문으로 10개의 임의의 수를 생성해서 배열에 넣고 넣은 배열을 42로 나눠서 다시 배열에 넣어준다. 스트림을 이용해서 distict으로 중복을 제거하고 count로 숫자를 세면됨. 스트림을 이용하여 간단하게 해결할 수 있다. 다른 정답 코드들. https://i..
백준 자바 알고리즘 [숫자의 개수] Scanner sc = new Scanner(System.in); int A = sc.nextInt(); int B = sc.nextInt(); int C = sc.nextInt(); sc.close(); int[] counts = new int[10]; int number = A * B * C; while (number > 0) { counts[number % 10]++; number /= 10; } for (int i = 0; i < counts.length; ++i) { System.out.println(counts[i]); } counts 배열은 {0,0,0,0...}; 이겠지 while문을 사용해서 abc의 곱이 0보다 클때까지 돌려주고 counts[나머지값]이 6이면 counts[6]이 되니까..
백준 자바 알고리즘 [최댓값] Scanner sc = new Scanner(System.in); int []a = new int[9]; int cut = 0; int max = 0; for (int i = 0; i < 9; i++) { a[i] = sc.nextInt(); if (max
백준 자바 알고리즘 [최소,최대] Scanner sc = new Scanner(System.in); int k = sc.nextInt(); int[] a = new int[k]; for (int j=0;j a[i]){ min = a[i]; } } System.out.println(min+" "+max); if문으로 최소값이면 min에 넣고 최대값이면 max 넣어 줌
백준 알고리즘 세수 자바 Scanner sc = new Scanner(System.in); int A = sc.nextInt(); int B = sc.nextInt(); int C = sc.nextInt(); if(A < B) { if(B < C) { System.out.println(B); } else if (A < C) { System.out.println(C); } else { System.out.println(A); } } else { if(A < C) { System.out.println(A); } else if(C < B) { System.out.println(B); } else { System.out.println(C); } Scanner sc = new Scanner(System.in); int[] intArr =..
백준 알고리즘 상근날드 자바 Scanner sc = new Scanner(System.in); int burger = 2001; int drink = 2001; // 버거 for (int i = 0; i < 3; i++) { int value = sc.nextInt(); if (value < burger) {// 최솟값 burger = value; } } // 음료 for (int i = 0; i < 2; i++) { int value = sc.nextInt(); if (value < drink) {// 최솟값 drink = value; } } System.out.println(burger + drink - 50); 버거와 음료의 최솟값을 찾아주면 되는 문제이다. 최댓값이 2000원 이하이므로 기본값을 2001원을 주면 된다. 나머..