이번 단계는 반복문이다.
반복을 알맞게 수행하면서도 읽기 쉬운 코드를 만드는 것이 관건이라고 생각한다.
https://www.acmicpc.net/problem/2739
2739번 구구단
나의 풀이
#include <iostream>
using namespace std;
int main()
{
int N;
cin >> N;
for (int i = 1; i < 10; ++i) {
cout << N << " * " << i << " = " << N * i << endl;
}
}
https://www.acmicpc.net/problem/10950
10950번 A+B - 3
나의 풀이
#include <iostream>
using namespace std;
int main()
{
int N;
cin >> N;
for (int i = 0; i < N; ++i) {
int A, B;
cin >> A >> B;
cout << A + B << endl;
}
}
https://www.acmicpc.net/problem/8393
8393번 합
나의 풀이
#include <iostream>
using namespace std;
int main()
{
int N;
cin >> N;
int sum{ 0 };
for (int i = 1; i <= N; ++i) {
sum += i;
}
cout << sum;
}
https://www.acmicpc.net/problem/25304
25304번 영수증
나의 풀이
#include <iostream>
using namespace std;
int main()
{
int X, N;
cin >> X >> N;
int sum{ 0 };
for (int i = 0; i < N; ++i) {
int a, b;
cin >> a >> b;
sum += a * b;
}
if (X == sum) {
cout << "Yes";
}
else {
cout << "No";
}
}
https://www.acmicpc.net/problem/25314
25314번 코딩은 체육과목 입니다
나의 풀이
#include <iostream>
#include <string>
using namespace std;
int main()
{
int N;
cin >> N;
string str{};
str.reserve(1000);
while (N > 0) {
N -= 4;
str = str + "long ";
}
cout << str + "int";
}
https://www.acmicpc.net/problem/15552
15552번 빠른 A+B
나의 풀이
#include <iostream>
using namespace std;
int main()
{
cin.tie(NULL);
cin.sync_with_stdio(false);
cout.sync_with_stdio(false);
int T;
cin >> T;
for (int i = 0; i < T; ++i) {
int a, b;
cin >> a >> b;
cout << a + b << '\n';
}
}
입출력 방식에 시간을 최대한 절약하기 위한 방법을 자연히 배울 수 있는 문제이다.
cin과 cout은 tie되어 있는 것이 default 설정인데, 이를 cin.tie(NULL); 로 untie를 함으로서 속도를 향상시킬 수 있다.
tie가 되어있다면 두 스트림이 다른 스트림에서 작업이 요청되면 작업한 내용을 flush 하기 때문에 이 작업을 하지 않도록 하여 속도 향상 효과를 얻을 수 있는 것이다.
sync_with_stdio(false) 는 표준 C++ 스트림이 표준 C 스트림에 동기화 되는지 여부를 설정하는 함수로 인자를 false로 주었기 때문에 동기화하지 않는 코드이다. 동기화가 off 된다면 C++ 표준 스트림은 I/O를 독립적으로 버퍼링하도록 하고 상당히 빠를 수 있다. 그러나 이는 C++과 C의 I/O를 혼합하지 않도록 주의해야 한다.
std::endl 보다 '\n' 개행문자를 사용하는 것이 훨씬 빠르다. endl은 flush 까지 하는 것이 포함되어 있는 것이고, 개행 문자는 단순히 개행만 하기 때문에 속도가 더 빠르다.
이와 같이 입출력 속도를 빠르게 하여 반복문에서의 입출력 시간 초과를 방지할 수 있는 방법을 알 수 있었다.
https://www.acmicpc.net/problem/11021
11021번 A+B - 7
나의 풀이
#include <iostream>
using namespace std;
int main()
{
cin.tie(NULL);
cin.sync_with_stdio(false);
cout.sync_with_stdio(false);
int T;
cin >> T;
for (int i = 0; i < T; ++i) {
int a, b;
cin >> a >> b;
cout << "Case #" << i + 1 << ": " << a + b << '\n';
}
}
https://www.acmicpc.net/problem/11022
11022번 A+B - 8
나의 풀이
#include <iostream>
using namespace std;
int main()
{
cin.tie(NULL);
cin.sync_with_stdio(false);
cout.sync_with_stdio(false);
int T;
cin >> T;
for (int i = 1; i <= T; ++i) {
int a, b;
cin >> a >> b;
cout << "Case #" << i << ": " << a << " + " << b << " = " << a + b << '\n';
}
}
https://www.acmicpc.net/problem/2438
2438번 별 찍기 - 1
나의 풀이
#include <iostream>
using namespace std;
int main()
{
int N;
cin >> N;
for (int i = 0; i < N; ++i) {
for (int j = 0; j <= i; ++j) {
cout << "*";
}
cout << '\n';
}
}
https://www.acmicpc.net/problem/2439
2439번 별 찍기 - 2
나의 풀이
#include <iostream>
using namespace std;
int main()
{
int N;
cin >> N;
for (int i = 0; i < N; ++i) {
for (int j = 1; j < N - i; ++j) {
cout << ' ';
}
for (int j = 0; j <= i; ++j) {
cout << "*";
}
cout << '\n';
}
}
https://www.acmicpc.net/problem/10952
10952번 A+B - 5
나의 풀이
#include <iostream>
using namespace std;
int main()
{
while (true) {
int a, b;
cin >> a >> b;
if ((a == b) && (a == 0)) break;
cout << a + b << '\n';
}
}
https://www.acmicpc.net/problem/10951
10951번 A+B - 4
나의 풀이
#include <iostream>
using namespace std;
int main()
{
ios::sync_with_stdio(false);
cin.tie(NULL);
int a, b;
while (cin >> a >> b) {
cout << a + b << endl;
}
}
cin과 cout 각각 동기화를 해제하지 않고 ios:: 에서 호출하여 한 번에 동기화를 해제하도록 했다.
** 오류 지적, 조언 환영합니다 **
'코테' 카테고리의 다른 글
백준 단계별로 풀어보기 (6) (1) | 2024.09.25 |
---|---|
백준 단계별로 풀어보기 (5) - 문자열 (0) | 2024.09.09 |
백준 단계별로 풀어보기 (4) (0) | 2024.09.06 |
백준 단계별로 풀어보기 (2) (0) | 2024.08.31 |
백준 단계별로 풀어보기 (1) (0) | 2024.08.30 |