When we encounter a lot of reading or output in the exam, the code consumes a lot of time to run the program, causing the TL
Originally LOG2N algorithm because read into the card into linear is not cost-effective, so we have to use read-in output optimization
GetChar and Putchar are the fastest way to read in and out, variable names try to use something that is not very common, so as not to conflict
1#include <cstdio>2#include <cstdlib>3#include <cstring>4#include <cmath>5#include <iostream>6#include <algorithm>7 using namespacestd;8 9InlineintRead ()Ten { One CharCh=GetChar (); A intRe=0; - BOOLFl=1; - /* the if (ch== '-') - { - re=0; - Ch=getchar (); + } - */ + while(ch>='0'&&ch<='9') A { atre=re*Ten+ch-'0'; -Ch=GetChar (); - } - returnfl?re:-re; - } -InlinevoidWriteintre) in { - /* to if (re<0) + { - Putchar ('-'); the Re=-re; * } $ */Panax Notoginseng if(re>9) Write (re/Ten); -Putchar (re%Ten+'0'); the } + intMain () A { the intn=read (), i,a; + for(i=1; i<=n;i++) - { $A=read (); $ write (a); -Putchar ('\ n'); - } the return 0; -}
The comment here is to consider the case of negative numbers, many cases are not used
There are many topics that output is only one line or very little data, we can not write output optimization
Also note that inline is also a function optimization, which is equivalent to the entire function define, can be seen as directly paste the code to the place of the call, without recursion, for small and frequently called functions can be used
Read-in optimization can be a template back down, in fact, write fast, it's only 20 seconds
Copyright, reprint please contact the author, offenders must investigate
qq:740929894
Read-In output optimization _c++