Read-In output optimization _c++

Source: Internet
Author: User

  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++

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.