Optimization of Kingsoft program Question 2

Source: Internet
Author: User

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:
  1. /*************************************** *********************
  2. * Name: problem02.c *
  3. * Description: n elements in the integer array and M (M> = N) equal to the number of elements )*
  4. * Assume that an element cannot be repeated *
  5. * Train of Thought: from 1 to M groups and array elements, and sum to determine whether it is equal to M *
  6. * Environment: Code: blocks & Windows 7 & x86 *
  7. * Note: davelv is from 09-12-06 *
  8. **************************************** *********************/
  9. # Include <stdio. h>
  10. # Include <stdlib. h>
  11. # Include <string. h>
  12. # Define buf_len 1024.
  13. // Comparison function for qsort
  14. Int compare (const void * a, const void * B );
  15. // There are several methods to obtain Functions
  16. Int getotalnum (INT array [], int m );
  17. // Numeric combination Function
  18. Void combine (INT array [], int result [], int m );
  19. Int main (void)
  20. {
  21. Int Buf [buf_len], M, I;
  22. Printf ("Please input no. of INTEGER (s) No more than % d:", buf_len );
  23. Scanf ("% d", & M );
  24. Printf ("Please input INTEGER (s ):");
  25. For (I = 0; I <m; I ++)
  26. {
  27. Scanf ("% d", BUF + I );
  28. }
  29. Printf ("totle % d way (s)/n", getotalnum (BUF, m ));
  30. Return 0;
  31. }
  32. Int compare (const void * a, const void * B)
  33. {
  34. Return * (int *) A-* (int *) B;
  35. }
  36. // The returned value is the total number of methods. If the value is-1, the function fails.
  37. Int getotalnum (INT array [], int m)
  38. {
  39. Int I, totle;
  40. Int * result;
  41. Qsort (array, M, sizeof (array [0]), compare); // sort
  42. // Exclude numbers larger than m
  43. For (I = 0; I <m; I ++)
  44. {
  45. If (array [I]> m)
  46. {
  47. M = I;
  48. Break;
  49. }
  50. }
  51. Result = (int *) malloc (M * sizeof (INT); // allocate cache space for the combine Function
  52. If (result = NULL)
  53. Return-1;
  54. Combine (array, result, M );
  55. Totle = * result;
  56. Free (result );
  57. Return totle;
  58. }
  59. // Array: Stores numbers, result: Number of stored results, and M: Number of numbers
  60. Void combine (INT array [], int result [], int m)
  61. {
  62. Int I, T; // counter, the intermediate variable of the begin variable
  63. 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
  64. For (I = begin; I <= m; I ++)
  65. {
  66. Totle + = array [I];
  67. Result [level] = array [I]; // Save the current number
  68. If (Totle <m) // determines whether the current combination number and m limit have been exceeded.
  69. {
  70. // Determine whether the current number of layers is M-1
  71. If (Level m-1)
  72. {
  73. Level ++;
  74. T = begin;
  75. Begin = I + 1;
  76. Combine (array, result, m); // lower Recursion
  77. Begin = T;
  78. Level --;
  79. }
  80. }
  81. Else if (Totle> m) // if the value exceeds m, the current loop is exceeded.
  82. {
  83. Totle-= array [I];
  84. Break;
  85. }
  86. Else
  87. {
  88. Int J;
  89. For (j = 0; j <= level; j ++) // outputs a number if the match exists.
  90. Printf ("% d", result [J]);
  91. Puts ("");
  92. Num ++; // counter plus one
  93. }
  94. Totle-= array [I];
  95. }
  96. If (Level = 0)
  97. {
  98. Result [0] = num;
  99. }
  100. }

Test the time consumed by the new and old programs getotlenum () using a self-written test program. The results are as follows:

Code:
  1. E:/cbwork/testc/bin/release> driver.exe
  2. Generate_test_data_file OK!
  3. Run tested programs
  4. Data old (MS) New (MS)
  5. 50 75 2
  6. 60 289 5
  7. 70 998 15
  8. 80 3278 44
  9. 90 9409 107
  10. 100 26747 261
  11. 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:
  1. # Include <stdio. h>
  2. # Include <stdlib. h>
  3. # Include <string. h>
  4. # Include <time. h>
  5. # Define program_name_type "xxx.exe"
  6. # Define in_pipe "<"
  7. Void generate_test_data_file (int from, int to, int step );
  8. Void run_programs (int from, int to, int step );
  9. Int main ()
  10. {
  11. Int from = 50, to = 110, step = 10;
  12. Printf ("generate_test_data_file ");
  13. Generate_test_data_file (from, to, step );
  14. Puts ("OK! ");
  15. Puts ("Run tested programs ");
  16. Puts ("Data/T/told (MS)/T/tnew (MS )");
  17. Run_programs (from, to, step );
  18. Return 0;
  19. }
  20. Void run_programs (int from, int to, int step)
  21. {
  22. Char cmd [filename_max];
  23. Int Len;
  24. For (; from <= to; from + = step)
  25. {
  26. Len = strlen (program_name_type) + strlen (in_pipe );
  27. ITOA (from, CMD + Len, 10 );
  28. Printf (CMD + Len );
  29. Printf ("/T ");
  30. Memcpy (CMD, "old.exe <", strlen ("old.exe <"));
  31. System (CMD );
  32. Printf ("/T ");
  33. Memcpy (CMD, "new.exe <", strlen ("new.exe <"));
  34. System (CMD );
  35. Puts ("");
  36. }
  37. }
  38. Void generate_test_data_file (int from, int to, int step)
  39. {
  40. File * FP;
  41. Int I;
  42. Char filename [filename_max];
  43. For (; from <= to; from + = step)
  44. {
  45. Fp = fopen (ITOA (from, filename, 10), "W ");
  46. If (FP = NULL)
  47. {
  48. Puts ("Generate Test Data File error! ");
  49. Exit (1 );
  50. }
  51. Fprintf (FP, "% d/N", from );
  52. For (I = 0; I <from; I ++)
  53. {
  54. Fprintf (FP, "% d", I );
  55. }
  56. Fclose (FP );
  57. }
  58. }

 

Statistical System

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.