https://www.acmicpc.net/step/4
if문 단계
점이 어느 사분면에 있는지 알아내는 문제
www.acmicpc.net
https://www.acmicpc.net/problem/1330
1330번: 두 수 비교하기
두 정수 A와 B가 주어졌을 때, A와 B를 비교하는 프로그램을 작성하시오.
www.acmicpc.net
방법 1
#include <iostream>
using namespace std;
int main() {
int a, b;
cin >> a >> b;
if (a > b)
cout << ">" << endl;
else if (a < b)
cout << "<";
else
cout << "==";
return 0;
}
방법 2
#include <iostream>
using namespace std;
int main() {
int a, b;
cin >> a >> b;
cout << ((a>b)? ">" : (a<b)? "<" : "==");
return 0;
}
https://www.acmicpc.net/problem/9498
9498번: 시험 성적
시험 점수를 입력받아 90 ~ 100점은 A, 80 ~ 89점은 B, 70 ~ 79점은 C, 60 ~ 69점은 D, 나머지 점수는 F를 출력하는 프로그램을 작성하시오.
www.acmicpc.net
방법 1
#include <iostream>
using namespace std;
int main() {
int a;
cin >> a;
if (90 <= a && a <= 100)
cout << 'A';
else if (80 <= a && a <= 89)
cout << 'B';
else if (70 <= a && a <= 79)
cout << 'C';
else if (60 <= a && a <= 69)
cout << 'D';
else
cout << 'F';
return 0;
}
방법 2
#include <iostream>
using namespace std;
int main() {
int a;
cin >> a;
if (90 <= a)
cout << 'A';
else if (80 <= a)
cout << 'B';
else if (70 <= a)
cout << 'C';
else if (60 <= a)
cout << 'D';
else
cout << 'F';
return 0;
}
방법 3
#include <iostream>
using namespace std;
int main() {
int a;
cin >> a;
cout << ((a >= 90) ? "A" : (a >= 80) ? "B" : (a >= 70) ? "C" : (a >= 60) ? "D" : "F");
return 0;
}
https://www.acmicpc.net/problem/2753
2753번: 윤년
연도가 주어졌을 때, 윤년이면 1, 아니면 0을 출력하는 프로그램을 작성하시오. 윤년은 연도가 4의 배수이면서, 100의 배수가 아닐 때 또는 400의 배수일 때이다. 예를 들어, 2012년은 4의 배수이면서
www.acmicpc.net
방법 1
#include <iostream>
using namespace std;
int main() {
int a;
cin >> a;
if (a % 4 == 0)
if (a % 400 == 0)
cout << 1;
else if (a % 100 == 0)
cout << 0;
else
cout << 1;
else
cout << 0;
return 0;
}
방법 2
#include <iostream>
using namespace std;
int main() {
int a;
cin >> a;
if ((a % 4 == 0 && a % 100 != 0) || a % 400 == 0)
cout << 1;
else
cout << 0;
return 0;
}
방법 3
#include <iostream>
using namespace std;
int main() {
int a;
cin >> a;
cout << ((a % 4 == 0) ? ((a % 400 == 0) ? 1 : (a % 100 != 0) ? 1 : 0) : 0);
return 0;
}
https://www.acmicpc.net/problem/14681
14681번: 사분면 고르기
점 (x, y)의 사분면 번호(1, 2, 3, 4 중 하나)를 출력한다.
www.acmicpc.net
방법 1
#include <iostream>
using namespace std;
int main() {
int x,y;
cin >> x >>y;
if (x > 0 && y > 0)
cout << 1;
else if (x < 0 && y > 0)
cout << 2;
else if (x < 0 && y < 0)
cout << 3;
else if (x > 0 && y < 0)
cout << 4;
return 0;
}
방법 2
#include <iostream>
using namespace std;
int main() {
int x,y;
cin >> x >>y;
cout << (x > 0 ? (y > 0 ? 1 : 4) : (y > 0 ? 2 : 3));
return 0;
}
https://www.acmicpc.net/problem/2884
2884번: 알람 시계
상근이는 매일 아침 알람을 듣고 일어난다. 알람을 듣고 바로 일어나면 다행이겠지만, 항상 조금만 더 자려는 마음 때문에 매일 학교를 지각하고 있다. 상근이는 모든 방법을 동원해보았지만,
www.acmicpc.net
방법 1
#include <iostream>
using namespace std;
int main() {
int h,m;
cin >> h >> m;
if (m >= 45)
cout << h << ' ' << m - 45;
else if (h != 0)
cout << h - 1 << ' ' << m + 60 - 45;
else
cout << 23 << ' ' << m + 60 - 45;
return 0;
}
방법 2
#include <iostream>
using namespace std;
int main() {
int h,m;
cin >> h >> m;
if (m < 45) {
m += 60;
h--;
if (h < 0) h = 23;
}
cout << h << ' ' << m - 45;
return 0;
}