<C++> 기하 : 직사각형과 삼각형

긍이·2023년 12월 3일
0

C++

목록 보기
12/12

오늘은 백준 알고리즘 10단계 기하: 직사각형과 삼각형을 풀어볼 것이다!

📌 기하 : 직사각형과 삼각형

✔ 직사각형

#include <iostream>


using namespace std;

int main()
{
	int A, B;

	cin >> A >> B;

	cout << A * B;
}

4
4
16

✔ 직사각형에서 탈출

#include <iostream>

using namespace std;

int main()
{
	int x, y, w, h;
	cin >> x >> y >> w >> h;

	int minx = 0;
	int miny = 0;
	if (w - x < x) minx = w - x;
	else minx = x;

	if (h - y < y) miny = h - y;
	else miny = y;

	if (minx > miny) cout << miny;
	else cout << minx;
}

55 55 56 56
1

✔ 네 번째 점

XOR 연산자

^ : XOR 연산자 두 값이 다른지 확인
ex) x = 30^0 - 결과: 30 (두 값이 다른지에 대해 참)
x = 30^30 - 결과: 0 (두 값이 다른지에 대해 거짓)

if에서 사용할 경우

int x1 = 30;
int y1 = 50;

int x2 = 20;
int y2 = 20;

if(x1 ^ y1) cout << "1";
else cout << "0";
if(x2 ^ y2) cout << "1";
else cout << "0";

1
0

값을 넣을때

int x1 = 30;
int y1 = 50;

int x2 = 20;
int y2 = 20;

int a = x1 ^ x2 ^ y2;
int b = x1 ^ y1 ^ y2; // 비교하는 값 모두 다르고 0이 없는 경우는 쓰레기 값이 출력됨
cout << a <<"\n" << b;

30
56

#include <iostream>

using namespace std;

int main()
{
    int arr[3][2]{0,};
    for (int i = 0; i < 3; i++)
    {
        for (int j = 0; j < 2; j++)
        {
            cin >> arr[i][j];
        }
    }

    int x, y;

    x = arr[0][0]^arr[1][0]^arr[2][0];
    y = arr[0][1]^arr[1][1]^arr[2][1];

    cout << x << " " << y;
}

5 7
7 5
7 5
5 7

✔ 수학은 체육과목입니다

자꾸 틀려서 뭔가 했더니
n = 10^9 라서 long int로 받아서 하니깐 됐다

#include <iostream>

using namespace std;

int main()
{
	long int n,x;

	cin >> n;

	x = n * 4;

	cout << x;
}

10000000
40000000

✔ 대지

#include <iostream>

using namespace std;

int main()
{
	int N;
	cin >> N;

	int xM = -10001;
	int xN = 10001;
	int yM = -10001;
	int yN = 10001;

	for (int i = 0; i < N; i++)
	{
		int x, y;
		cin >> x >> y;

		if (xM < x) xM = x;
		if (xN > x) xN = x;

		if (yM < y) yM = y;
		if (yN > y) yN = y;

	}

	if (N <= 1) cout << 0;
	else
	{
		int X, Y;

		X = xM - xN;
		Y = yM - yN;

		cout << X * Y;
	}
}

2
-100 -100
0 0
10000

✔ 삼각형 외우기

정답으로 나왔는데 틀린 문제

#include <iostream>

using namespace std;

int main()
{
	int a, b, c;

	cin >> a >> b >> c;


	if (a + b + c != 180) cout << "Error";
	else
	{
		if (!(a ^ b ^ c ^ 60)) cout << "Equilateral"; // 문제의 코드..ㅎㅎ
		else if (a != b && a != c && b != c) cout << "Scalene";
		else cout << "Isosceles";
	}
}

80
80
20
Isosceles

될 줄 알고 작성해봤고 실제로 빌드했을 때 출력도 정상적으로 나오길래 되는 줄 알았는데
이 다음 문제도 이 코드를 응용해서 풀고있었는데
자꾸 값이 이상하게 뜨길래 혹시나 해서 다시 테스트를 해봤다

if (!(a ^ b ^ c^5)) cout << "테스트 O";
else cout << "테스트 X";

입력 5 2 5 넣으면 '테스트 X' 나오는데
입력 2 5 2 넣으면 '테스트 O' 나온다..ㅎㅎ
운이 좋았었나보다?
10101번 게시판 보니깐 틀렸는데 정답으로 뜬다는 글이 몇개 있다

다시 작성해서 제출했다

#include <iostream>

using namespace std;

int main()
{
	int a, b, c;

	cin >> a >> b >> c;


	if (a + b + c != 180) cout << "Error";
	else
	{
		if (a == b && a == c) cout << "Equilateral";
		else if (a != b && a != c && b != c) cout << "Scalene";
		else cout << "Isosceles";
	}
}

✔ 삼각형과 세 변

#include <iostream>
#include <list>

using namespace std;

int main()
{
	list<string> slist;

	while (true)
	{
		int a, b, c, M = 0;
		cin >> a >> b >> c;

		if ((a == b && a == c && a == 0))
			break;

		bool check = false;
		string s = "";
		if (a >= b && a >= c) M = a;
		else if (b >= a && b >= c) M = b;
		else M = c;

		if (M >= (a + b + c) - M)
		{
			s = "Invalid";
		}
		else
		{
			if (a == b && b == c) s = "Equilateral";
			else if (a != b && a != c && b != c)  s = "Scalene";
			else s = "Isosceles";
		}
		slist.push_back(s);
	}

	int size = slist.size();

	for (int i = 0; i < size; i++)
	{
		cout << slist.front()<<"\n";
		slist.pop_front();
	}
}

1 1 1
1 1 2
1 2 2
2 2 2
1 2 3
0 0 0

✔ 세 막대

#include <iostream>
#include <algorithm>

using namespace std;

int main()
{
	int arr[3]{0,};
	
	for (int i = 0; i < 3; i ++)
	{
		int  a;
		cin >> a;

		arr[i] = a;
	}

	sort(arr, arr + 3);

	int min = arr[0];
	int max = arr[1];
	int p = 0;
	if (min == max && min == arr[2])
	{
		p = max * 3;
	}
	else if (min+ max > arr[2])
	{
		p = min + max + arr[2];
	}
	else
	{
		p = min + max +(min + max - 1);
	}
	cout << p;
}

50 50 60
160

0개의 댓글

관련 채용 정보