```
백준 7510번 고급수학 C++ 구현해보기
```

이번 글을 통해 배워갈 내용
- 백준 7510번 고급 수학 풀이
- 객체지향적으로 삼각형을 직각삼각형 판단해보기
https://www.acmicpc.net/problem/7510
7510번: 고급 수학
각 테스트 케이스마다 "Scenario #i:"를 출력한다. i는 테스트 케이스 번호이며, 1부터 시작한다. 그 다음 줄에는 입력으로 주어진 삼각형이 직각 삼각형인 경우에는 "yes", 아닌 경우에는 "no"를 출력
www.acmicpc.net
백준 7510번은
난이도 브론즈 등급의 문제로서
테스트 케이스만큼
삼각형의 좌변 우변 그리고 비스듬한 변의 길이가 주어질 때
직각삼각형인지 판단하면 되는 문제입니다.
30분 정도 위에 링크를 방문하셔서 풀어보시고
안 풀리시는 경우에만 아래 해답을 봐주시면 감사하겠습니다.
오늘따라 객체 지향적인 방법으로 풀어보고 싶어서
객체 지향적으로 풀었습니다.
먼저 삼각형의 각 변을 받아주는 클래스 CThreeSides를 선언해주고
입력값을 받고 또한 직각삼각형인지 확인 및 출력하는 메서드들을 가진 CRightTriangle 클래스를 선언했습니다.
int main()
{
CRightTriangle *cRightTriangle = new CRightTriangle();
cRightTriangle->getUserInputTestCase();
cRightTriangle->getUserInputThreeSides();
cRightTriangle->printIsRightTriangle();
delete cRightTriangle;
}
메인에서 깔끔하게 위와 같이 진행되었고
메서드명이 무슨 역할을 하는지 적었기 때문에 따로 주석이나 설명을 다는 게 줄었으며
추후 테스트 케이스를 작성하거나
테스트 케이스를 기반으로 코드를 작성하기 편하게
각 함수는 최대한 짧으면서도 이해하기 편하고 시간 복잡도가 적게 생각하며
작성하였습니다.
전체 코드는 다음과 같습니다
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <array>
#include <stack>
#include <queue>
#include <map>
#include <algorithm>
#include <numeric>
#include <cmath>
#include <regex>
#include <unordered_map>
using namespace std;
class CThreeSides
{
private:
int32_t side1;
int32_t side2;
int32_t side3;
public:
CThreeSides(int32_t side1, int32_t side2, int32_t side3)
{
this->side1 = side1;
this->side2 = side2;
this->side3 = side3;
}
int32_t getSide1()
{
return side1;
}
int32_t getSide2()
{
return side2;
}
int32_t getSide3()
{
return side3;
}
};
class CRightTriangle
{
private:
int32_t testCaseNum;
std::vector<CThreeSides> sidesVec;
public:
CRightTriangle();
~CRightTriangle();
void getUserInputTestCase();
void getUserInputThreeSides();
void printIsRightTriangle();
bool isRightTriangle(CThreeSides cThreeSides);
};
int main()
{
CRightTriangle *cRightTriangle = new CRightTriangle();
cRightTriangle->getUserInputTestCase();
cRightTriangle->getUserInputThreeSides();
cRightTriangle->printIsRightTriangle();
delete cRightTriangle;
}
CRightTriangle::CRightTriangle()
{
testCaseNum = 0;
}
CRightTriangle::~CRightTriangle()
{
}
void CRightTriangle::getUserInputTestCase()
{
std::cin.tie(NULL);
std::ios::sync_with_stdio(false);
std::string inputStr;
std::getline(std::cin, inputStr);
std::stringstream ss(inputStr);
ss >> testCaseNum;
}
void CRightTriangle::getUserInputThreeSides()
{
std::cin.tie(NULL);
std::ios::sync_with_stdio(false);
std::string inputStr;
for (int32_t i = 0; i < testCaseNum; i++)
{
std::getline(std::cin, inputStr);
std::stringstream ss(inputStr);
int32_t side1;
int32_t side2;
int32_t side3;
ss >> side1;
ss >> side2;
ss >> side3;
CThreeSides cThreeSides = { side1,side2,side3 };
sidesVec.push_back(cThreeSides);
}
}
void CRightTriangle::printIsRightTriangle()
{
int idx = 1;
for (const auto& cThreeSides : sidesVec)
{
std::string retVal = "no";
if (isRightTriangle(cThreeSides) == true)
{
retVal = "yes";
}
std::cout << "Scenario #" << idx << ":\n" << retVal << "\n\n";
idx++;
}
}
bool CRightTriangle::isRightTriangle(CThreeSides cThreeSides)
{
int32_t side1Sq = cThreeSides.getSide1() * cThreeSides.getSide1();
int32_t side2Sq = cThreeSides.getSide2() * cThreeSides.getSide2();
int32_t side3Sq = cThreeSides.getSide3() * cThreeSides.getSide3();
bool retBool = false;
if ((side1Sq + side2Sq == side3Sq) ||
(side2Sq + side3Sq == side1Sq) ||
(side1Sq + side3Sq == side2Sq))
{
retBool = true;
}
return retBool;
}
읽어주셔서 감사합니다
무엇인가 얻어가셨기를 바라며
오늘도 즐거운 코딩 하시길 바랍니다 ~ :)
참조 및 인용
C++ Primer
Introduction to Algorithms
https://codemasterkimc.tistory.com/35
C++ 이론을 배울수 있는 곳 정리
개요 C++을 배우는 책, 강의, 블로그, 링크 등을 공유합니다. (링크 및 간략한 설명을 하였으나 만약 원작자가 링크를 거는것을 원치 않을 경우 연락주시기 바랍니다.) 서적 https://www.amazon.com/Prime
codemasterkimc.tistory.com
https://codemasterkimc.tistory.com/50
300년차 개발자의 좋은 코드 5계명 (Clean Code)
이번 글을 통해 배워갈 내용 좋은 코드(Clean Code)를 작성하기 위해 개발자로서 생각해볼 5가지 요소를 알아보겠습니다. 개요 좋은 코드란 무엇일까요? 저는 자원이 한정적인 컴퓨터 세상에서 좋
codemasterkimc.tistory.com
'C++ > C++ 알고리즘' 카테고리의 다른 글
| 백준 5052번 전화번호 목록 C++ 구현해보기 (0) | 2021.10.16 |
|---|---|
| 백준 5054번 주차 C++ 구현해보기 (0) | 2021.10.16 |
| 백준 5800번 성적통계 C++ 구현해보기 (0) | 2021.10.11 |
| 백준 2502번 떡먹는 호랑이 C++ 구현해보기 (0) | 2021.10.07 |
| 백준 2947번 나무조각 C++ 구현해보기 (1) | 2021.10.04 |