can anyone explain the logic behind fast input and output…?
and why we use getchar_unlocked…??
1 Like
please help for java language …
1 Like
and c++ also…
1 Like
In c or c++ :
here pf is for print and sf is for input
inline void pf(int a)
{
char snum[200];
int i=0;
do
{
snum[i++]=a%10+48;
a=a/10;
}while(a!=0);
i=i-1;
while(i>=0)
putchar(snum[i--]);
//putchar('\n');
}
int sf()
{
register int c = getcx();
int num = 0;
for(; c < '0' || c > '9'; c = getcx());
for(; c >= '0' && c <= '9'; c = getcx())
num = (num << 3) + (num << 1) + c - 48;
return num;
}
2 Likes
and for java…??
1 Like
i don’t know for java…
3 Likes
This topic has been already discussed here(The method is very well explained here)!!
Please try and search for topics before you add a new question!!
Also as @emin3m points out…the code snippet provided by @himanshu_95 will not work in the case of negative numbers!!!
For integers you can use this for ip:
int scan_d()
{
int ip=getchar_unlocked(),ret=0,flag=1;
for(;ip<'0'||ip>'9';ip=getchar_unlocked())
if(ip=='-')
{
flag=-1;
ip=getchar_unlocked();
break;
}
for(;ip>='0'&&ip<='9';ip=getchar_unlocked())
ret=ret*10+ip-'0';
return flag*ret;
}
and this for op:
void print_d(int n)
{
if(n<0)
{
n=-n;
putchar_unlocked('-');
}
int i=10;
char output_buffer[10];
do
{
output_buffer[--i]=(n%10)+'0';
n/=10;
}while(n);
do
{
putchar_unlocked(output_buffer[i]);
}while(++i<10);
}
Also for fast io in java you can go through some of the codes here: LINK!!
Hope this helps…