본문 바로가기
코드트리

[코드트리 조별과제] C++ 기초 : 조건문

by ahrelee 2024. 8. 6.

4. 조건문

if (조건) {
  
  여기에 조건이 참일 경우에만 수행되는 코드 작성

}

 

1. if 조건문

 

1) 음수 구별하기

#include <iostream>
using namespace std;

int main() {
    int n;

    cin >> n;
    
    cout << n << endl;
    if (n < 0)
        cout << "minus";
    return 0;
}

 

2) 큰 수에서 빼기

#include <iostream>
using namespace std;

int main() {
    int a, b;

    cin >> a >> b;
    
    if (a > b)
        cout << a - b;
    if (b > a)
        cout << b - a;
    if (b == a)
        cout << 0;
        
    return 0;
}

 

3) 체질량지수

 

#include <iostream>
using namespace std;

int main() {
    int h, w;
    int b;

    cin >> h >> w;
    
    b = (10000 * w)/(h * h);

    cout << b;
    if (b >= 25)
        cout << endl << "Obesity";

    return 0;
}

 

4) 정사각형의 넓이

#include <iostream>
using namespace std;

int main() {
    int n;
    int area;

    cin >> n;
    
    area = n * n;

    cout << area;
    if (n < 5)
        cout << endl << "tiny";

    return 0;
}

 

2. if else 조건문

 

if (조건) {
  
  여기에 조건이 참일 경우에만 수행되는 코드 작성

}
else {

    여기에 조건이 거짓일 경우에만 수행되는 코드 작성

}

이 위치에 있는 코드는 조건과 무관하게 항상 수행됩니다.

 

1) 시험 통과 여부 확인하기

#include <iostream>
using namespace std;

int main() {
    int n;

    cin >> n;

    if (n >= 80)
        cout << "pass";
    else
        cout << (80 - n) << " more score";
        
    return 0;
}

 

2) 숫자의 조건 여부

#include <iostream>
using namespace std;

int main() {
    int a;

    cin >> a;

    if (a >= 113)
        cout << 1;
    else
        cout << 0;
        
    return 0;
}

 

3) 비교에 따른 연산

#include <iostream>
using namespace std;

int main() {
    int a, b;

    cin >> a >> b;

    if (a > b)
        cout << a * b;
    else
        cout << b / a;
        
    return 0;
}

3. 삼항 연산자

if (조건) {
    a = v1;
}
else {
    a = v2;
}

 

a = 조건 ? v1 : v2;

 

1) 2개 중 최대

#include <iostream>
using namespace std;

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

    cin >> a >> b;

    max = a > b ? a : b;

    cout << max;
    
    return 0;
}

 

2) 삼항연산자

 

#include <iostream>
#include <string>
using namespace std;

int main() {
    int score;
    string str;

    cin >> score;

    str = score == 100 ? "pass" : "failure";

    cout << str;
    
    return 0;
}

 

3) 삼항연산자 2

#include <iostream>
using namespace std;

int main() {
    int a;
    char t_or_f;

    cin >> a;

    t_or_f = a == 1 ? 't' : 'f';

    cout << t_or_f;
    
    return 0;
}

 

4. if elif else 조건문

  • if /  else if / else
if (조건1) {
    코드1
}

else if (조건2) {
   코드2
}

else {
    코드3
}

코드4

 

  • if / else if
if (조건1) {
    코드1
}

else if (조건2) {
   코드2
}

코드4

 

1) 물의 상태

#include <iostream>
using namespace std;

int main() {
    int temp;

    cin >> temp;

    if (temp < 0)
        cout << "ice";
    else if (temp >= 100)
        cout << "vapor";
    else
        cout << "water";
        
    return 0;
}

 

2) 시력 검사 2

#include <iostream>
using namespace std;

int main() {
    double sight;

    cin >> sight;

    if (sight >= 1.0)
        cout << "High";
    else if (sight >= 0.5)
        cout << "Middle";
    else
        cout << "Low";
        
    return 0;
}

 

3) 살 수 있는 물건

#include <iostream>
using namespace std;

int main() {
    int money;

    cin >> money;

    if (money >= 3000)
        cout << "book";
    else if (money >= 1000)
        cout << "mask";
    else
        cout << "no";
        
    return 0;
}

5. if elif elif else 조건문

1) 출석 부르기

 

#include <iostream>
using namespace std;

int main() {
    int num;

    cin >> num;

    if (num == 1)
        cout << "John";
    else if (num == 2)
        cout << "Tom";
    else if (num == 3)
        cout << "Paul";
    else
        cout << "Vacancy";
        
    return 0;
}

 

2) 등급 매기기

 

#include <iostream>
using namespace std;

int main() {
    int score;

    cin >> score;

    if (score >= 90)
        cout << 'A';
    else if (score >= 80)
        cout << 'B';
    else if (score >= 70)
        cout << 'C';
    else if (score >= 60)
        cout << 'D';
    else if (score < 60)
        cout << 'F';
        
    return 0;
}

 

3) 알파벳에 따른 평가

 

#include <iostream>
using namespace std;

int main() {
    char upper;

    cin >> upper;

    if (upper == 'S')
        cout << "Superior";
    else if (upper == 'A')
        cout << "Excellent";
    else if (upper == 'B')
        cout << "Good";
    else if (upper == 'C')
        cout << "Usually";
    else if (upper == 'D')
        cout << "Effort";
    else
        cout << "Failure";

    return 0;
}

 

4) 살 수 있는 물건 2

#include <iostream>
using namespace std;

int main() {
    int n;

    cin >> n;

    if (n >= 3000)
        cout << "book";
    else if (n >= 1000)
        cout << "mask";
    else if (n >= 500)
        cout << "pen";
    else
        cout << "no";

    return 0;
}

 

6. if if 조건문

1) 최대 2번의 연산

 

#include <iostream>
using namespace std;

int main() {
    int a;

    cin >> a;

    // 만약 a가 짝수라면
    if (!(a % 2))
        a /= 2;
    if (a % 2)
        a = (a + 1) / 2;
    
    cout << a;

    return 0;
}

 

2) 숫자의 조건 여부 2

 

#include <iostream>
using namespace std;

int main() {
    int a;

    cin >> a;

    if (a == 5)
        cout << 'A';
    if (!(a % 2))
        cout << 'B';
        
    return 0;
}

 

3) 두 번의 연산

 

#include <iostream>
using namespace std;

int main() {
    int a;

    cin >> a;

    if (a % 2)
        a += 3;
    if (!(a % 3))
        a /= 3;
    
    cout << a;
    
    return 0;
}

 

7. if else if else 조건문

1) 두 숫자의 짝홀 여부

 

#include <iostream>
using namespace std;

int main() {
    int a, b;

    cin >> a >> b;

    if (a % 2)
        cout << "odd" << endl;
    else
        cout << "even" << endl;
    if (b % 2)
        cout << "odd" << endl;
    else
        cout << "even" << endl;
    return 0;
}

 

 

2) 특정 조건 두 정수 비교

 

#include <iostream>
using namespace std;

int main() {
    int a, b;

    cin >> a >> b;

    if (a < b)
        cout << 1 << " ";
    else
        cout << 0 << " ";
    if (a == b)
        cout << 1;
    else
        cout << 0;
    return 0;
}

 

3) 3 또는 5의 배수

 

#include <iostream>
using namespace std;

int main() {
    int a;

    cin >> a;

    if (a % 3 == 0)
        cout << "YES" << endl;
    else
        cout << "NO" << endl;
    if (a % 5 == 0)
        cout << "YES";
    else
        cout << "NO";
    return 0;
}

 

 

8. 비교 연산자와 조건문

1)  비교 연산

 

#include <iostream>
using namespace std;

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

    cin >> a >> b;

    res = a >= b ? 1 : 0;
    cout << res << endl;
    res = a > b ? 1 : 0;
    cout << res << endl;
    res = a <= b ? 1 : 0;
    cout << res << endl;
    res = a < b ? 1 : 0;
    cout << res << endl;
    res = a == b ? 1 : 0;
    cout << res << endl;
    res = a != b ? 1 : 0;
    cout << res << endl;
    
    return 0;
}

 

2) 4가지 관계연산자

 

#include <iostream>
using namespace std;

int main() {
    int a, b;

    cin >> a >> b;

    cout << (a >= b) << endl;
    cout << (a > b) << endl;
    cout << (b >= a) << endl;
    cout << (b > a);
    
    return 0;
}

 

3) 4번의 크기 비교

#include <iostream>
using namespace std;

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

    cin >> a >> b >> c >> d >> e;

    cout << (a > b) << endl;
    cout << (a > c) << endl;
    cout << (a > d) << endl;
    cout << (a > e);
    
    return 0;
}

 

9. and 기호

 

if (조건1 && 조건2) {

    코드1

}

 

1) 점수 비교

 

#include <iostream>
using namespace std;

int main() {
    int A_math, A_eng, B_math, B_eng;

    cin >> A_math >> A_eng >> B_math >> B_eng;

    cout << (A_math > B_math && A_eng > B_eng);
    
    return 0;
}

 

2) 범위 안의 수

 

#include <iostream>
using namespace std;

int main() {
    int a;

    cin >> a;

    cout << (a >= 10 && a <= 20 ? "yes" : "no");
    
    return 0;
}

 

3) 시력 검사

 

#include <iostream>
using namespace std;

int main() {
    double a, b;

    cin >> a >> b;

    if (a >= 1.0 && b >= 1.0)
        cout << "High";
    else if (a >= 0.5 && b >= 0.5)
        cout << "Middle";
    else
        cout << "Low";
        
    return 0;
}

 

4) 특정 조건 세 정수 비교

 

#include <iostream>
using namespace std;

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

    cin >> a >> b >> c;

    cout << (a <= b && a <= c) << ' ';
    cout << (a == b && a == c);
        
    return 0;
}

 

5) 세 정수의 최솟값

 

#include <iostream>
using namespace std;

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

    cin >> a >> b >> c;

    min = a;

    if (b < min)
        min = b;
    if (c < min)
        min = c;
    
    cout << min;
        
    return 0;
}

 

6) 세 수의 중간값

 

#include <iostream>
using namespace std;

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

    cin >> a >> b >> c;

    cout << (b > a && b < c);

    return 0;
}

10. or 기호

if ( 조건1 || 조건2 || 조건3 ) {

    코드1

}

 

1) 범위 밖의 수

 

#include <iostream>
using namespace std;

int main() {
    int a;

    cin >> a;

    if (a < 10 || a > 20)
        cout << "yes";
    else
        cout << "no";
        
    return 0;
}

 

 

2) 숫자의 조건 여부 3

 

#include <iostream>
using namespace std;

int main() {
    int a;

    cin >> a;

    if (a % 13 == 0 || a % 19 == 0)
        cout << "True";
    else
        cout << "False";
        
    return 0;
}

 

3) 둘 중 하나의 배수

 

#include <iostream>
using namespace std;

int main() {
    int a;

    cin >> a;

    cout << (a % 3 == 0 || a % 5 == 0);
    
    return 0;
}

 

11. and, or 혼합

  • and는 or 보다 연산자 우선순위가 높다.

1) 굉장한 숫자

#include <iostream>
using namespace std;

int main() {
    int n;

    cin >> n;

    if (((n % 2) && !(n % 3)) || (!(n % 2) && !(n % 5)))
        cout << "true";
    else
        cout << "false";
         
    return 0;
}

 

2) 숫자의 계절은

 

1. a가 홀수면서 5보다 작으면 A를 출력합니다.
2. a가 홀수면서 5보다 같거나 크면 B를 출력합니다.
3. a가 짝수면 C를 출력합니다.

 

코드화

	if (a % 2 == 1 && a < 5) {
		cout << "A" << endl;
	}
	if (a % 2 == 1 && a >= 5) {
		cout << "B" << endl;
	}
	if (a % 2 == 0) {
		cout << "C" << endl;
	}

 

복잡도 개선

	if (a % 2 == 0) {
		cout << "C" << endl;
	}
	else if (a < 5) {
		cout << "A" << endl;
	}
	else {
		cout << "B" << endl;
	}

 

 

#include <iostream>
using namespace std;

int main() {
    int m;

    cin >> m;

    if (m >= 3 && m <= 5)
        cout << "Spring";
    else if (m >= 6 && m <= 8)
        cout << "Summer";
    else if (m >= 9 && m <= 11)
        cout << "Fall";
    else
        cout << "Winter";
         
    return 0;
}

 

 

#include <iostream>

using namespace std;

int main() {
    // 변수 선언
    int m;

	// 입력
	cin >> m;
    
    // 출력
    if(m >= 12 || m <= 2)
		cout << "Winter";
	else if(m <= 5)
		cout << "Spring";
	else if(m <= 8)
		cout << "Summer";
	else
		cout << "Fall";
	
    return 0;
}

 

3) 장학금

 

#include <iostream>

using namespace std;

int main() {
    int mid, final;

	cin >> mid >> final;
    
    if (mid >= 90)
    {
        if (final == 100 || final >= 95)
		    cout << 100000;
	    else if (final >= 90)
		    cout << 50000;
	    else
		    cout << 0;
    }
    else
        cout << 0;
	
    return 0;
}

 

#include <iostream>

using namespace std;

int main() {
    // 변수 선언
    int m, f;

	// 입력
	cin >> m >> f;
    
    // 출력
    if(m >= 90 && f >= 95)
		cout << "100000";
	else if(m >= 90 && f >= 90)
		cout << "50000";
	else
		cout << "0";
	
    return 0;
}

 

4) 좀 더 어려운 수학 점수

 

#include <iostream>

using namespace std;

int main() {
    int A_math, A_eng, B_math, B_eng;

	cin >> A_math >> A_eng >> B_math >> B_eng;
    
    if (A_math > B_math)
		cout << 'A';
	else if (A_math < B_math)
		cout << 'B';
	else if (A_eng > B_eng)
		cout << 'A';
    else
        cout << 'B';
	
    return 0;
}

 

#include <iostream>

using namespace std;

int main() {
    // 변수 선언
    int a_math, a_eng;
    int b_math, b_eng;

    // 입력
    cin >> a_math >> a_eng;
    cin >> b_math >> b_eng;

    // 출력
    if(a_math > b_math || (a_math == b_math && a_eng > b_eng))
        cout << "A";
    else
        cout << "B";

    return 0;
}

 

5) 두 사람

 

#include <iostream>

using namespace std;

int main() {
    int year1, year2;
    char sex1, sex2; 

    cin >> year1 >> sex1 >> year2 >> sex2;

    cout << ((year1 >= 19 && sex1 == 'M') || (year2 >= 19 && sex2 == 'M'));

    return 0;
}

 

12. 중첩 조건문 

1. a가 홀수라면 숫자 a가 10 이상이면 A, 
                   그렇지 않다면 B를 출력합니다.
2. a가 짝수라면 숫자 a가 15 이상이면 C, 
                   그렇지 않다면 D를 출력합니다.

 

1) 남녀노소 구분짓기

 

#include <iostream>
using namespace std;

int main() {
    int sex, age;

    cin >> sex >> age;

    if (sex == 0)
    {
        if (age >= 19)
            cout << "MAN";
        else
            cout << "BOY";
    }
    else
    {
        if (age >= 19)
            cout << "WOMAN";
        else
            cout << "GIRL";
    }
    
    return 0;
}

 

2) 윤년인가

 

https://www.codetree.ai/missions/4/problems/is-leap-year?&utm_source=clipboard&utm_medium=text

#include <iostream>
using namespace std;

int main() {
    int y;

    cin >> y;

    if (!(y % 100) && (y % 400))
        cout << "false";
    else if (!(y % 4))
        cout << "true";
    else
        cout << "false";

    return 0;
}

 

#include <iostream>
using namespace std;

int main() {
    int y;

    cin >> y;

    if ((!(y % 4) && (y % 100)) || !(y % 400))
        cout << "true";
    else
        cout << "false";

    return 0;
}

 

3) 출력결과 61

 

https://www.codetree.ai/missions/4/problems/reading-k201814?&utm_source=clipboard&utm_medium=text

 

4) 일 수 구하기

 

#include <iostream>
using namespace std;

int main() {
    int n;

    cin >> n;

    if (n == 1 || n == 3 || n == 5 || n == 7 || n == 8 || n == 10 || n == 12)
        cout << 31;
    else if (n == 2)
        cout << 28;
    else
        cout << 30;
        
    return 0;
}

 

https://www.codetree.ai/missions/4/problems/number-of-days-in-month?&utm_source=clipboard&utm_medium=text

 

정수가 2인지 판단 (2월 따로 처리)

정수가 7보다 작거나 같은지, 큰지 판단

7보다 작거나 같은 경우 홀수달은 31일, 짝수달은 30일

7보다 큰 경우 홀수달은 30일, 짝수달은 31일 

 

#include <iostream>

using namespace std;

int main() {
    // 변수 선언
    int n;

	// 입력
	cin >> n;
    
    // 출력
    if(n == 2)
		cout << "28";
	else if(n <= 7) {
    	if(n % 2 == 1)
        	cout << "31";
    	else
        	cout << "30";
	}
	else {
    	if(n % 2 == 0)
        	cout << "31";
    	else
        	cout << "30";
	}
	
    return 0;
}

 

5) 최댓값 구하기

 

#include <iostream>

using namespace std;

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

	cin >> a >> b >> c;

    max = a;
    if (max < b)
        max = b;
    if (max < c)
        max = c;
    
    cout << max;
	
    return 0;
}

 

https://www.codetree.ai/missions/4/problems/maximum-value?&utm_source=clipboard&utm_medium=text

#include <iostream>

using namespace std;

int main() {
    // 변수 선언
    int a, b, c;

	// 입력
	cin >> a >> b >> c;
    
    // 출력
	// a와 b를 비교한뒤, a가 b보다 크다면 a와 c를 비교하여 최댓값을 구합니다.
    if(a >= b) {
		if(a >= c)
			cout << a;
		else
			cout << c; 
	}		

	// a와 b를 비교한 결과가 나와있으므로, b와 c만 비교하여 최댓값을 구합니다.
	else {
		if(b >= c)
			cout << b;
		else
			cout << c;
	}
	return 0;
}

 

 

6) 코로나 메뉴얼

 

#include <iostream>
using namespace std;
char manuel(char sym, int temp);

int main() {
    char sym1, sym2, sym3;
    char res1, res2, res3;
    int temp1, temp2, temp3;

    cin >> sym1 >> temp1;
    cin >> sym2 >> temp2;
    cin >> sym3 >> temp3;

    res1 = manuel(sym1, temp1);
    res2 = manuel(sym2, temp2);
    res3 = manuel(sym3, temp3);

    if ((res1 == res2 && res1 == 'A') || (res2 == res3 && res2 == 'A') || (res3 == res1 && res3 == 'A') || (res1 == res2 && res2 == res3 && res1 == 'A'))
        cout << 'E';
    else
        cout << 'N';

    return 0;
}

char manuel(char sym, int temp)
{
    char res;

    if (temp >= 37)
    {
        if (sym == 'Y')
            res = 'A';
        else
            res = 'B';
    }
    else
    {
        if (sym == 'Y')
            res = 'C';
        else
            res = 'D';
    }
    return (res);
}

 

https://www.codetree.ai/missions/4/problems/covid-manual?&utm_source=clipboard&utm_medium=text

 

첫번째 사람이 'A' 인 경우와 아닌 경우로 나눈다.

1 == A 라면, 2 == A || 3 == A 인 경우를 찾는다.

1 != A 라면, 2 == A && 3 == A 인 경우를 찾는다.

 

#include <iostream>

using namespace std;

int main() {
    // 변수 선언
    char c1, c2, c3;
    int t1, t2, t3;
    
    // 입력
    cin >> c1 >> t1;
    cin >> c2 >> t2;
    cin >> c3 >> t3;
    
    // A가 2명 이상인지 판단하기
    if(c1 == 'Y' && t1 >= 37) {
        // 첫 번째 사람이 A라면, 남은 두 사람 중 한 사람이라도 A면 됩니다.
        if((c2 == 'Y' && t2 >= 37) || (c3 == 'Y' && t3 >= 37))
            cout << "E";
        else
            cout << "N";
    }
    else {
        // 첫 번째 사람이 A가 아니라면, 남은 두 사람 모두 A여야만 합니다.
        if((c2 == 'Y' && t2 >= 37) && (c3 == 'Y' && t3 >= 37))
            cout << "E";
        else
            cout << "N";
    }
    return 0;
}

 

7) 중앙값 구하기

 

https://www.codetree.ai/missions/4/problems/find-the-median?&utm_source=clipboard&utm_medium=text

#include <iostream>
using namespace std;

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

    cin >> a >> b >> c;

    if ((a > b && b > c) || (c > b) && (b > a))
        cout << b;
    else if ((b > a && a > c) || (c > a) && (a > b))
        cout << a;
    // else if ((a > c && c > b) || (b > c) && (c > a))
    else
        cout << c;

    return 0;
}