Getting Started with algorithms (C + +)

Source: Internet
Author: User
Tags website hosting

Iostream, there are many commonly used functions in this header file, such as the value of swap two variables, max for the maximum of two values and so on.

Cstdio header file, this header file contains C-style input and output. If you have studied the C + + language before, you should know how CIN reads and cout output. So why should we use C's scanf and printf? Because scanf and printf are much more efficient than CIN and cout.

Using namespace STD If you don't have this line, swap is going to be written std::swap, and the code will be troublesome.

The main function returns a value of type int. So what is the value of the return? It is used to flag whether the program is correctly returned without errors, and this return value is obtained when the compiled program is executed. Returning 0 means that the program exits without errors.

Input format: Enter a number n for the first row of each group of data, indicating how many squares there are; the second line enters n positive integers, which represent the length of each square's edge. If n=0, enter the end.

Output format: Each group outputs a value that represents the total area of all squares of the group.

while (scanf ("%d", &n), n) loops through n, until N is 0 o'clock exits the loop. This input format is very common in algorithmic topics. Comma expression, the result of the entire expression is a comma-delimited value of the last expression. When the value of n is 0 o'clock, the value of the entire expression is 0, at which point the loop exits.

Reference only one header file #include <bits/stdc++.h> without referencing two header files. This is not the case for all platforms, use the FAQ before you remember to consult the platform.

Enter the original text to be copied, perhaps some readers will want to write scanf("%s", str); but here is not feasible, because there will be spaces inside the article, and such a statement can only read the contents of the space before the content into Str. So we're going to use a function that reads a whole line, as follows: Get (str), more than a reminder, when the use of Get overflow if the program is not error, so when using the gets must be careful!

The return value of the scanf function: When scanf is correctly read, the return value is equal to the number of variables read. The scanf function returns 1 when the read error occurs and the end of the file is read.while (scanf("%d", &cnt) != -1)

The maximum range of input data is 10000,10000 cubic is 10^12, and the maximum value of type int is 2147483647, approximately 2*10^9, far exceeds the maximum value of int, so overflow occurs during the calculation, which results in an error. So we need to use a larger integer type long long, which has a maximum value of about 10^19, even if 10,000 10^12 does not exceed the range of a long long.

The program design contest, as its name implies, is the game of writing code. The current program design contest has a variety of competitions, such as problem-solving competitions, performance competitions, creative contests, etc.

ACM-ICPC, the International College Program Design Competition, is the largest program design competition for university students worldwide, hosted by the American Computer Association (ACM). The competition consists of three teams , completing 8-12 questions within 5 hours , and determining the final position according to the number of questions and the cumulative time of problem solving. Each issue will be sent to the team with a balloon. The ACM-ICPC (ACM) competition consists of regional and final competitions, which consist of the network heats and the field races. The pre-qualifying schools in the network are eligible for live matches, usually with 1-3 places per school, and the history of the school, the rankings of online races, and so on. At present, the first 15 teams in the mainland region won the gold medal, 第16-45名 won the silver medal, the remaining team in the first 60% won the bronze medal. One of the top-ranked schools can represent the Chinese mainland in the world finals. There are 4 gold medals, 4 silver medals and 4 bronze medals in the world Finals. Mainland China's team in the finals of the history of excellence, including Shanghai Jiaotong University won 3 times, Zhejiang College won 1 times.

Topcoder (topcoder.com) is a website hosting online programming competitions with many types of matches, common with SRM (single Round match), Marathon (long duration, only one problem, no fixed solution), TC Open and so on. We then focus on SRM, the personal algorithmic programming contest. Users need to download the specified client (arena download link) to participate in SRM competitions, TopCoder is also the only program design contest that must be downloaded by the client to participate. The user will have points (rating) and points corresponding to the color indicator in TopCoder, as shown in Red > Yellow > Blue > Green > Gray > White. The ID of the player with the highest number of points will have a bull-like flag on the left, which we call target. Acrush (Loutian) is one of the target players.

Topcoder (SRM) each match will be divided into Div1 and div2 respectively according to the rating of the user before the tournament. The competition is divided into coding stage, challenge stage and system Test stage. Each division will have dozens of rooms, each player in the room in the challenge stage can see each other's code, found the error can submit test data to challenge, the challenge is successful (the opponent program calculation results and correct output inconsistent) can be added, failure will be deducted points. This is what we often call "Cha people", very exciting. The interesting thing about arena is that you can install a variety of plugins (plug-ins list), here is recommended to you a most commonly used plug-ins: Examplebuilder, its documentation is connected here. With this plugin, you can write and debug your code locally very quickly.

Google Code Jam (Code.google.com/codejam) is the annual world-scale Program design competition held by Google, with participants having to solve about 4 problems within 2-3 hours. You will be able to participate in the Live (Onsite) finals from several rounds of online competitions. The event features two sets of input data for each title, small and large. Even if the difficulty factor is large, the input size is small enough to be easily solved. In addition, Google-code-jam (usually referred to as GCJ) does not automatically execute the program on the server, but instead requires that the source code and local execution results be submitted together.

In the ACM Competition team, the role of the players can be divided into the captain, the core of problem solving and auxiliary, etc. the tasks to be completed in the competition can be divided into reading questions, preparation, programming and troubleshooting. The main job of the team leader is to plan and make the team's plans and decisions, including training on the pitch and on the field . an excellent assistant to the team plays a very important role, the stability of the ability to check the wrong, excellent data ability, detailed ability to read the question to enable the team to improve the strength of several grades. Mutual understanding between teammates is the foundation of everything. Teammates should clearly know what is good, what is not good at what, each problem assigned to the most suitable person to do. Good teamwork is based on mutual trust between teammates, and players can accurately express their meaning to their teammates. These can be cultivated slowly through training.

IDE (integegrated development environment), Chinese translation as an integrated development environment, it is much easier for ACM beginners to use the IDE than to use an editor to compile with the compiler. The languages commonly used in ACM contests are C + + or java. The most common IDE for C/s + + is Codeblocks,java, the most commonly used IDE for eclipse. It is important to note that the download of the Windows version of Codeblocks must download with MinGW, otherwise you need to configure C + + compiler, for beginners may be more troublesome. MinGW is the minimalist GNU on Windows abbreviation, as the name implies is the GNU library running on Windows, used to compile C/s code.

On Linux and MinGW corresponds to GCC and g++, because these are the system comes with the program, in the installation of Codeblocks no need to install it again. Two compilers are still different in some nuances, such as the input (scanf) and output (printf) of 64-bit integer variables, and the parameters under Linux are%lld, and the parameters under Windows are%i64d. For example, HDU (acm.hdu.edu.cn) and CF (codeforces.com) are typical Windows-built OJ, but most OJ are still running in the Linux environment. When submitting code that uses 64-bit integers, be sure to pay attention to the operating system of the platform, usually by checking the OJ FAQ or by consulting the referee at the contest site.

There are some online Judge (referred to as OJ) system, they can automatically evaluate the previous program design contest algorithm problem, on the OJ can practice. In addition, some OJ will regularly hold their own competitions, you may wish to participate in a regular.

Input format: reads the data sequentially until the end of the file. So what do you do with this input? Let me show you how to read in each language.

First is C/c++:while (scanf ("%d", &n)! =-1) {} or while (CIN >> N) {}

Java:scanner cin = new Scanner (system.in); while (Cin.hasnext ()) {}

Python:for Line in stdin:

Codechef (codechef.com) is an Indian-founded OJ, there are two forms of competition, one is challenge, lasts 10 days, one is lunchtime, lasts 3 hours.

Codeforces (codeforces.com) is a Russian-founded OJ, will regularly host algorithmic competitions, the competition is divided into Div1 and Div2, a set of points and color system, red highest, red users have a lot of privileges.

HDU (acm.hdu.edu.cn) is the maintenance of Hangzhou University of OJ, annual summer will organize a large number of universities to carry out ACM training, known as multi-school joint training, is the largest ACM inter-university training.

What is code capability? Simply put, if you think you have learned an algorithm or data structure, can you complete the program in half an hour (or an hour) and pass through very little debugging? If you do not, you can only say that you have not mastered the algorithm or data structure, to continue to train, until the requirements, only in this way to ensure that the high tension in the game to achieve the courage to make mistakes.

Writing code is not a problem solving, but a logical process that has already been prepared in the mind.

The estimation of complexity is very important when designing algorithms that meet the requirements of the problem. It is not possible to implement every algorithm we think of to see if it is fast enough. The complexity of the algorithm should be used to determine whether the desired algorithm is sufficient to cope with the data size given by the topic. Complexity is divided into time complexity and space complexity. In layman's words, it is "how fast" and "how much memory". When analyzing complexity, we usually consider what it is proportional to and call it the order of the algorithm. For example, the program executes a four-cycle, each loop executes n times, then the running time is proportional to the N4. We will write O (N4) in proportion to N4. Another example of the program opened a two-dimensional array, subscript range is [0..n-1][0..n-1], then the spatial complexity of the program is O (N2). The running time of a program depends not only on the complexity of time, but also on factors such as computational complexity and recursion, but the resulting gap is several times. When estimating whether a complexity can withstand the current scale of data, it will be possible to determine whether the algorithm can meet the requirement of running time limit by the gradual formula of the maximum treatment complexity of the numerical value. For example, the O (N2) algorithm, the n=1000 into the generation to get 1000000. In general, it is calculated that 108 is basically the maximum value of the 1s time limit as described above. The different solution of the same problem, even if the time complexity is the same, it is possible to run a lot of times, so it is equally important for the efficiency optimization of code details, especially the simplification of repetitive calculation.

Division, that is, "divide and conquer", is to divide a complex problem into two or more of the same or similar sub-problem, until the last sub-problem can be simply solved directly, the solution of the original problem is the solution of the sub-problem of the merger. Summed up is: decomposition, settlement, merger, these three big strides. One of the most classic examples of divide-and-conquer strategies is the binary search algorithm, which is two-point lookup. A binary lookup is a search algorithm that finds a particular element in an ordered array. The search process starts with the middle element of the array, judging the relationship between the unknown origin element and the intermediate element, to determine which half of the next selection continues to be searched. Since the binary search algorithm reduces the search area by half each time, the time complexity is O (Logn), which is much more efficient than the naïve algorithm O (N), which is the advantage of the divide-and-conquer strategy: efficient. Other classic examples of divide-and-conquer strategies are the merge ordering in the sorting algorithm. The idea of merge sorting is to divide the elements to be sorted into roughly the same two sub-sets, sort the two sub-sets, and eventually merge the sorted subsets into an orderly set. The problems that can be solved by divide and conquer generally have the following characteristics: The scale of the problem can be easily resolved to a certain extent; the problem can be decomposed into several small-scale identical sub-problems, and the solution of multiple sub-problems can be merged into the solution of the original problem. In addition, if each sub-problem is not independent, then it is necessary to solve the common sub-problem repeatedly, which is more suitable for the dynamic programming algorithm we will talk about later.

Codeforces (codeforces.com) is a Russian-founded OJ, will regularly host algorithmic competitions, the competition is divided into Div1 and Div2, a set of points and color system, red highest, red users have a lot of privileges.

Getting Started with algorithms (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.