Topcoder competition: C ++, STL usage Quick Start & input/output

Source: Internet
Author: User

********************************

The following summarizes the common STL library usage in some topics.


# Include <algorithm>
# Include <string>
# Include <vector>
# Include <map>
# Include <iostream>
 
Using namespace STD;
 
// Recursion
Int getn (int n)
{
If (n = 1) return 1;
Else return getn (n-1 );
}
 
Void teststl_main (INT argc, char * argv [])
// Void main (INT argc, char * argv [])
{
/******** STL **********/
 
// String usage
{
String S = "Mmmmm ";
String S2 ("ss22 ");
S2.insert (2, "kkkkk"); // Insert "kkkkk" to the first position of S2 (the position starts from 0)
S2 + = S + "44444" + 'C ';
Const char * Pc = S. c_str (); // convert string to the string of C-style and end with/0
Const char * ptr1 = S. Data (); // convert string to string
If (s2 [2] = 'k') S2 [2] = 'C ';
S + = "jkl ";
S + = 'M ';
S. push_back ('/N'); // place'/N' in the last position of s.
Reverse (S. Begin (), S. End (); // reverse
Basic_string <char >:: iterator str_iter; // Traversal
Str_iter = S. Begin ();
}
 
// Vector usage
{
Vector <int> V;
V. push_back (8); // insert an element into V. The element value is 8.
Int ilen = (INT) v. Size ();
For (INT I = 0; I <ilen; I ++)
{
Int K = V [0]; // K = 8
}
}
 
// Map usage
{
Map <int, int> MP;
For (INT I = 0; I <3; I ++)
{
MP [I] = I * 2; // access the second element through the [first element]
}
 
Int Total = 100;
Map <int, int >:: iterator it = mp. Begin ();
For (; it! = Mp. End (); It ++) // traverse MP
{
Total + = it-> second; // use iterator it to access the second element
}
Cout <"Total =" <total <Endl;
}
 
// Algorithm
Int n = getn (5); // recursion n! = N * (n-1) * (n-2 )*... * 1
Int AA = 10, BB = 15;
Int Maxi = max (AA, BB); // maximum value
Int mini = min (AA, BB); // minimum value
Int Absi = ABS (-12); // absolute value
Vector <string> V;
V. push_back ("hello ");
V. push_back ("123 ");
V. push_back ("no ");
Sort (V. Begin (), V. End (); // sort the elements in V in alphabetical order.
Int savei;
Sscanf (V [0]. c_str (), "% d", & savei); // convert the string "123" to the number 123
Cout <"savei =" <savei <Endl;
Char Buf [100];
Sprintf (BUF, "V [1] = % d", savei); // print the content into the string
Cout <"Buf =" <Buf <Endl;
 
}

**************************************** ****************

TC input and output are different from general OJ! Remember to change

SRM Input and Output
SRM does not require standard or file input and output. You only need to write a member function of a class. That is to say, what you need to write is not a complete program, but a class.
Input is a parameter of the member function, and return is used for output. Therefore, vector and string in STL are often required.
The TC system does not test the standard output, so the standard output can be used for debugging.
The following describes how to write a program based on the 413-point question of SRM 1000 Div 2.
The question is as follows (select different languages and the description of the question will be slightly different. This article uses C ++ as an example ):

Problem Statement
Note: This problem statement contains subscripts that may not display properly if viewed outside of the applet.

Let's consider an infinite sequence a defined as follows:
A0 = 1;
Ai = A [I/P] + A [I/Q] for all I> = 1, where [x] denotes the floor function of X. (See notes)
You will be given n, p and q. Return the n-th element of a (index is 0-based ).

Definition
Class:
Infinitesequence
Method:
Calc
Parameters:
Long long, Int, int
Returns:
Long long
Method signature:
Long long calc (long N, int P, int q)
(Be sure your method is public)

Notes
-[X] denotes the floor function of X which returns the highest integer less than or equal to X. For example, [3.4] = 3, [0.6] = 0.

Constraints
-N will be between 0 and 10 ^ 12, inclusive.
-P and q will both be between 2 and 10 ^ 9, aggressive.

Examples
0)
0
2
3
Returns: 1

A [0] = 1.

1)
7
2
3
Returns: 7

A [0] = 1; A [1] = A [0] + A [0] = 2; A [2] = A [1] + A [0] = 2 + 1 = 3; A [3] = A [2] + A [1] = 3 + 2 = 5; A [7] = A [3] + A [2] = 5 + 3 = 8.

2)
10000000
3
3
Returns: 32768

3)
256
2
4
Returns: 89

4)
1
1000000
1000000
Returns: 2

This problem statement is the exclusive and proprietary property of topcoder, Inc. any unauthorized use or reproduction of this information without the prior written consent of topcoder, Inc. is strictly prohibited. (c) 2003, topcoder, Inc. all rights reserved.

The following is a program with an incorrect algorithm, which is used for reference only:

# Include <string>
# Include <vector>
Class infinitesequence {
Public:
Long long calc (long N, int P, int q ){
If (n = 0)
Return 1;
Else
Return calc (N/P, p, q) + calc (N/Q, P, Q );
}
};

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.