Analysis of Common (C ++) Problems of ACM

Source: Internet
Author: User

1. Common Input/output format problems

A. Enter T as the number of data groups in the first row, and then enter T as the number of data groups.
Int main ()
{
........
Int T;
Cin> T;
While (t --)
{
.......
}
......
Return 0;
}
}
B. Read the int main () until the end of the file ()
{
........
Int T;
While (CIN> T)
{
.......
}
......
Return 0;
}
}
2. floating point number precision settings
Header file # include <iomanip>
Cout <fixed <setprecision (n) <t <Endl;
That is, output T, retain the decimal point t decimal places

3. Rounding
Header file # include <cmath>
Rounded up: Double Ceil (Double X)
Rounded down: Double floor (Double X)
You can also use forced conversion of data types, which depends on the data size.
INT () or long (), for example, INT (104.23) = 104
4. sorting problems
Header file # include <algorithm>
Most commonly used: Sort (begin, end, compare );
Begin: initial iterator Position end: Last iterator Position + 1, compare: Sorting Function
Example:
Vector <int> VEC;
VEC. push_back (2 );
VEC. push_back (5 );
VEC. push_back (3 );

Bool comapre (int A, int B) // note that the parameter type must be a container parameter type and the function is written outside the main function.
{
Return A> = B; // sort in descending order
}
Sort (VEC. Begin (), VEC. End (), compare); // Vec. End () actually points to the next bit of the last element.

5. General array initialization Methods

Header file # include <string. h>

Int array [1000000];

Memset (array, 0, sizeof (array); // uses the memory block as the initialization unit, which is fast. It is recommended that

6. Remove repeated items in string

Header file:

# Include <string>

# Include <algorithm>

Example:

String STR ("aaasssdddsssaa ");

Sort (Str. Begin (), str. End ());

Str. Erase (unique (Str. Begin (), str. End (), str. End ());

7. Delete All characters in the second string contained in the first string

To improve efficiency, first remove the repeated items in the second string, see Figure 6.

Second (assuming the first string str1 ("djasghjasdg "))

Bool compare (char ch) // compile it outside the main function
{
For (it2 = str2.begin (); it2! = Str2.end (); it2 ++)
If (CH = * it2)
Return true;
Return false;
}

Str1.resize (remove_if (str1.begin (), str1.end (), compare)-str1.begin ());

Cout <str1 <Endl;

8. A full line of string Input

String STR;

Getline (CIN, STR); // line break is not included

9. The problem of converting a string to a different hexadecimal INTEGER:

Converts a string type char STR [] to an n-base number.

Header file: # include <stdlib. h>

Long int strtol (const char * nptr, char ** endptr, int Base)

This function converts the nptr parameter to the Growth integer number based on the base parameter. The parameter range is from 2 to 36, or 0. the base parameter indicates the base mode. However, if the '0x 'prefix character is used, the hexadecimal mode is used for conversion, if you encounter a '0' prefix rather than a '0x 'prefix, the system will use 8-in for conversion. Strtol will scan at first

The nptr parameter is a string that skips the leading space character and starts conversion only when a number or plus or minus sign is met, and ends conversion when a non-number or string ends ('\ 0, and return the result. If the weak endptr parameter is not null, the character pointer in the nptr that terminates when the condition is not met is returned by endptr. If the endptr parameter is null, no invalid characters are returned.

Likewise:

Strtoul () converts the nptr parameter string to an unsigned long integer value based on the base parameter.

Strtoll () converts the parameter nptr string to the Growth integer number based on the parameter base

10. c ++ output format problems

For example, to specify an integer of 5 to output a fixed width format such as 00005

Set cout. Width (n); // n to width

Cout. fil (CH); // The ch character is a fill character

 

 

 

 

 

 

Related Article

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.