프로그래밍/문제풀이

acmicpc 1004 문제풀이 [오답..]

Cycrypt0 2020. 1. 18. 14:42
728x90
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <math.h>
 
int a_my_final_result[1000000= { 0, };
int i_res = 0;
 
void Calc(int x1, int y1, int r1, int x2, int y2, int r2) // 입력값을 적절히 계산하여 최종 결과값을 출력하는 함수
{
    int i_len_center_of_the_center = 0;    // 원의 점과 점 사이의 거리
    int i_sum_of_two_rad = 0;    // 두 원의 각각 반지름의 합
    int final_result = 0// 최종 결과 출력
 
    i_len_center_of_the_center = (int)sqrt(pow(((double)x1 - (double)x2), 2.0+ pow(((double)y1 - (double)y2), 2.0));
    i_sum_of_two_rad = r1 + r2; //계산공식
    
    if (x1 == x2 && y1 == y2) {    // 원점이 같은 경우
        if (r1 == r2)
            final_result = -1;
        else
            final_result = 0;
    }
 
    else {
        if (i_len_center_of_the_center > i_sum_of_two_rad)
            final_result = 0;
 
        else if (i_len_center_of_the_center == i_sum_of_two_rad)
            final_result = 1;
 
        else if (i_len_center_of_the_center < i_sum_of_two_rad)
            final_result = 2;
 
        else
            final_result = -1;
    }
 
    a_my_final_result[i_res] = final_result;
    i_res++;
}
 
int main(void)
{
    int a_circle_location[6= { 0, }; // 입력값을 받아들여옴
    int i_rp_count = 0;
 
    if (scanf("%d"&i_rp_count) == 1);
    else
        printf("Failed to read integer");
 
 
    for (int i = 0; i < i_rp_count; i++) { //들어온 테스트케이스만큼의 루프를 돌림
        for (int j = 0; j < 6; j++) {    // 좌표값과, 거리의 값을 받아들여 배열에 저장 후 func calc 호출 후 결과 출력
            if (scanf("%d"&a_circle_location[j]) == 1);    // 배열에 값을 입력받아 저장.
 
            else
                printf("Failed to input array");
        }
 
        Calc(a_circle_location[0], a_circle_location[1], a_circle_location[2],
            a_circle_location[3], a_circle_location[4], a_circle_location[5]);
    }
 
    for (int k = 0; k < i_res; k++) {
        printf("%d\n", a_my_final_result[k]);
    }
}
cs

약 70줄의 코드이다..

대략적인 설명은 주석으로 쓰여있으나 프로그램을 풀기 위한 방식은 두 원의 접점의 개수를 출력하는 것이다.

원이 일부 겹치면 접점은 2개, 두 원이 서로 접하면 접점은 1개 두 원이 서로 일치하면 접점은 무한개로 -1을 출력해야 한다.

접점이 없는 경우
접점이 1개인 경우

 

접점이 2개인 경우

 

접점이 무한대인 경우[서로 일치]

또한 두 원의 중심이 일치할 때, 반지름의 길이가 같으면 서로 일치하는 원 이므로 접점은 -1개 이다. 코드상에서는 원이 일치할 때의 경우를 두번 찾게 만들었는데, 적절히 수정하였다.

 

원점이 같고, 반지름이 다른 경우 [접점 0개]

 

i_rp_count라는 변수값으로 첫번째 테스트 케이스 만큼의 반복을 돌릴 준비를 하고, 이후, a_circle_location이라는 배열을 통해 x1,y1,r1,x2,y2,r2의 값을 받아온다.

오랜만에 C를 해서 그런지 scanf를 받는 방식이 달라졌다.

이전에는

#define _CRT_SECURE_NO_WARNINGS를 가장 위에 선언해주기만 하면 되었는데, 

https://stackoverflow.com/questions/7271939/warning-ignoring-return-value-of-scanf-declared-with-attribute-warn-unused-r

 

Warning: ignoring return value of 'scanf', declared with attribute warn_unused_result

#include int main() { int t; scanf("%d", &t); printf("%d", t); return 0; } I compiled the above C code using ideone.com and the following warning popped up: ...

stackoverflow.com

위와 같은 오류가 발생하여 해당 글을 보고 참고하였다.

Calc라는 함수에서는 중심과 중심의 좌표의 거리와, 반지름의 합을 나타낸 함수를 호출하여, 전역변수로 설정해 둔 int a_my_final_result[1000000= { 0, };

int i_res = 0;

에 저장하였다, 여기서 i_res는 나중에 출력할 때 a_my_final_result에 저장된 최종 방의 번호를 저장하기 위한 용도로 선언하였다.

 

그러나 https://acmicpc.net/problem/1002 에서는 오답으로 나와 다른 방법을 찾아봐야 할것이다..

728x90