"프로그래머스 169198 당구 연습"의 두 판 사이의 차이

(새 문서: ==개요== {{프로그래머스|레벨=2|페이지=1|분류=연습문제}} ==C++== <syntaxhighlight lang='cpp'> #include <string> #include <vector> #include <algorithm> using namespa...)
 
10번째 줄: 10번째 줄:


vector<int> solution(int m, int n, int startX, int startY, vector<vector<int>> balls) {
vector<int> solution(int m, int n, int startX, int startY, vector<vector<int>> balls) {
     vector<int> distances;
     vector<int> answer;
 
     for (const auto& ball : balls) {
     for (const auto& ball : balls) {
         int x = ball[0];
         int x = ball[0];
         int y = ball[1];
         int y = ball[1];
         if (startX == x) {
         if (startX == x) {
             int a = min(startX + x, 2 * m - startX - x);
             int a = min(startX + x, 2 * m - startX - x);
             int b = startY >= y ? 2 * n - startY - y : startY + y;
             int b = startY >= y ? 2 * n - startY - y : startY + y;
             distances.push_back(min(a * a + (startY - y) * (startY - y), b * b));
             answer.push_back(min(a * a + (startY - y) * (startY - y), b * b));
             continue;
             continue;
         }
         }
25번째 줄: 23번째 줄:
             int a = startX >= x ? 2 * m - startX - x : startX + x;
             int a = startX >= x ? 2 * m - startX - x : startX + x;
             int b = min(startY + y, 2 * n - startY - y);
             int b = min(startY + y, 2 * n - startY - y);
             distances.push_back(min((startX - x) * (startX - x) + b * b, a * a));
             answer.push_back(min((startX - x) * (startX - x) + b * b, a * a));
             continue;
             continue;
         }
         }
         int a = min(startX + x, 2 * m - startX - x);
         int a = min(startX + x, 2 * m - startX - x);
         int b = min(startY + y, 2 * n - startY - y);
         int b = min(startY + y, 2 * n - startY - y);
         distances.push_back(min((startX - x) * (startX - x) + b * b, a * a + (startY - y) * (startY - y)));
         answer.push_back(min((startX - x) * (startX - x) + b * b, a * a + (startY - y) * (startY - y)));
     }
     }
 
     return answer;
     return distances;
}
}
</syntaxhighlight>
</syntaxhighlight>

2024년 2월 1일 (목) 22:01 판

1 개요

프로그래머스 169198 당구 연습

2 C++

#include <string>
#include <vector>
#include <algorithm>
using namespace std;

vector<int> solution(int m, int n, int startX, int startY, vector<vector<int>> balls) {
    vector<int> answer;
    for (const auto& ball : balls) {
        int x = ball[0];
        int y = ball[1];
        if (startX == x) {
            int a = min(startX + x, 2 * m - startX - x);
            int b = startY >= y ? 2 * n - startY - y : startY + y;
            answer.push_back(min(a * a + (startY - y) * (startY - y), b * b));
            continue;
        }
        if (startY == y) {
            int a = startX >= x ? 2 * m - startX - x : startX + x;
            int b = min(startY + y, 2 * n - startY - y);
            answer.push_back(min((startX - x) * (startX - x) + b * b, a * a));
            continue;
        }
        int a = min(startX + x, 2 * m - startX - x);
        int b = min(startY + y, 2 * n - startY - y);
        answer.push_back(min((startX - x) * (startX - x) + b * b, a * a + (startY - y) * (startY - y)));
    }
    return answer;
}
문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}