근본없는 코딩
[C++] 백준 3단계 반복문 본문

안녕하세요.
금요일이라 그런지, 피곤하네요...
그래도 금요일이니 오늘치 얼른 풀고 게임하러 가려고 합니다!!
언제까지 쉬운 문제가 나올지 모르겠네요.
일단 아직까지는 너무 난이도가 낮아서
그냥 손가락 연습 정도...!
곧 막혀서 1일 1문제도 못하는 날이 오겠죠 ㅠㅠ
난이도 높은 문제도 1일1문제 할 수있는 날이 올 때까지 파이팅...!
#백준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;
}
return 0;
}
#백준10950 A+B-3
#include <iostream>
using namespace std;
int main() {
int t, a, b;
cin >> t;
for (int i = 0; i < t; i++) {
cin >> a >> b;
cout << a + b << endl;
}
return 0;
}
#백준8393 합
#include <iostream>
using namespace std;
int main() {
int n, res=0;
cin >> n;
for (int i = 1; i <= n; i++) {
res += i;
}
cout << res << endl;
return 0;
}
#백준25304 영수증
#include <iostream>
using namespace std;
int main() {
int x, n, a, b, res = 0;
cin >> x >> n;
for (int i = 0; i < n; i++) {
cin >> a >> b;
res = res + a * b;
}
cout << (res == x ? "Yes" : "No") << endl;
return 0;
}
#백준25314 코딩은 체육과목 입니다
#include <iostream>
using namespace std;
int main() {
int n, len = 0;
char str[1300] = "";
cin >> n;
for (int i = 0; i < n/4; i++) {
str[len++] = 'l';
str[len++] = 'o';
str[len++] = 'n';
str[len++] = 'g';
str[len++] = ' ';
}
str[len++] = 'i';
str[len++] = 'n';
str[len++] = 't';
cout << str << endl;
return 0;
}
#백준15552 빠른 A+B
#include <iostream>
using namespace std;
int main() {
ios::sync_with_stdio(false); cin.tie(NULL);
int t, a, b;
cin >> t;
for (int i = 0; i < t; i++) {
cin >> a >> b;
cout << a + b << '\n';
}
return 0;
}
✔️ 빠른 입출력
- C++을 사용하고 있고 cin/cout을 사용하고자 한다면, cin.tie(NULL)과 sync_with_stdio(false)를 둘 다 적용해 주고, endl 대신 개행문자(\n)를 쓰자. 단, 이렇게 하면 더 이상 scanf/printf/puts/getchar/putchar 등 C의 입출력 방식을 사용하면 안 된다.
- 아래 얘기는 cin, cout을 쓸 때의 얘기지, scanf/prinf로 입출력을 하고자 하신다면 그냥 쓰시면 됩니다. scanf/printf는 충분히 빠릅니다.
- endl은 개행문자를 출력할 뿐만 아니라 출력 버퍼를 비우는 역할까지 합니다. 그래서 출력한 뒤 화면에 바로 보이게 할 수 있는데, 그 버퍼를 비우는 작업이 매우 느립니다. 게다가 온라인 저지에서는 화면에 바로 보여지는 것은 중요하지 않고 무엇이 출력되는가가 중요하기 때문에 버퍼를 그렇게 자주 비울 필요가 없습니다. 그래서 endl을 '\n'으로 바꾸는 것만으로도 굉장한 시간 향상이 나타납니다.
- cin.tie(NULL)은 cin과 cout의 묶음을 풀어 줍니다. 기본적으로 cin으로 읽을 때 먼저 출력 버퍼를 비우는데, 마찬가지로 온라인 저지에서는 화면에 바로 보여지는 것이 중요하지 않습니다. 입력과 출력을 여러 번 번갈아서 반복해야 하는 경우 필수적입니다.
- ios_base::sync_with_stdio(false)는 C와 C++의 버퍼를 분리합니다. 이것을 사용하면 cin/cout이 더 이상 stdin/stdout과 맞춰 줄 필요가 없으므로 속도가 빨라집니다. 단, 버퍼가 분리되었으므로 cin과 scanf, gets, getchar 등을 같이 사용하면 안 되고, cout과 printf, puts, putchar 등을 같이 사용하면 안 됩니다.
#백준11021 A+B-7
#include <iostream>
using namespace std;
int main() {
int t, a, b;
cin >> t;
for (int i = 1; i <= t; i++) {
cin >> a >> b;
cout << "Case #" << i << ": " << a + b << endl;
}
return 0;
}
#백준11022 A+B-8
#include <iostream>
using namespace std;
int main() {
int t, a, b;
cin >> t;
for (int i = 1; i <= t; i++) {
cin >> a >> b;
cout << "Case #" << i << ": " << a << " + " << b << " = " << a + b << endl;
}
return 0;
}
#백준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 << endl;
}
return 0;
}
#백준2439 별 찍기-2
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n-i-1; j++) {
cout << ' ';
}
for (int j = 0; j <= i; j++) {
cout << '*';
}
cout << '\n';
}
return 0;
}
#백준10952 A+B-5
#include <iostream>
using namespace std;
int main() {
int a, b;
while (true) {
cin >> a >> b;
if (a == b && a == 0) break;
else cout << a + b << '\n';
}
return 0;
}
#백준10951 A+B-4
#include <iostream>
using namespace std;
int main() {
int a, b;
while (true) {
cin >> a >> b;
if (cin.eof() == true) break;
else cout << a + b << '\n';
}
return 0;
}
✔️ EOF
. EOF(End of File) : 파일의 끝, 더 이상 읽을 데이터가 없다
. cin으로 입력을 받으려고 할 때, EOF라면 입력이 취소되고 cin.eof()는 true를 반환한다.이를 이용하여 파일이 종료될 때까지 입력을 받는 코드를 작성할 수 있다.
. 터미널(콘솔)에서는 EOF를 수동으로 넣어 주어야 한다.
'✔ Online Judge' 카테고리의 다른 글
| [C++] 백준 10871 X보다 작은 수 (0) | 2023.06.09 |
|---|---|
| [C++] 백준 10807 개수 세기 (0) | 2023.06.09 |
| [C++] 백준 2단계 조건문 (1) | 2023.06.09 |
| [C++] 백준 1단계 입출력과 사칙연산 (0) | 2023.05.29 |
| [알고리즘] 백준 온라인 저지로 알고리즘 초보 공부 시작하기 (0) | 2023.05.29 |