问题描述
#include <iostream>
using namespace std;
inline void fast(int &n)
{
n = 0;
register int c;
bool negative = false;
c = getchar();
while((c < 48 || c > 57) && c != '-' )
c = getchar();
if(c == '-')
{
negative = true;
c = getchar();
}
while(c > 47 && c < 58)
{
n = (n<<1) + (n<<3)+ c - 48;
c = getchar();
}
if(negative)
n *= -1;
}
int main ()
{
ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
int a,b;
string s;
fast(a);
fast(b);
cin >> s;
cout << a << " " << b << "\n";
cout << s;
return 0;
}
我在竞争性编程中使用getchar()以及其他使用CIN快速输入的整数来编写此代码。
输入:
1。 1 2
你好
(输入不在一行中) 这行得通,在输入“ hello”之前先按'enter'。
2。。1 2你好
(单行输入) 在按“ Enter”键后不起作用。必须再次输入。如果没有ios_base :: sync_with_stdio(false),它可以很好地工作。 通过删除同步,这是 BREAKING 代码。
有什么方法可以解决这个问题,并使1 2 HELLO工作 以最快的速度投入 或建议任何其他方法或代码!!!
也可以请您提出“ INTEGERS的输入和输出的最快方法以及竞争性编程的STRINGS”!
解决方法
这发生在我一个月前。您不应在cin.tie() and cout.tie()
中使用NULL,而应在其中使用'0'。
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
这应该工作正常。