c++中是自带输入的(废话),目前c++中有两种输入方法:
int main(){
int n;
cin>>n;
return 0;
}
这种方法的优点是代码简洁,适合初学者。可是它太慢了。(有一次一道题我用cin结果输入就花了1s多);
int main(){
int n;
scanf("%d",n);
return 0;
}
这种方法相比cin快多了!可是有些毒瘤会超时。。。
当然,cin可以用std::ios::sync_with_stdio(false)来解决它输入慢的问题。
但是在noip中会爆零。因为noip只能用freopen,而freopen是stdio包含的,而std::ios::sync_with_stdio用了就不能用stdio。
手写快速读入的速度
首先声明一点:快读不是万能的,有些毒瘤会卡read。但,懂总比不懂好。
快读的原理是getchar()比cin和scanf快。所以,快读就是把输入的字符转为int。
inline long long read(){ long long ans=0; char last=' ',ch=getchar(); while(ch<'0' || ch>'9')last=ch,ch=getchar(); while(ch>='0' && ch<='9')ans=ans*10+ch-'0',ch=getchar(); if(last=='-')ans=-ans; return ans; }
__int128的读写
__int128是c++中最大整型变量,存储范围为-2^127~2^127-1
但是__int128变量使用cin和scanf会报错:
tempCodeRunnerFile.cpp: In function 'int main()':
tempCodeRunnerFile.cpp:5:8: error: no match for 'operator>>' (operand types are 'std::istream' {aka 'std::basic_istream<char>'} and '__int128')
cin>>n;
~~~^~~
In file included from C:/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/sstream:38,
from C:/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/complex:45,
from C:/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/ccomplex:39,
from C:/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/x86_64-w64-mingw32/bits/stdc++.h:52,
from tempCodeRunnerFile.cpp:1:
C:/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/istream:120:7: note: candidate: 'std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(std::basic_istream<_CharT, _Traits>::__istream_type& (*)(std::basic_istream<_CharT, _Traits>::__istream_type&)) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>]' <near match>
operator>>(__istream_type& (*__pf)(__istream_type&))
^~~~~~~~
C:/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/istream:120:7: note: conversion of argument 1 would be ill-formed:
tempCodeRunnerFile.cpp:5:10: error: invalid conversion from '__int128' to 'std::basic_istream<char>::__istream_type& (*)(std::basic_istream<char>::__istream_type&)' {aka 'std::basic_istream<char>& (*)(std::basic_istream<char>&)'} [-fpermissive]
cin>>n;
这时候,只能用快读才能输入输出。。。(我也是因为这才了解快读的)
a+b problem with __int128:
#include <bits/stdc++.h>
using namespace std;
inline __int128 read(){
__int128 x=0,f=1;
char ch=getchar();
while(ch<'0'||ch>'9'){
if(ch=='-')
f=-1;
ch=getchar();
}
while(ch>='0'&&ch<='9'){
x=x*10+ch-'0';
ch=getchar();
}
return x*f;
}
inline void print(__int128 x){
if(x<0){
putchar('-');
x=-x;
}
if(x>9)
print(x/10);
putchar(x%10+'0');
}
int main(void){
__int128 a = read();
__int128 b = read();
print(a + b);
cout<<endl;
return 0;
}
p.s:noip 不可用__int128
这篇就结束了,下次再见。
审核通过
并不是,使用Iossyncwithcstdio以后noip不会爆零,你这部分讲的很奇怪
但是这时候如果同时使用cincout和scanfprintf的话,会爆零
所以用iossyncwithstdio的时候只能用cincout,但不会爆零
@NSObject 23786 打了这条语句以后不能混用
@NSObject 23786 freopen是在<stdio.h>头文件的。
ios::sync_with_stdio是关闭了混用防止混乱的指针
正因为有了指针cin才那么慢
@Alex172 反正我们老师说可以用(误导
呃呃还有这种操作 长见识了
@EricNTH 速度是getchar/putchar快于stdio快于iostream吧
@EricNTH noip不能用 害
话说您什么时候回归的
@EricNTH eh?