惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Hugging Face - Blog
Hugging Face - Blog
Jina AI
Jina AI
宝玉的分享
宝玉的分享
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
人人都是产品经理
人人都是产品经理
博客园 - 聂微东
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
J
Java Code Geeks
博客园 - 【当耐特】
小众软件
小众软件
博客园 - Franky
S
SegmentFault 最新的问题
WordPress大学
WordPress大学
雷峰网
雷峰网
The Cloudflare Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
量子位
Last Week in AI
Last Week in AI
博客园_首页
月光博客
月光博客
IT之家
IT之家
阮一峰的网络日志
阮一峰的网络日志
Webroot Blog
Webroot Blog
Stack Overflow Blog
Stack Overflow Blog
腾讯CDC
云风的 BLOG
云风的 BLOG
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
W
WeLiveSecurity
Recent Commits to openclaw:main
Recent Commits to openclaw:main
D
Docker
The Last Watchdog
The Last Watchdog
有赞技术团队
有赞技术团队
Hacker News - Newest:
Hacker News - Newest: "LLM"
D
DataBreaches.Net
S
Security @ Cisco Blogs
Blog — PlanetScale
Blog — PlanetScale
GbyAI
GbyAI
TaoSecurity Blog
TaoSecurity Blog
S
Security Affairs
Y
Y Combinator Blog
O
OpenAI News
罗磊的独立博客
MongoDB | Blog
MongoDB | Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Forbes - Security
Forbes - Security
P
Palo Alto Networks Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
K
Kaspersky official blog
Cloudbric
Cloudbric

博客园 - Alex Lee

开始学习DW QextSerialPort 编译指南 [转]SlickEdit 设置点滴 [转帖]mysql消除乱码方法集 简体中文的MSI形式的安装程序显示乱码的处理 VoIP之语音编码器 SIP简介 SIP简介及工作原理 算法收集(不断更新) MYSQL数据同步 注册一个公司的费用和常见问题参考 创业者不得不去的10个网站! 硬件方案终于谈下来了,松了一口气,呼 44B0X串口扩展 Dependency下载网站 Windows平台上修改某个文件的创建、访问、修改日期时间属性 要做个P2P应用,先收集点相关基于UDP可靠传输的资料 规则文本数据文件根据关键字段进行排序的实现 ClassA A() 在幕后做了什么?
一个简单的算法题
Alex Lee · 2007-02-28 · via 博客园 - Alex Lee

还没有从过节的情绪中恢复过来,最近都不想写代码,越来越懒了。不想写代码,就写文档吧,今天花了一个上午时间写好了我们公司应用中定义的一个应用层协议的文档,手都打软了。。。。
下午跑到CSDN去逛了哈,看到很多人在讨论这个算法:
http://community.csdn.net/Expert/topic/5362/5362401.xml?temp=.5933344

求数值在 1 - 50 之内的任意5个数之和为100 。
数值:1,2,3,4,5,6....48,49,50
取其中的任意5个数,但这5个数相加之和要等于100,并将所有这种组合罗列
例: 1+10+19+20+50=100
     3+14+16+27+40=100
不能有重复

大多数都是循环实现,我给出我的一个递归实现吧(GCC编译器,DEVCPP下编译通过,计算时间我没算,反正马上出结果,应该不到1秒):

 1#include <conio.h>
 2#include <stdlib.h>
 3#include <stdio.h>
 4#include <dos.h>
 5
 6
 7#define NUM 5
 8#define LOWER 1
 9#define UPPER 50
10#define MATCH_TOTAL_NUM 100
11#define OUTPUT_FILENAME  "result.txt"
12
13void CountNext(int *num, int length, int total);
14
15int g_num[NUM];
16static int g_totalWays = 0;
17FILE *g_fp;
18
19void CountNext(int *num, int length, int total)
20{   
21  if(total < 0)
22  
23     return;
24  }

25  for(int i = LOWER; i <= UPPER; i++)
26  
27    if(length != NUM - 1)                                                          
28    {
29      if(g_num[length+1>= i)
30        continue;
31    }

32    
33    g_num[length] = i;    
34                                    
35    if(length == 0)  
36    {                  
37      if(total != i)
38      {
39        if(i == UPPER)
40        {
41          return;
42        }

43        continue;      
44      }

45      else
46      {
47        g_totalWays++;                            
48        for(int idx = 0; idx < NUM; idx++)
49        {
50          fprintf(g_fp, "%d ", g_num[idx]);
51        }

52        fputs("\n", g_fp);                                    
53      }

54      return;
55    }

56           
57    CountNext(num, length - 1, total - i);
58  }

59}

60
61int main() 
62{
63  unlink(OUTPUT_FILENAME);
64  g_fp = fopen(OUTPUT_FILENAME, "at");
65  CountNext(g_num, NUM - 1, MATCH_TOTAL_NUM);    
66  fclose(g_fp);
67  
68  printf("Total ways = %d\n", g_totalWays);
69  getch();  
70  return 0;
71}

72