C/c ++ scanf printf usage and Optimization

Source: Internet
Author: User

I. The cin and cout of C ++ are too slow. Therefore, scanf and printf are always used for input and output. However, recently, it was found that scanf and printf are still too slow when the data volume is large, so they can only optimize the input and output by hand. However, the input and output functions of scanf and printf are very powerful, so I have learned it all. Here is a summary. Ii. Formatting specifier meaning % d decimal signed integer % u decimal unsigned integer % o octal unsigned integer % x hexadecimal unsigned integer % f decimal floating point number, if there is no domain width, it should contain 6 decimal places % e decimal scientific notation form floating point % g decimal form and scientific notation form shorter one % c single character % s character array % p pointer address ii. scanf format input function, read the input information from the standard input device. The call format is: ("<format>", <address>). format: % [flag character] [domain width] [length] formatting specifier (optional in brackets) flag character: * (skip the input value if no variable is assigned after reading) domain width: if the actual number of digits is greater than the domain width, the length of the intercepted domain width is truncated. The remaining part is still in the standard input, waiting for the next input. If the actual number of digits is less than the domain width, no effect is required. The length is divided into h, l (the following may vary depending on different compilers) h indicates reading by short type (short integer, single precision type) l indicates reading by long type (integer = long integer, Double Precision = Long double Precision type) Others: if it does not belong to any of the above cases, it is ignored by comparison in the format, that is, if "," appears between % d and % d, the input will ignore "," which is tightly clipped between two integers. If the integer contains two ",", two "," are required in the format. The difference is that if it is a space, ignore, but ignore all spaces or blank lines in the middle. When reading integers, floating-point numbers, and strings, you can ignore spaces or empty rows without adding spaces in the format. Address: the address of the parameter to be read, not the variable itself. Common variable: add the address symbol "&" before the variable name. array name indicates the first address of the array, no need to add the address symbol pointer variable: the pointer name is the address, no need to add "*" 3. printf format output function, output information to the standard output device, call format: ("<format>", <parameter>) Format: % [flag character] [domain width] [precision] [length] format specifier (optional in brackets) flag characters:-(the result is left aligned. When the field width is not enough, enter a space on the right) + (no matter whether the positive or negative signs are output symbols) ("space", the positive signs show spaces, and the symbol output symbols) # (add the octal prefix 0 to output % o, and add the hexadecimal prefix 0x to output % x) domain width: if the actual number of digits is greater than the domain width, and the actual number of digits is less than the actual number of digits (decimal point, or the same below), the field width is left blank, if there is a flag character "-" before, and vice versa, it indicates that the number of the domain width starts with 0, then it is right when the actual number of digits is less than the domain width, and the left side is filled with leading zero precision: ". open First, followed by a decimal integer. For floating-point numbers, this parameter indicates the number of decimal places. If the actual number of digits is more, this parameter is rounded to the nearest integer. If the actual length is greater, truncate the first part, with less effective length: divided into h and l (which may vary depending on different compilers). h indicates short output (short integer, single precision type) l indicates long-type output (integer = long integer, double-precision = Long double-precision type). Others: if it does not belong to any of the above situations, including unmentioned characters, or not in a certain order. For example, if "space" is not directly followed by "%" but appears after the formatting specifier, the output parameter is as is: the value of the output parameter is required, not an address-specific variable: directly name the variable, without adding an array of address symbols before: directly name the array to tell the program to output the starting position pointer variable: add the "*" sign before the pointer name. 4. Optimization: The data size is very large. For example, if you need to input or output 10 million integers, scanf and printf will become very slow, however, the getchar and putchar functions are very Fast, so we need to use it to optimize the reading and output. Read optimization: [cpp] // read 1-: 3 before optimization. 176 seconds // read 1-after optimization. 232 seconds void scan () {k = num = 0, bit = 1; while (ch [++ num] = getchar ())! = 32); num --; while (num) k + = (ch [num --]-48) * bit, bit * = 10;} output optimization: [cpp] www.2cto.com // output 1-before optimization. 336 S // output 1-after optimization. 897 seconds void print (int k) {num = 0; while (k> 0) ch [++ num] = k % 10, k/= 10; while (num) putchar (ch [num --] + 48); putchar (32);} Note: Since optimization is required, it is time-consuming to minimize the number of parameters! NOTE: If any error occurs, please leave a message. Thank you very much.

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.