Updated: 2012.5.10
At noon last Sunday (2012.5.6), I went to huake to participate in the Baidu test. The last question of the test paper was to ask the suggestion in the baidu search box how to implement the function, what data structure andAlgorithm.
I briefly mentioned the top K.
I have read some interview questions in the July blog of algorithm Daniel some time ago, including top K algorithm. The beauty of programming also has a section dedicated to top K. Now Baidu has also obtained the test, indicating that the top K algorithm is really too important. It's a pity that I just read it over and did not fully understand it. Now we are working on top K learning,CodeWhere can I find a large amount of test data after writing it? Well, of course you write it yourself.ProgramGenerated. The following section describes how to generate random numbers randomly and store the data in the data.txt file so that we can test the data at any time.
Essence:
Srand (unsigned (time (0); // Random Seed, in order to increase the probability of non-repetition
Rand () % (MAX-MIN + 1) + min; // The generated random value is between min and max (including Min, max)
Read the Code directly. all the important information is on it.
/* Functions implemented by this program: generate 10 million random numbers that may be repeated, used as the test data and the time used to generate the data */# include <iostream> # include <ctime> // time. h can also be used to calculate the program running time and generate random seed using namespace STD; # define self_rand_max 0x7fffffint main () {// typedef long clock_t start_time = clock (); // timing start srand (unsigned (time (0); // generation time seed // The generated value is between 1000 and 10 million. Const int max = 10000000; const int min = 1000;/* read data freopen ("in.txt", "r", stdin); // write data freopen ("out. TXT "," W ", stdout); // fclose (stdin); // close the file // fclose (stdout); // close the file and redirect the standard input stream stdints to the in.txt file, in this way, data is not read from the standard input stream when scanf or CIN is used, but the input is obtained from the in.txt file. You only need to paste the input data to in.txt first, which makes debugging much easier. */Freopen ("data.txt", "W", stdout); // cout <"--- generate 10,000,000 random numbers \ // which maybe repeated ---" <Endl; for (INT I = 0; I <10000000; ++ I) {// # define rand_max 0x7fff // standard unsigned long data = (max * rand ()) /(rand_max + min) + 1; // unsigned long data = (max-min + 1) * rand () + min; // unsigned long data = (max-min + 1) * rand () + min) % 10000000; unsigned long data = rand () % (max-min + 1) + min; // cout <data <''; // output data to data.txt} fclose (stdout); freopen (" time.txt "," W ", stdout ); cout <Endl <"--- the end ---" <Endl; // clocks_per_sec controls the precision. The value is 1000 in windows, in Linux, the duration is several milliseconds. // in windows, the duration is several milliseconds. in Linux, the duration is cout <"elapsed time:" <double (clock ()-start_time) /clocks_per_sec <'s '<Endl; // fclose (stdout );}
Running result:
Time.txt
--- The end ---
Elapsed time: 5.171 s
The number of generated data.txt files is as high as 55 MB. A total of 10 million pieces of data can be determined through the following test procedure that the values of these 10 million pieces of data are.
# Include <iostream> # include <ctime> // # include <boost/Timer. HPP> // using namespace boost; using namespace STD; int main () {unsigned long data; // timer t; int Len = 0; freopen ("data.txt ", "r", stdin); While (CIN> data) {If (Data >=1000 & Data <= 10000000) {// cout <data <Endl; len ++ ;}}cout <Len <Endl; // cout <t. elapsed () <Endl ;}
The running result is 10000000.
In addition, how can we generate 10 million unique numbers? This is a huge challenge that will be solved in the future.
Okay, continue to invest in top K learning.