# 1-2-5组合问题的最高效完整算法

305人阅读 评论(0)
1-2-5组合问题的最高效完整算法
94人阅读评论(3)收藏举报

1. /************************************************************************
2. ***By       : Summon                                                  ***
3. ***Data     : 2010.01.17                                              ***
4. ***Function : 1-2-5组合之和等于100，文总共有多少种                           ***
5. ***           组合方式。                                                      ***
6. ***Method   : 首先使用蛮力发，复杂度O(n^3/)                              ***
7. ***           其次使用蛮力法的改进，复杂度O(n^2)                          ***
8. ***           最后是超级算法，复杂度O(n)                            ***
9. /************************************************************************/
10. #include "stdafx.h"
11. #include <stdio.h>
12. #include <stdlib.h>
13. #include <string.h>
14. #include <time.h>
15.
16. typedef int STATUS;
17.
18. STATUS FoolHardinessMethod(int N, int *result);
19. STATUS FoolHardinessMethodImproved(int N, int *result);
20. STATUS SuperMethod(int N, int *result);
21.
22. int main(int argc, char* argv[])
23.     clock_t start;
24.     clock_t end;
25.     double realTime;
26.     int N;
27.
29.     fflush(stdin);
30.     scanf("%d", &N);
31.
32.     int result = 0;
33.     start = clock();
34.     FoolHardinessMethod(N, &result);
35.     end = clock();
36.     realTime = ((double)(end - start))/CLOCKS_PER_SEC;
37.     printf("N= %d/n", N);
38.     printf("FoolHardinessMethod Running Time = %f/n", realTime);
39.     printf("result = %d/n", result);
40.     printf("/n");
41.
42.     start = clock();
43.     FoolHardinessMethodImproved(N, &result);
44.     end = clock();
45.     realTime = ((double)(end - start))/CLOCKS_PER_SEC;
46.     printf("N= %d/n", N);
47.     printf("FoolHardinessMethodImproved Running Time = %f/n", realTime);
48.     printf("result = %d/n", result);
49.     printf("/n");
50.
51.     start = clock();
52.     SuperMethod(N, &result);
53.     end = clock();
54.     realTime = ((double)(end - start))/CLOCKS_PER_SEC;
55.     printf("N= %d/n", N);
56.     printf("SuperMethod Running Time = %f/n", realTime);
57.     printf("result = %d/n", result);
58.     printf("/n");
59.
60.     return 1;
61.
62. STATUS FoolHardinessMethod(int N, int *result)
63.     if (result == NULL)
64.     {
65.         return 0;
66.     }
67.
68.     int N1 = N/1;
69.     int N2 = N/2;
70.     int N5 = N/5;
71.     *result = 0;
72.
73.     for (int i=0; i<=N5; i++)
74.     {
75.         for (int j=0; j<=N2; j++)
76.         {
77.             for (int k=0; k<=N1; k++)
78.             {
79.                 if (5*i + 2*j + k == N)
80.                 {
81.                     *result += 1;
82.                 }
83.             }
84.         }
85.     }
86.     return 1;
87.
88. STATUS FoolHardinessMethodImproved(int N, int *result)
89.     if (result == NULL)
90.     {
91.         return 0;
92.     }
93.
94.     int N2 = N/2;
95.     int N5 = N/5;
96.     *result = 0;
97.
98.     for (int i=0; i<=N5; i++)
99.     {
100.         for (int j=0; j<=N2; j++)
101.         {
102.             if (((N - 5*i - 2*j) >= 0) && ((N - 5*i - 2*j) <= N))
103.             {
104.                 *result += 1;
105.             }
106.         }
107.     }
108.     return 1;
109.
110. STATUS SuperMethod(int N, int *result)
111.     if (result == NULL)
112.     {
113.         return 0;
114.     }
115.
116.     *result = 0;
117.
118.     for (int i=0; i<=N; i+=5)
119.     {
120.         *result += (i+2)/2;
121.     }
122.
123.     switch (N%5)
124.     {
125.     case 0:
126.         {
127.             break
128.         }
129.     case 1:
130.         {
131.             *result += (1*(N - N%5))/10;
132.             break
133.         }
134.     case 2:
135.         {
136.             *result += (2*(N - N%5))/10 + 1;
137.             break
138.         }
139.     case 3:
140.         {
141.             *result += (3*(N - N%5))/10 + 1;
142.             break
143.         }
144.     case 4:
145.         {
146.             *result += (4*(N - N%5))/10 + 2;
147.             break
148.         }
149.     }
150.     return 1;

0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：479782次
• 积分：6071
• 等级：
• 排名：第4269名
• 原创：43篇
• 转载：611篇
• 译文：1篇
• 评论：10条
最新评论