AlgorithmicProblemSolvingStrategies / Study

AlgorithmicProblemSolvingStrategies
0 stars 0 forks source link

[1/27 문제2] DRAWRECT #4

Open jinaPlus opened 10 years ago

jinaPlus commented 10 years ago

http://algospot.com/judge/problem/read/DRAWRECT

직사각형의 마지막 좌표를 알아내는 문제입니다.

mewards commented 10 years ago

오답이라고 뜨네요 ㅠㅠ 뭐가 문제일까요

package drawrect;

import java.util.ArrayList;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        ArrayList<Integer> X = new ArrayList<>();
        ArrayList<Integer> Y = new ArrayList<>();
        int cases = sc.nextInt();
        while (cases-- > 0) {
            for (int i = 0; i < 3; i++) {
                int x = sc.nextInt();
                int y = sc.nextInt();
                X.add(x);
                Y.add(y);
            }
        }
        for (int i = 0; 3 * i < X.size(); i++) {
            double a, b, c;
            int ans_x, ans_y;
            a = Math.pow((X.get(3 * i) - X.get(3 * i + 1)), 2)
                    + Math.pow((Y.get(3 * i) - Y.get(3 * i + 1)), 2);

            b = Math.pow((X.get(3 * i + 1) - X.get(3 * i + 2)), 2)
                    + Math.pow((Y.get(3 * i + 1) - Y.get(3 * i + 2)), 2);

            c = Math.pow((X.get(3 * i) - X.get(3 * i + 2)), 2)
                    + Math.pow((Y.get(3 * i) - Y.get(3 * i + 2)), 2);
            if (Math.max(a, b) == a) {
                if (Math.max(a, c) == a) {
                    ans_x = X.get(3 * i)
                            - (X.get(3 * i + 2) - X.get(3 * i + 1));
                    ans_y = Y.get(3 * i)
                            - (Y.get(3 * i + 2) - Y.get(3 * i + 1));
                } else {
                    ans_x = X.get(3 * i)
                            - (X.get(3 * i + 1) - X.get(3 * i + 2));
                    ans_y = Y.get(3 * i + 1)
                            - (Y.get(3 * i + 1) - Y.get(3 * i + 2));
                }
            } else {
                if (Math.max(b, c) == b) {
                    ans_x = X.get(3 * i + 2)
                            - (X.get(3 * i) - X.get(3 * i + 1));
                    ans_y = Y.get(3 * i + 2)
                            - (Y.get(3 * i) - Y.get(3 * i + 1));
                } else {
                    ans_x = X.get(3 * i)
                            - (X.get(3 * i + 1) - X.get(3 * i + 2));
                    ans_y = Y.get(3 * i)
                            - (Y.get(3 * i + 1) - Y.get(3 * i + 2));
                }
            }
            System.out.println(ans_x + " " + ans_y);

        }
    }
}
Sprexatura commented 10 years ago

와우- 빠르시네요 :) 전 아직 문제도 안봐서( __)

gsgod commented 10 years ago

전 이제야 보내요 ㅠ_ㅠ 얼른 해야지!

spdkimo commented 10 years ago

색다르게 할려고 했는데 코드만 길어졌네요 ㅋ 184615 DRAWRECT spdkimo cpp 1.3KB 정답 3ms 4분 전

#include <stdio.h>
#include <string.h>

int main() {
    int x[4];
    int y[4];
    int cases;
    int nextX, nextY;
    int answerX, answerY;
    scanf("%d", &cases);
    while(cases--) {
        memset(x, 0, sizeof(x));
        memset(y, 0, sizeof(y));
        scanf("%d %d", &x[0], &y[0]);
        scanf("%d %d", &nextX, &nextY);
        if (nextX == x[0]) {
            x[1] = nextX;
        } else {
            x[2] = nextX;
        }
        if (nextY == y[0]) {
            y[1] = nextY;
        } else {
            y[2] = nextY;
        }
        scanf("%d %d", &nextX, &nextY);
        if (nextX == x[0]) {
            x[1] = nextX;
        } else {
            if (x[2] != 0)
                x[3] = nextX;
            else
                x[2] = nextX;
        }
        if (nextY == y[0]) {
            y[1] = nextY;
        } else {
            if (y[2] != 0)
                y[3] = nextY;
            else
                y[2] = nextY;
        }
        for (int i = 0; i < 4; i++) {
            if (x[i] == 0) {
                answerX = x[i-1];
                break;
            }
        }
        for (int i = 0; i < 4; i++) {
            if (y[i] == 0) {
                answerY = y[i-1];
                break;
            }
        }
        printf( "%d %d\n", answerX, answerY);
    }
}
Sprexatura commented 10 years ago

185269 에 풀었습니다

def solve(coor_1, coor_2, coor_3):
    x = -1
    y = -1

    if coor_1[0] == coor_2[0]: x = coor_3[0]
    elif coor_2[0] == coor_3[0]: x = coor_1[0]
    else: x = coor_2[0]

    if coor_1[1] == coor_2[1]: y = coor_3[1]
    elif coor_2[1] == coor_3[1]: y = coor_1[1]
    else: y = coor_2[1]

    return x, y

NumberOfTestcase = int(raw_input())
answerList = []
TestcaseNumber = 0
while NumberOfTestcase > TestcaseNumber:
    coor_1 = raw_input().strip().split()
    coor_2 = raw_input().strip().split()
    coor_3 = raw_input().strip().split()

    answerList.append(solve(coor_1, coor_2, coor_3))
    TestcaseNumber = TestcaseNumber+1

for result in answerList: print result[0], result[1]
gsgod commented 10 years ago

부끄럽지만 이제야 올리네요 엉엉 방법에 정답이 없다지만 제방법은.... 노가다 케이스 같아서 좀 거시기 하네요 ㅋㅋ 수학적으론 쉬운데 만드는건 어렵네요 ㅋ 저는 노력이 필요할듯합니다 ㅠ_ㅠ

int main(int argc, const char * argv[])
{

    // insert code here...

    int caseNum = 0;

    int intTestCaseCnt = 0;

    printf("Hello, Input test case count : ");
    scanf("%d",&intTestCaseCnt);

    int *arrayResultX  = (int*)calloc(intTestCaseCnt, sizeof(int));
    int *arrayResultY  = (int*)calloc(intTestCaseCnt, sizeof(int));

    printf("Total test count : %d\n",intTestCaseCnt);
    int intTempIntTestCaseCnt = 0;
    while (intTestCaseCnt > 0) {

        int x[3]={0,0,0};
        int y[3]={0,0,0};

        printf("======== Case NO %d ======\n",caseNum+1);
        printf("Set X / Y Array Cnt  : %lu | %lu\n",sizeof(x)/sizeof(int),sizeof(y)/sizeof(int));

        for (int i = 0; i < 3; i++) {
            printf("x%d : y%d = ",i+1,i+1);
            scanf("%d %d",&x[i],&y[i]);

            if(x[i] < 0 || y[i] < 0 || x[i] > 1000 || y[i] > 1000){
                //다시 입력!
                printf("x%d : y%d = ",i+1,i+1);
                scanf("%d %d",&x[i],&y[i]);
            }

        }

        //결과 : 처리 시작
        int resultX =0;
        int resultY =0;
        if(x[0] == x[1]){
            //x가 같으면

            if(y[0] == y[2]){
                resultX = x[2];
                resultY = y[1];
            }else{
                resultX = x[2];
                resultY = y[0];
            }

        }else{
            //x가 다르면

            if(y[0] == y[1] && x[1] == x[2]){
                resultX = x[0];
                resultY = y[2];
            }else{

                if(x[1] == x[2]){
                    resultX = x[0];
                    resultY = y[1];
                }else{
                    resultX = x[1];
                    resultY = y[0];
                }
            }

        }
        intTempIntTestCaseCnt++;
        arrayResultX[caseNum] = resultX;
        arrayResultY[caseNum] = resultY;
        //결과 : 처리 끝

        intTestCaseCnt--;       //테스트 케이스 카운트

        caseNum++;              //보여주기 편하려고 만듬
    }

    //결과 출력
    printf("------- Out Put --------");
    printf("Total test count : %d\n",intTestCaseCnt);
    for (int i = 0 ; i < intTempIntTestCaseCnt; i++) {
        printf("결과 %d: (%d,%d)\n",i+1,arrayResultX[i],arrayResultY[i]);
    }

    free(arrayResultX);
    free(arrayResultY);

    printf("\n");

    return 0;

}
Sprexatura commented 10 years ago

수고하셨습니다 :)

KyusungDev commented 10 years ago

algospot #188009

오랜만에 c++ 코드를 쓰려니.. 어색하네요.. MFC에 익숙해져서..ㅠㅠ 근데 이거 코드 어떻게 올리는거죠~? 계속 깨지네요..

#include <iostream>
#include <vector>

using namespace std;

void solve(int inputX[3], int inputY[3], vector<int>& resultX, vector<int>& resultY)
{
    int x[2];
    int y[2];

    x[0] = inputX[0];
    y[0] = inputY[0];
    x[1] = (x[0] != inputX[1]) ? inputX[1] : inputX[2];
    y[1] = (y[0] != inputY[1]) ? inputY[1] : inputY[2];

    for (int i=0; i<2; i++)
    {
        for (int j=0; j<2; j++)
        {
            if ((x[i] == inputX[0] && y[j] == inputY[0]) ||
                (x[i] == inputX[1] && y[j] == inputY[1]) ||
                (x[i] == inputX[2] && y[j] == inputY[2]))
            {
                continue;
            }

            resultX.push_back(x[i]);
            resultY.push_back(y[j]);
        }
    }
}

int main() 
{
    int cases;
    vector<int> resultX, resultY;

    cin >> cases;
    for(int c=0; c<cases; c++)
    {
        int x[3], y[3];
        for (int e=0; e<3; e++)
        {
            cin >> x[e] >> y[e];
        }

        solve(x, y, resultX, resultY);
    }

    for (int c=0; c<cases; c++)
    {
        cout << resultX[c] <<' '<< resultY[c] <<endl;
    }
}
spdkimo commented 10 years ago

Comment 다실 때

```으로 묶으시면 됩니다. (https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet)
추가로 멤버 등록도 했어요
KyusungDev commented 10 years ago

감사합니다~^^

syshin commented 10 years ago

그동안 참여못하다가 이번에 자취방에 인터넷신청해서 하게됬네요 앞으로 잘부탁드립니다~

190811

package DRAWRECT; import java.util.HashMap; import java.util.Scanner;

public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in);

    int cases = sc.nextInt();
    while(cases-- > 0) {
        int num = 3;
        HashMap<Integer, Integer> xMap = new HashMap<Integer,Integer>();
        HashMap<Integer, Integer> yMap = new HashMap<Integer,Integer>();
        while(num > 0){
            int x = sc.nextInt();
            int y = sc.nextInt();

            if(xMap.containsKey(x))
                xMap.put(x, 2);
            else
                xMap.put(x, 1);

            if(yMap.containsKey(y))
                yMap.put(y, 2);
            else
                yMap.put(y, 1);                                         
            num--;
        }

        for(int x : xMap.keySet()){
            if(xMap.get(x)==1)
                System.out.print(x + " ");
        }
        for(int y : yMap.keySet()){
            if(yMap.get(y)==1)
                System.out.println(y);
        }            
    }
}

}