https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AXZuaLsqz9wDFAST
SW Expert Academy
SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!
swexpertacademy.com
어떤 점이 원 안에 포함되는지 여부를 묻는 문제입니다.
입력에 따라 받을 수 있는 점수를 계산하고, 점수의 누적합을 리턴하면 됩니다.
표적판의 원들은 전부 동심원(중심이 같음)이기 때문에 (0,0) 점을 기준으로 화살이 꽂힌 위치의 거리가 원의 반지름보다 작으면 그 원 안에 들어있는 상태입니다.
if문을 통해 바깥쪽부터 범위를 좁혀가며 해당하는 점수를 찾았고 굳이 제곱근을 씌워 계산하지 않고 제곱을 한 채로 풀어주었습니다.
1년도 더 전에 풀었던 코드라서 새롭게 짜 보았는데 실행시간이나 메모리면에서 엄청난 차이를 보았습니다.
예전 코드를 보면 무슨 생각으로 짰나 싶은 부분이 많네요. 비교해보시라고 둘 다 첨부합니다.
더보기
import java.util.Scanner;
class q11285_SWEA_다트게임 {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int T, TC, sum;
long x, y;
T = sc.nextInt();
for (int testcase = 1; testcase < T + 1; testcase++) {
TC = sc.nextInt();
sum = 0;
for (int i = 0; i < TC; i++) {
x = sc.nextInt();
y = sc.nextInt();
if (40000 < x * x + y * y)
sum += 0;
else if (32400 < x * x + y * y)
sum += 1;
else if (25600 < x * x + y * y)
sum += 2;
else if (19600 < x * x + y * y)
sum += 3;
else if (14400 < x * x + y * y)
sum += 4;
else if (10000 < x * x + y * y)
sum += 5;
else if (6400 < x * x + y * y)
sum += 6;
else if (3600 < x * x + y * y)
sum += 7;
else if (1600 < x * x + y * y)
sum += 8;
else if (400 < x * x + y * y)
sum += 9;
else
sum += 10;
}
System.out.println("#" + testcase + " " + sum);
}
}
}
더보기
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class q11285_SWEA_다트게임Re {
static StringTokenizer st;
static StringBuilder sb = new StringBuilder();
public static void main(String args[]) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int T, TC, sum, x, y;
T = Integer.parseInt(br.readLine());
for (int t = 1; t < T + 1; t++) {
TC = Integer.parseInt(br.readLine());
sum = 0;
for (int tc = 0; tc < TC; tc++) {
st = new StringTokenizer(br.readLine());
x = Integer.parseInt(st.nextToken());
y = Integer.parseInt(st.nextToken());
int far = (x * x) + (y * y);
if (40000 < far)
sum += 0;
else if (32400 < far)
sum += 1;
else if (25600 < far)
sum += 2;
else if (19600 < far)
sum += 3;
else if (14400 < far)
sum += 4;
else if (10000 < far)
sum += 5;
else if (6400 < far)
sum += 6;
else if (3600 < far)
sum += 7;
else if (1600 < far)
sum += 8;
else if (400 < far)
sum += 9;
else
sum += 10;
}
sb.append("#").append(t).append(" ").append(sum).append("\n");
}
System.out.println(sb);
}
}
728x90
'🔍 알고리즘 > SWEA' 카테고리의 다른 글
[Java] SWEA 7699.수지의 수지맞는 여행 D4 (0) | 2022.07.04 |
---|---|
[Java] SWEA 8382.방향전환 D4 (0) | 2022.07.03 |
[Java] SWEA 8458.원점으로 집합 D4 (0) | 2022.07.03 |
[Java] SWEA 11112.셀로판지 D4 (0) | 2022.06.30 |
[Java] SWEA 10966.물놀이를 가자 D4 (0) | 2022.06.29 |