근본없는 코딩

[C++] 백준 2단계 조건문 본문

✔ Online Judge

[C++] 백준 2단계 조건문

근본없는 개발자 2023. 6. 9. 20:16

안녕하세요.

오늘은 백준 2단계 조건문을 완료했습니다.

사실 코드 짤 때 조건문 자체는 너무 쉽지만,

이 조건문을 최대한 간단하게 표현하고 싶은데 그게 가장 어려운 듯 합니다.

코테만 아니라면 사실 그냥 생각나는 경우의 수 전부 분기타버리면 편하니까요...

사실 조건문에서 수학의 중요성이 가장 많이 나오는걸지도...ㅎㅎ


#백준1330 두 수 비교하기

#include <iostream>
using namespace std;
int main() {
	int a, b;
	cin >> a >> b;
	if (a > b) cout << ">" << endl;
	else if (a < b) cout << "<" << endl;
	else cout << "==" << endl;
	return 0;
}

✔️ 쌍따옴표와 홀따옴표 차이

. 홀따옴표(' ')는 char 형에 쓰인다. 글자 하나로 치는 경우에만 홀따옴표를 써야 한다.

. 쌍따옴표(" ")는 char*형에 쓰인다. char 하나만 쓰는 것이 아닌 경우라면, 쌍따옴표를 써야한다.

 

 

#백준9498 시험 성적

#include <iostream>
using namespace std;
int main() {
	int score;
	cin >> score;
	if (score >= 90) cout << 'A' << endl;
	else if (score >= 80) cout << 'B' << endl;
	else if (score >= 70) cout << 'C' << endl;
	else if (score >= 60) cout << 'D' << endl;
	else cout << 'F' << endl;
	return 0;
}

 

 

#백준2753 윤년

#include <iostream> 
using namespace std;
int main() {
	int y;
	cin >> y;
	if ((y % 4 == 0 && y % 100 != 0) || y % 400 == 0) cout << 1 << endl;
	else cout << 0 << endl;
	return 0;
}

✔️ 논리연산자

. and: &&

. or: ||

. not: !

 

#백준14681 사분면 고르기

#include <iostream>
using namespace std;
int main() {
	int x, y, res;
	cin >> x >> y;
	if (x > 0) y > 0 ? res = 1 : res = 4;
	else y > 0 ? res = 2 : res = 3;
	cout << res << endl;
	return 0;
}

✔️ 조건연산자

. (조건) ? 참일 경우 표현식 : 거짓일 경우 표현식

. 조건을 적고, 해당 조건이 참일 경우 ? 와 : 사이에 그 결과 시 수행할 표현식을 작성하고, 거짓일 경우 : 뒷부분에 작성한다.

 

#백준2884 알람 시계

#include <iostream>
using namespace std;
int main() {
	int h, m;
	cin >> h >> m;
	if (m >= 45) m -= 45;
	else if (h == 0) h = 23, m += 15;
	else h -= 1, m += 15;
	cout << h << ' ' << m << endl;
	return 0;
}

✔️ 시간 계산

. 45분을 앞당겨야 하므로, 분 값이 45보다 작다면?

시간 계산할 때 1시간을 줄이고, 분 계산에서 현재분에서 +60 -45를 하므로 +15로 계산한다.

#백준2525 오븐 시계

#include <iostream>
using namespace std;
int main() {
	int bef_h, bef_m, use_m;
	int aft_h, aft_m;
	cin >> bef_h >> bef_m >> use_m;

	if (bef_m + use_m % 60 >= 60) {
		aft_h = (bef_h + use_m / 60 + 1) % 24; //1
		aft_m = bef_m + use_m % 60 - 60; //2
	}
	else {
		aft_h = (bef_h + use_m / 60) % 24; //3
		aft_m = bef_m + use_m % 60; //4
	}
	cout << aft_h << ' ' << aft_m << endl;
	return 0;
}

✔️ 시간 계산 2

. 우선 요리하는데 필요한 시간이 분단위로 최대 1000분까지 입력되므로, 시간과 분을 나누기 위해 %를 사용

. 요리하는데 필요한 시간 = 입력된 시간 / 60, 요리하는데 필요한 분 = 입력된시간 % 60

. 조건문: 요리하는데 필요한 분 + 기존 분이 60분이 초과되면, 1시간을 올려줘야하므로 조건문으로 구분

#백준2480 주사위 세개

// max 함수를 사용하지 않은 경우
#include <iostream>
using namespace std;
int main() {
	int a, b, c, prize;
	cin >> a >> b >> c;
	
	if (a == b && b == c) prize = 10000 + a * 1000; //같은 눈 3개
	else if (a != b && a != c && b != c) { //모두 다른 눈
		if (a > b && a > c) prize = a * 100;
		else if (b > c) prize = b * 100;
		else prize = c * 100;
	}
	else {	//같은 눈 2개
		if (a == b || a == c) prize = 1000 + a * 100;
		else prize = 1000 + b * 100;
	}
	cout << prize << endl;
	return 0;
}
// max 함수를 사용한 경우
#include <iostream>
using namespace std;
int main() {
	int a, b, c, prize;
	cin >> a >> b >> c;

	if (a == b && b == c) prize = 10000 + a * 1000; //같은 눈 3개
	else if (a != b && a != c && b != c) prize = max(max(a, b),c) * 100; //모두 다른 눈
	else {	//같은 눈 2개
		if (a == b || a == c) prize = 1000 + a * 100;
		else prize = 1000 + b * 100;
	}
	cout << prize << endl;
	return 0;
}