principle
As we all know, putchar () is a function to output a single character.
Therefore, each digit of the number is converted into character output to speed up.
It should be noted that the minus sign must be judged separately , and the% (mod) is taken out of the last digit of the number, so it must be output in reverse order .
Code
General quick write:
inline void write(int x) {
if(x<0) {
x=-x;
putchar('-');
}
if(x>9) write(x/10);
putchar(x%10+'0');
}
But the recursive implementation constant is larger, we can write a stack to achieve this process:
inline void write(int x) {
static int sta[35];
int top=0;
do {
sta[top++]=x%10, x/=10;
} while(x);
while(top) putchar(sta[--top]+'0');
}
Fast write regardless of integer type:
template <typename T>
inline void write(T x) {
if(x<0) putchar('-'),x=-x;
if(x<10) putchar(x+'0');
else write(x/10),putchar(x%10+'0');
}
fwrite
#include <cctype>
#include <cstdio>
typedef long long LL;
#define bsiz 1000000
int sta[30];
char buf[bsiz], pbuf[bsiz], *p = pbuf, *s = buf, *t = buf;
#define mputc(ch) (p - pbuf == bsiz ? fwrite(pbuf, 1, bsiz, stdout), p = pbuf, *p++ = ch : *p++ = ch)
inline void putll(LL x) {
register int top = 0;
if (x<0) mputc('-'), x = -x;
do sta[top++] = x % 10, x /= 10;
while (x);
while (top) mputc(sta[--top] + '0');
}
int main() {
LL ans=123;
putll(ans);
fwrite(pbuf, 1, p - pbuf, stdout);
printf("\n");
return 0;
}
related articles
Input optimization (fast reading): https://blog.csdn.net/Ljnoit/article/details/104161565