1-2-5组合问题的最高效完整算法
来源:互联网 发布:如何下载spss20软件 编辑:程序博客网 时间:2024/04/28 10:40
1-2-5组合问题的最高效完整算法
2010-01-17 16:1194人阅读评论(3)收藏举报
问题描述:用1,2,5这三个数组合,和为N。1,2,5的个数自己定,求有多少种不同的组合方法?
- /************************************************************************
- ***By : Summon ***
- ***Data : 2010.01.17 ***
- ***Function : 1-2-5组合之和等于100,文总共有多少种 ***
- *** 组合方式。 ***
- ***Method : 首先使用蛮力发,复杂度O(n^3/) ***
- *** 其次使用蛮力法的改进,复杂度O(n^2) ***
- *** 最后是超级算法,复杂度O(n) ***
- /************************************************************************/
- #include "stdafx.h"
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <time.h>
- typedefint STATUS;
- STATUS FoolHardinessMethod(int N,int *result);
- STATUS FoolHardinessMethodImproved(int N,int *result);
- STATUS SuperMethod(int N,int *result);
- int main(int argc,char* argv[])
- {
- clock_t start;
- clock_t end;
- double realTime;
- int N;
- printf("Please input the N!/n");
- fflush(stdin);
- scanf("%d", &N);
- int result = 0;
- start = clock();
- FoolHardinessMethod(N, &result);
- end = clock();
- realTime = ((double)(end - start))/CLOCKS_PER_SEC;
- printf("N= %d/n", N);
- printf("FoolHardinessMethod Running Time = %f/n", realTime);
- printf("result = %d/n", result);
- printf("/n");
- start = clock();
- FoolHardinessMethodImproved(N, &result);
- end = clock();
- realTime = ((double)(end - start))/CLOCKS_PER_SEC;
- printf("N= %d/n", N);
- printf("FoolHardinessMethodImproved Running Time = %f/n", realTime);
- printf("result = %d/n", result);
- printf("/n");
- start = clock();
- SuperMethod(N, &result);
- end = clock();
- realTime = ((double)(end - start))/CLOCKS_PER_SEC;
- printf("N= %d/n", N);
- printf("SuperMethod Running Time = %f/n", realTime);
- printf("result = %d/n", result);
- printf("/n");
- return 1;
- }
- STATUS FoolHardinessMethod(int N,int *result)
- {
- if (result == NULL)
- {
- return 0;
- }
- int N1 = N/1;
- int N2 = N/2;
- int N5 = N/5;
- *result = 0;
- for (int i=0; i<=N5; i++)
- {
- for (int j=0; j<=N2; j++)
- {
- for (int k=0; k<=N1; k++)
- {
- if (5*i + 2*j + k == N)
- {
- *result += 1;
- }
- }
- }
- }
- return 1;
- }
- STATUS FoolHardinessMethodImproved(int N,int *result)
- {
- if (result == NULL)
- {
- return 0;
- }
- int N2 = N/2;
- int N5 = N/5;
- *result = 0;
- for (int i=0; i<=N5; i++)
- {
- for (int j=0; j<=N2; j++)
- {
- if (((N - 5*i - 2*j) >= 0) && ((N - 5*i - 2*j) <= N))
- {
- *result += 1;
- }
- }
- }
- return 1;
- }
- STATUS SuperMethod(int N,int *result)
- {
- if (result == NULL)
- {
- return 0;
- }
- *result = 0;
- for (int i=0; i<=N; i+=5)
- {
- *result += (i+2)/2;
- }
- switch (N%5)
- {
- case 0:
- {
- break;
- }
- case 1:
- {
- *result += (1*(N - N%5))/10;
- break;
- }
- case 2:
- {
- *result += (2*(N - N%5))/10 + 1;
- break;
- }
- case 3:
- {
- *result += (3*(N - N%5))/10 + 1;
- break;
- }
- case 4:
- {
- *result += (4*(N - N%5))/10 + 2;
- break;
- }
- }
- return 1;
- }
- 1-2-5组合问题的最高效完整算法
- 1-2-5组合问题的最高效完整算法
- N皇后问题的两个最高效的算法
- N皇后问题的两个最高效的算法
- N皇后问题的两个最高效的算法
- N皇后问题的两个最高效的算法
- N皇后问题的两个最高效的算法
- N皇后问题的两个最高效的算法
- N皇后问题的两个最高效的算法
- N皇后问题的两个最高效的算法
- N皇后问题的两个最高效的算法
- N皇后问题的两个最高效的算法
- N皇后问题的两个最高效的算法
- N皇后问题的两个最高效算法
- 高效全组合算法
- 高效全组合算法
- 字符串匹配最高效的算法
- N皇后的最高效算法
- 第五周任务(3)
- c语言实现一个单元测试框架(Unit Test Framework)
- 数据结构的C实现_二叉树的非递归遍历和层序遍历
- 第五周任务(2)
- 《TCP/IP详解》笔记----第二章 链路层
- 1-2-5组合问题的最高效完整算法
- Windows Mobile Web Application支持ActiveX吗?
- 关于erron的使用
- 数据结构的C实现_图_邻接矩阵表示
- 不用库函数,自己实现strcpy和memcpy函数
- jQuery上传插件Uploadify详解及其中文按钮解决方案
- Linux USB subsystem --- USB File System initialize
- 数据结构的C实现_图_邻接表表示
- Haproxy安装、tcp/ip & http的负载均衡