A few days ago the Kingsoft company to the program problem under, the Code also posted, the original here: http://student.csdn.net/space.php? Uid = 53444 & Do = Blog & id = 16634
Programming 2
Given an array size m and an array Array
M = 10; array = {1, 2, 4, 5, 6, 7, 8, 9, 10}
Obtain the number of n (n <= m) randomly from the array so that the sum of M is obtained. For example, 10, 9, 2, and 5.
Method prototype: int getotalnum (INT [] array, int m );
The original code was not optimized very well, so it was rewritten this morning.
There is no change in time complexity. It is still O (SIGMA (1 <I <N, C (n, I), but the pruning effect of the new algorithm is better.
The main method is to rewrite the combine function and move the external call cycle of combine into the combine function. This reduces the redundancy of repeated calculation of some results in the combine function and improves the running efficiency.
The new Code is as follows:
Code:
- /*************************************** *********************
- * Name: problem02.c *
- * Description: n elements in the integer array and M (M> = N) equal to the number of elements )*
- * Assume that an element cannot be repeated *
- * Train of Thought: from 1 to M groups and array elements, and sum to determine whether it is equal to M *
- * Environment: Code: blocks & Windows 7 & x86 *
- * Note: davelv is from 09-12-06 *
- **************************************** *********************/
- # Include <stdio. h>
- # Include <stdlib. h>
- # Include <string. h>
- # Define buf_len 1024.
- // Comparison function for qsort
- Int compare (const void * a, const void * B );
- // There are several methods to obtain Functions
- Int getotalnum (INT array [], int m );
- // Numeric combination Function
- Void combine (INT array [], int result [], int m );
- Int main (void)
- {
- Int Buf [buf_len], M, I;
- Printf ("Please input no. of INTEGER (s) No more than % d:", buf_len );
- Scanf ("% d", & M );
- Printf ("Please input INTEGER (s ):");
- For (I = 0; I <m; I ++)
- {
- Scanf ("% d", BUF + I );
- }
- Printf ("totle % d way (s)/n", getotalnum (BUF, m ));
- Return 0;
- }
- Int compare (const void * a, const void * B)
- {
- Return * (int *) A-* (int *) B;
- }
- // The returned value is the total number of methods. If the value is-1, the function fails.
- Int getotalnum (INT array [], int m)
- {
- Int I, totle;
- Int * result;
- Qsort (array, M, sizeof (array [0]), compare); // sort
- // Exclude numbers larger than m
- For (I = 0; I <m; I ++)
- {
- If (array [I]> m)
- {
- M = I;
- Break;
- }
- }
- Result = (int *) malloc (M * sizeof (INT); // allocate cache space for the combine Function
- If (result = NULL)
- Return-1;
- Combine (array, result, M );
- Totle = * result;
- Free (result );
- Return totle;
- }
- // Array: Stores numbers, result: Number of stored results, and M: Number of numbers
- Void combine (INT array [], int result [], int m)
- {
- Int I, T; // counter, the intermediate variable of the begin variable
- Static int totle = 0, level = 0, begin = 0, num = 0; // combined numbers and, recursive depth, subscript of the starting element of the combination, the number of combinations that meet the requirements
- For (I = begin; I <= m; I ++)
- {
- Totle + = array [I];
- Result [level] = array [I]; // Save the current number
- If (Totle <m) // determines whether the current combination number and m limit have been exceeded.
- {
- // Determine whether the current number of layers is M-1
- If (Level m-1)
- {
- Level ++;
- T = begin;
- Begin = I + 1;
- Combine (array, result, m); // lower Recursion
- Begin = T;
- Level --;
- }
- }
- Else if (Totle> m) // if the value exceeds m, the current loop is exceeded.
- {
- Totle-= array [I];
- Break;
- }
- Else
- {
- Int J;
- For (j = 0; j <= level; j ++) // outputs a number if the match exists.
- Printf ("% d", result [J]);
- Puts ("");
- Num ++; // counter plus one
- }
- Totle-= array [I];
- }
- If (Level = 0)
- {
- Result [0] = num;
- }
- }
Test the time consumed by the new and old programs getotlenum () using a self-written test program. The results are as follows:
Code:
- E:/cbwork/testc/bin/release> driver.exe
- Generate_test_data_file OK!
- Run tested programs
- Data old (MS) New (MS)
- 50 75 2
- 60 289 5
- 70 998 15
- 80 3278 44
- 90 9409 107
- 100 26747 261
- 110 69330 601
GCC (GCC) 3.4.5 (mingw-Vista special), O2 switch optimization, win7, Intel Pentium dual-core 1.8 GHz, 1 GB memory.
To eliminate the noise interference when the time is low, we can draw the following conclusion.
1. When the data volume of the old Program increases by 10, the time is increased by about three times.
2. Each time the data volume of new programs increases by 10, the time increases to about 2 + times that of the original programs.
3. New programs are about 100 times faster than old programs with the same data volume.
The test driver is also provided:
Code:
- # Include <stdio. h>
- # Include <stdlib. h>
- # Include <string. h>
- # Include <time. h>
- # Define program_name_type "xxx.exe"
- # Define in_pipe "<"
- Void generate_test_data_file (int from, int to, int step );
- Void run_programs (int from, int to, int step );
- Int main ()
- {
- Int from = 50, to = 110, step = 10;
- Printf ("generate_test_data_file ");
- Generate_test_data_file (from, to, step );
- Puts ("OK! ");
- Puts ("Run tested programs ");
- Puts ("Data/T/told (MS)/T/tnew (MS )");
- Run_programs (from, to, step );
- Return 0;
- }
- Void run_programs (int from, int to, int step)
- {
- Char cmd [filename_max];
- Int Len;
- For (; from <= to; from + = step)
- {
- Len = strlen (program_name_type) + strlen (in_pipe );
- ITOA (from, CMD + Len, 10 );
- Printf (CMD + Len );
- Printf ("/T ");
- Memcpy (CMD, "old.exe <", strlen ("old.exe <"));
- System (CMD );
- Printf ("/T ");
- Memcpy (CMD, "new.exe <", strlen ("new.exe <"));
- System (CMD );
- Puts ("");
- }
- }
- Void generate_test_data_file (int from, int to, int step)
- {
- File * FP;
- Int I;
- Char filename [filename_max];
- For (; from <= to; from + = step)
- {
- Fp = fopen (ITOA (from, filename, 10), "W ");
- If (FP = NULL)
- {
- Puts ("Generate Test Data File error! ");
- Exit (1 );
- }
- Fprintf (FP, "% d/N", from );
- For (I = 0; I <from; I ++)
- {
- Fprintf (FP, "% d", I );
- }
- Fclose (FP );
- }
- }
Statistical System