결론적으로 이야기하면, c++ 사용자가 더 빠른 속도로 입출력을 하기 위해서 사용하는 구문이라고 보면 됩니다.
우선,
ios_base::sync_with_stdio(true); 구문은
c++의 iostream 파일과 c의 stdio 파일을 동기화시켜줘서, 두 구문을 모두 사용할 수 있게 해준다. 하지만, 이렇게 될 경우, c++의 버퍼와 c의 버퍼 둘 다 사용하기 때문에 딜레이가 발생한다고 합니다.
따라서,
이 딜레이를 줄이기 위해
ios_base::sync_with_stdio(false); 구문을 써서
두 파일의 동기화를 끊고, c++의 버퍼만을 사용하게 된다. 이렇게 해서 앞서 말했던 딜레이가 사라지게 되서 조금이라도 더 빠르게 입출력을 할 수 있게 됩니다.
더 빠른 입출력을 위해 해당 구문을 사용한다면, printf, scanf, puts 등 C언어 버퍼의 구문을 사용하게 되면 출력 형식이 잘못될 수가 있습니다.
저의 경우, 해당 구문이 가지는 이유는 모르고, 그냥 사용하면 빠르다는 것만 알고 c언어 버퍼의 구문인 puts()를 사용했는데, visiual studio 2022에서는 잘만 출력이 되는 것이 백준에 코드를 올리면 계속 틀리다고 나와서 매우 난감했습니다.
[백준] 6603번 로또 문제를 풀려고 보니까, [출력 형식이 잘못되었습니다] 결과가 계속 나왔다... 도저히 어떤 것이 틀렸는지 감이 안 왔다.
실제로 출력 결과도 다른 블로그에서와 똑같이 나왔다.
문제의 원인은,
구문 중간에 cout<<"\n"; 대신에 c언어 버퍼를 사용하는 puts()를 사용해서 출력 형식이 계속 잘못됐다고 나왔다.
참고 사이트: ideone
그래서 수정을 하니까 정상적으로 정답 처리 됐다.
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <stdio.h>
#include <string>
#include <string.h>
#include <vector>
#include <algorithm>
#include <math.h>
using namespace std;
int ch[15], a[10], lotto[15], k = 1;
void DFS(int loc, int L) {
if (L == 7) {
for (int i = 1; i <= 6; i++)
cout << a[i] << " ";
puts("");
return;
}
else {
for(int i=loc;i<=k;i++)
if (ch[i] == 0) {
ch[i] = 1;
a[L] = lotto[i];
DFS(i, L + 1);
ch[i] = 0;
}
}
}
int main(int argc, const char* argv[]) {
ios_base::sync_with_stdio(false);
cout.tie(NULL);
cin.tie(NULL);
while (1) {
cin >> k;
if (k == 0)
return 0;
for (int i = 1; i <= k; i++)
cin >> lotto[i];
DFS(1, 1);
puts("");
fill_n(ch, 15, 0);
}
return 0;
}
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <stdio.h>
#include <string>
#include <string.h>
#include <vector>
#include <algorithm>
#include <math.h>
using namespace std;
int ch[15], a[10], lotto[15], k = 1;
void DFS(int loc, int L) {
if (L == 7) {
for (int i = 1; i <= 6; i++)
cout << a[i] << " ";
cout << "\n";
return;
}
else {
for(int i=loc;i<=k;i++)
if (ch[i] == 0) {
ch[i] = 1;
a[L] = lotto[i];
DFS(i, L + 1);
ch[i] = 0;
}
}
}
int main(int argc, const char* argv[]) {
ios_base::sync_with_stdio(false);
cout.tie(NULL);
cin.tie(NULL);
while (1) {
cin >> k;
if (k == 0)
return 0;
for (int i = 1; i <= k; i++)
cin >> lotto[i];
DFS(1, 1);
cout<<"\n";
fill_n(ch, 15, 0);
}
return 0;
}