小刀人
-No business too small, no problem too big.
<2010年3月>
28123456
78910111213
14151617181920
21222324252627
28293031123
45678910
公告
  • Learn,Think,and Imagine...

    正在看的书:

    点击发送消息给我

留言簿(6)

随笔分类

随笔档案

文章档案

相册

CSDN

VC知识库

开发站点

技术blog

搜索

最新评论

  • 1. purchase xanax
  • Some editors are failed writers, but so are most writers.
  • --purchase xanax
  • 2. neomwvbl
  • --neomwvbl
  • 3. order xanax
  • Think of yourself as an incandescent power, illuminated and perhaps forever talked to by God and his messengers.
  • --order xanax
  • 4. cialis for
  • It is your work in life that is the ultimate seduction.
  • --cialis for
  • 5. ffysegxg
  • --ffysegxg
  • 6. ammonation
  • God is a comedian playing to an audience too afraid to laugh.
  • --ammonation
  • 7. dyed
  • That would be a good thing for them to cut on my tombstone: Wherever she went, including here, it was against her better judgment.
  • --dyed
  • 8. ebullioscopic
  • We are made to persist. That's how we find out who we are.
  • --ebullioscopic
  • 9. keppra
  • They are ill discoverers that think there is no land, when they can see nothing but sea.
  • --keppra
  • 10. ampicillin
  • Any transition serious enough to alter your definition of self will require not just small adjustments in your way of living and thinking but a full-on metamorphosis.
  • --ampicillin
  • 11. reglan
  • The most erroneous stories are those we think we know best - and therefore never scrutinize or question.
  • --reglan
  • 12. oryxbxoc
  • --oryxbxoc
  • 13. linnaeite
  • In America, they want you to accomplish these great feats, to pull off these David Copperfield-type stunts. You want me to be great, but you don't ever want me to say I'm great?
  • --linnaeite
  • 14. humungous
  • --humungous
  • 15. doxycycline hyclate
  • There are sadistic scientists who hurry to hunt down errors instead of establishing the truth.
  • --doxycycline hyclate
  • 16. cephalexin 500mg hypermedia argyrophil
  • It is not enough to aim; you must hit.
  • --cephalexin 500mg hypermedia argyrophil
  • 17. female viagra
  • Good judgment comes from experience, and experience comes from bad judgment.
  • --female viagra
  • 18. sjkovbbg
  • --sjkovbbg
  • 19. datapath
  • We don't know a millionth of one percent about anything.
  • --datapath
  • 20. verticilliose
  • All truth passes through three stages. First, it is ridiculed. Second, it is violently opposed. Third, it is accepted as being self-evident.
  • --verticilliose

阅读排行榜

评论排行榜

 
VC知识库BLOG   首页  新随笔  联系  聚合  登录 
  随笔-23 文章-0 评论-184 Trackbacks-0

设有个背包,它能放的重量为S,设有N件物品,其重量分别为w1,w2,...,wn,希望从N件物品中选择若干件物品,所选物品的重量之和恰能放入该背包,即所选物品的重量之和等于S。可以用递归解决的基础是每次选择一个物品放入背包,那么剩余物品和背包剩余数量又构成一个新的背包问题。

#include<stdio.h>        /*N定义为7,即有7件物品;S定义为15,即背包能放的重量为15,比如15公斤*/
#define N 7                                
#define S 15                            
int w[N+1= {0,1,4,3,4,5,2,7};                /*给定各物品的重量值,放入数组w[N+1]中*/
int knap(int s,int n)                                    /*knap函数递归计算出符合选择要求的物品,并显示其重量。形参s 实际是放入物品i后,背包还能装载的重量,n为考查物品i后下一个待考查的物品。*/
   
if(s == 0return 1;                               /*如果s等于0时返回0并退出knap函数 */
     
if ( s<0|| ( s>0 && n<1 )) return 0;                               /*如果s<0或s>0同时n<1则返回0*/
     
if (knap(s-w[n],n-1))                            /*考查物品n被选择的情况*/
      
{
        printf(
"%d  ",w[n]);                              /*在屏幕上打印被选择的物品的重量*/
        
return 1;                                              /*返回1*/
      }

      
return knap(s,n-1);                               /*考查不选择物品n的情况*/
   }

main() 

      
if (knap(S,N)) printf( " OK! " );          /*如果knap最终返回1,打印OK!则表示成功选出一组物品*/
      
else printf( "N0! " );                               /*否则打印NO!表示没有选出合适的一组!*/
   }


用代码中的数据结果为:
1        5        2          7
OK!
如果物品用这一组0,1,1,3,1,5,2,1  就会是NO!

posted on 2005-02-26 19:16 小刀人 阅读(6751) 评论(3)  编辑 收藏