본문 바로가기
백준

백준 단계 2. 조건문

by 템닉___ 2024. 3. 17.

이 포스팅은 템닉의 개발저장소의 레거시 글입니다.

 

템닉의 개발저장소

기억 못할 수도 있는 지식들을 덤프해버리는 공간입니다.

tempnixk.tistory.com

 

1. 두 수 비교하기 (문제번호 1330)

#include <iostream>
using namespace std;

int main() {
	short a, b;
  cin >> a >> b;

  if (a>b) {
  cout << ">" <<endl;
  }  
  else if (a < b) {
  cout << "<" << endl;
  }
  else {
  cout << "==" << endl;
  }

}

2. 시험 성적 (문제번호 9498)

#include <iostream>
using namespace std;

int main() {
	short 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;
  }
}

복붙하다가 } 하나 빼먹어서 컴파일 오류 났다....

 

 

3. 윤년 (문제번호 2753)

#include <iostream>
using namespace std;

int main() {
  short leap;
  short multiple4;
  short multiple100;
  short multiple400;
  cin >> leap;
  multiple4 = leap % 4 == 0;
  multiple100 = leap % 100 == 0;
  multiple400 = leap % 400 == 0;
  if (multiple4 && !multiple100 || multiple400) {
    cout << 1 << endl;
  }
  else {
    cout << 0 << endl;
  }
}

처음에 if 안의 로직을 어떻게 짜야할지 고민을 많이했다. if 중첩을 쓰는게 가장 먼저 생각났지만, 뭔가 성능상 안좋을것 같아서 고민했었다. 지금 찾아보니 if는 중첩해도 성능에 크게 영향이 없고, for만 중첩하지 않으면 된다고 한다.... 다른 코드들 참조해봐도 메모리, 시간 다 똑같이 나온다. C로 짰을때 메모리 줄어들긴 하는데 cout과 printf의 차이인가..? 시간상으로는 printf가 cout보다는 빠르다고 한다.

 

4. 사분면 고르기 (문제번호 14681)

#include <iostream>
using namespace std;

int main() {
  short x, y;
  cin >> x;
  cin >> y;
  if (x * y > 0) {
    if (x > 0) {
      cout << 1 << endl;
    }
    else {
      cout << 3 << endl;
    }
  }
  else {
    if (x > 0) {
      cout << 4 << endl;
    }
    else {
      cout << 2 << endl;
    }
  }
}

삼항 연산자로 푼 사람들도 있었다.

y>0 ? printf("1") : printf("4"); 처럼 사용했는데 어느 코드가 더 좋을지는 모르겠다.

 

5. 알람 시계 (문제번호 2884)

#include <iostream>
using namespace std;

int main() {
  unsigned short H, M;
  
  cin >> H;
  cin >> M;
  
  if (M > 45) {
    cout << H << " " << M-45 << endl;
  }
  else {
    if ( H > 0 ) {
      cout << H - 1 << " " << M + 15 << endl;
    }
    else {
      cout << 23 << " " << M + 15 << endl;
    }
  }
}

실패한 코드이다. 처음에 틀렸을때 short를 unsigned 로 바꿨는데 여전히 틀린 코드로 나온다. 이유를 찾는중...

#include <iostream>
using namespace std;

int main() {
  unsigned short H, M;
  
  cin >> H;
  cin >> M;
  
  if (M >= 45) {
    cout << H << " " << M-45 << endl;
  }
  else {
    if ( H > 0 ) {
      cout << H - 1 << " " << M + 15 << endl;
    }
    else {
      cout << 23 << " " << M + 15 << endl;
    }
  }
}

반례가 있었다. 0 45를 입력 시, 12 60 이 출력되어 잘못된 출력값이 나왔다.
따라서 45분일때에도 똑같이 첫 if문에 들어갈 수 있도록 범위를 수정해줘야 했었다.

 

6. 오븐 시계 (문제번호 2525)

#include <iostream>
using namespace std;

int main() {
  unsigned short hour, minute, count;
  cin >> hour;
  cin >> minute;
  cin >> count;

  cout << (hour + (count+minute) / 60) % 24 << " " << (minute + count) % 60 << endl;
}

2884 문제처럼 하기에는 너무 복잡한것 같아서 조금 더 생각을 해봤다. 연산으로도 충분히 풀리는 문제였고, 2884번도 이런 식으로풀어낼 수 있을 것 같다고 느꼈다.

 

7. 주사위 세개 (문제번호 2480)

#include <iostream>
using namespace std;

int main() {
  unsigned short a, b, c;
  cin >> a;
  cin >> b;
  cin >> c;

  if (a == b && b == c && c == a) {
    cout << 10000 + a * 1000 << endl; 
  }
  else if (( a == b || b == c || c == a) && !(a==b==c) ) {
    if(a==b)
      cout << 1000 + a * 100 << endl;
    else if (b==c)
      cout<< 1000 + b * 100 << endl;
    else
      cout << 1000 + c * 100 << endl;
  }
  else {
    cout << max(max(a,b), max(b,c)) * 100 << endl;
  }
  return 0;
};

같은지 다른지 판별하는 STL이 있을 것 같긴 한데 일단은 머릿속에서 처음에 생각나는 대로 풀었다. max도 안쓰려고 했는데 너무 구현하기가 더러워질거 같아서 사용하게 되었다. 정렬해서 하는 것도 중간에 생각나긴 했다. 큰 순서대로 a,b,c 에 값을 넣어볼까 생각해봤다.

조건문을 깔끔하게 짜지 못했다. 다른 답안들 보니 알게되었다.

 

#include <iostream>
using namespace std;

int main() {
  unsigned short a, b, c;
  cin >> a;
  cin >> b;
  cin >> c;

  if (a == b && b == c ) {
    cout << 10000 + a * 1000 << endl; 
  }
  else if ( a == b || c == a ) {
    cout << 1000 + a * 100;
  }
  else {
    cout << max(max(a, b), c) * 100 << endl;
  }
  return 0;
};