라벨이 codeground인 게시물 표시

codeground 연습문제 6 - 좋은 수

 필자는 이 문제를 풀고 나서 나름 머리를 쓴 문제라고 생각했다. 이 문제의 키워드는 "너는 배열의 인덱스를 값으로 사용할 줄 아니?" 이다. 좋은 문제이다 수열이 주어 졌을 때, 3개의 숫자의 합으로 구성이 될 수 있는 숫자이면 "좋은 수"라고 판단을 한다. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 import java.util.Arrays ; import java.util.Scanner ; class Solution { final static int M = 200000 ; static int Answer, T, N, x; static int [] a = new int [ 5005 ]; static boolean[] s = new boolean[ 400005 ]; public static void main(String[] args) { Scanner sc = new Scanner(System. in ); T = sc.nextInt(); for ( int i = 0 ; i < T; i++) { Answer = 0 ; N = sc.nextInt(); Arrays.fill(s, false); Arrays.fill(a, 0 ); for ( int j = 0 ; j < N; j++) { a[j] = sc.nextInt(); for ( int k = 0 ; k < j; k++) { ...

codeground 연습문제 5 - 미궁속의 방

 이 문제 역시 연습문제 4와 같이 단순한 계산 문제이다. 직관적으로 생각할 때 dfs로 이동을 하면서 채우는 것이 일반적이다. 하지만 시간도 오래걸릴 뿐더러 stack 공간을 많이 사용하기 때문에 비효율적이다. 따라서 좌표에 따른 해당 칸의 값을 계산하는 것이 훨씬 간단하고 복잡도도 줄일 수 있는 방법이다. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 import java.util.Scanner ; class Solution { static int T, N, K, cur, dir , x, y, reo, ceo, rs, term; static String input ; static char[] chars; static long Answer; public static void main(String[] args) { Scanner sc = new Scanner(System. in ); T = sc.nextInt(); for ( int i = 0 ; i < T; i++) { Answer = 0 ;N = sc.nextInt();K = sc.nextInt(); input = "" ;x = 1 ; y = 1 ; sc.nextLine(); input = sc.nextLine(); chars = input .toCharArray(); Answer += 1 ; for ( int j = 0 ; j ...

codeground 연습문제 4 - 다트 게임

 이번 문제는 수학적인 계산을 필요로하는 문제이다. "너는 수학적인 계산을 이용하여 게임을 만들 수 있니?" 라는 질문을 한다. 어렵지는 않지만 약간의 생각이 필요한 문제이다. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 import java.util.Scanner ; import static java. lang . Math . PI ; class Solution { static int Answer, T, N, A, B, C, D, E, x, y, quadrant, score = 0 , tmp; static double distance, inclination; static int [] scoreData = { 6 , 13 , 4 , 18 , 1 , 20 , 5 , 12 , 9 , 14 , 11 , 8 , 16 , 7 , 19 , 3 , 17 , 2 , 15 , 10 }; public static void main (String[] args) { Scanner sc = new Scanner(System. in ); T = sc. nextInt (); for ( int i = 0 ; i < T; i++) { A = sc. nextInt ();B = sc. nextInt (); C = sc. nextInt ();D = sc. nextInt (); E = sc. nextInt ();N = sc. nextInt (); Answer = 0...

codeground 연습문제 3

 이 문제는 간단한 sorting으로 해결 할 수 있다. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 import java.util.Scanner; import java.util.Arrays; class Solution { static int Answer, T, N, K; static int [] subjects; public static void main( String [] args) { Scanner sc = new Scanner(System. in ); T = sc.nextInt(); for ( int i = 0 ; i < T; i++) { N = sc.nextInt(); K = sc.nextInt(); subjects = new int [N];Answer = 0 ; for ( int j = 0 ; j < N; j++) { subjects[j] = sc.nextInt(); } Arrays.sort(subjects); for ( int j = N; j > N - K; j--) { Answer += subjects[j - 1 ]; } System.out.println( "Case #" +(i+ 1 )); System.out.println(Answer); } } }

codeground 연습문제 2

 이 문제는 주어진 입력 값을 받아 최종 우승할 가능성이 있는 응시자의 수를 구하는 문제이다. *주의할 점은 공동 우승자가 존재할 수 있으나, 공동 우승자의 수를 구하는 것은 아니다. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 import java.util.Arrays; import java.util.Scanner; class Solution { static int Answer, T, N, max, temp; static int [] player; public static void main( String [] args) { Scanner sc = new Scanner(System. in ); T = sc.nextInt(); for ( int i = 0 ; i < T; i++) { max = 0 ;Answer = 0 ;N = sc.nextInt(); player = new int [N]; for ( int j = 0 ; j < N; j++) { player[j] = sc.nextInt(); } Arrays.sort(player); for ( int j = 0 ; j < N; j++) { temp = player[j] + (N - j); max = Math .max(max, temp); } for ( int j = 0 ; j < N; j++) { ...

codeground 연습문제 1

 이 문제는 XOR 연산에 대하여 잘 알고 있는지를 묻고 있는 문제이다. 우선 주어진 숫자가 홀수번 나타난 것들 끼리 XOR연산을 하는 작업이다. XOR의 특성은 x라는 수에 y를 두번 XOR 연산을 하면 다시 x가 되기 때문에 무시가 된다. 따라서 다음과 같은 코드를 작성할 수 있다. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 import java.util.Scanner; class Solution { static int Answer, T, N, num; public static void main( String [] args) { Scanner sc = new Scanner(System. in ); T = sc.nextInt(); for ( int i = 0 ; i < T; i++) { Answer = 0 ;N = sc.nextInt(); for ( int j = 0 ; j < N; j++) { num = sc.nextInt(); if (j == 0 ) { Answer = num; } else { Answer ^= num; } } System.out.println( "Case #" +(i+ 1 )); System.out.println(Answer); } } }  출처: https://www.codeground.org/practice/...