Skip to main content

读写优化

参考资料

关闭同步 & 解除关联

ios::sync_with_stdio(false);
cin.tie(nullptr);
warning

关闭同步后,cin/coutscanf/printf 不能混用,否则可能会导致未定义行为(UB),从而引发读写顺序错误。

快读快写

int read()
{
int x=0,f=1;char c=getchar();
while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
while(c>='0'&&c<='9')x=x*10+c-48,c=getchar();
return x*f;
}
void write(int x)
{
if(x<0)putchar('-'),x=-x;
if(x>9)write(x/10);
putchar(x%10+48);
}

例题

输入 nn 个整数 aia_i,输出它们的和。(ain108|a_i| \le n \le 10^8

Code (1)
#include <bits/stdc++.h>
using namespace std;

using ll=long long;
ll read()
{
ll x=0,f=1;char c=getchar_unlocked();
while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar_unlocked();}
while(c>='0'&&c<='9')x=x*10+c-48,c=getchar_unlocked();
return x*f;
}
void write(ll x)
{
if(x<0)putchar('-'),x=-x;
if(x>9)write(x/10);
putchar(x%10+48);
}
int main()
{
int n=read();
ll ans=0;
for(int i=1;i<=n;i++)
{
ll x=read();
ans+=x;
}
write(ans);
return 0;
}